Back to Freecodecamp

Step 103

curriculum/challenges/english/blocks/learn-introductory-javascript-by-building-a-pyramid-generator/660f4b641290da41b2cf0dd9.md

latest1.5 KB
Original Source

--description--

Just like addition, there are different operators you can use for subtraction. The <dfn>subtraction assignment</dfn> operator -= subtracts the given value from the current variable value, then assigns the result back to the variable.

Replace your iteration statement with the correct statement using the subtraction assignment operator.

--hints--

Your for loop should not use i = i - 1.

js
assert.notMatch(__helpers.removeWhiteSpace(__helpers.removeJSComments(code)), /for\(leti=count;i>0;i=i-1\)\{rows\.push\(padRow\(i,count\)\);/);

Your for loop should use subtraction assignment to reduce i by 1.

js
assert.match(__helpers.removeJSComments(code), /for\s*\(\s*let\s+i\s*=\s*count\s*;\s*i\s*>\s*0\s*;\s*i\s*-=\s*1\s*\)\s*\{\s*rows\.push\(\s*padRow\s*\(\s*i\s*,\s*count\s*\)\s*\);/);

--seed--

--seed-contents--

js
const character = "#";
const count = 8;
const rows = [];

function padRow(rowNumber, rowCount) {
  return " ".repeat(rowCount - rowNumber) + character.repeat(2 * rowNumber - 1) + " ".repeat(rowCount - rowNumber);
}

// TODO: use a different type of loop
/*for (let i = 1; i <= count; i++) {
  rows.push(padRow(i, count));
}*/

/*while (rows.length < count) {
  rows.push(padRow(rows.length + 1, count));
}*/

--fcc-editable-region--
for (let i = count; i > 0; i = i - 1) {
  rows.push(padRow(i, count));
}
--fcc-editable-region--

let result = ""

for (const row of rows) {
  result = result + row + "\n";
}

console.log(result);