Back to Freecodecamp

Step 71

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

latest1.3 KB
Original Source

--description--

Your pyramid generator now functions as expected. But this is an excellent opportunity to further explore the code you have written.

The addition operator is not the only way to add values to a variable. The <dfn>addition assignment</dfn> operator can be used as shorthand to mean "take the original value of the variable, add this value, and assign the result back to the variable." For example, these two statements would yield the same result:

js
test = test + 1;
test += 1;

Update your iteration statement in the for loop to use addition assignment.

--hints--

Your for loop should not use i = i + 1;

js
assert.notMatch(__helpers.removeJSComments(code), /i\s*=\s*i\s*\+\s*1/);

Your for loop should use addition assignment with i.

js
assert.match(__helpers.removeJSComments(code), /i\s*\+=\s*1/);

--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);
}

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

let result = ""

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

console.log(result);