Back to Freecodecamp

Step 111

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

latest1.5 KB
Original Source

--description--

Your pyramid is no longer inverted. This is because you are adding new rows to the end of the array.

Update your loop body to add new rows to the beginning of the array.

--hints--

You should use the unshift method of rows.

js
const stripped = __helpers.removeJSComments(code);
assert.match(stripped, /\.unshift/);

You should pass a padRow() call as the argument for your .unshift() method.

js
assert.match(__helpers.removeJSComments(code), /rows\.unshift\(\s*padRow\(/);

You should pass i as the first argument to your padRow() call.

js
assert.match(__helpers.removeJSComments(code), /rows\.unshift\(\s*padRow\(\s*i/)

You should pass count as the second argument to your padRow() call.

js
assert.match(__helpers.removeJSComments(code), /rows\.unshift\(\s*padRow\(\s*i\s*,\s*count\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
--fcc-editable-region--
for (let i = 1; i <= count; i++) {
  rows.push(padRow(i, count));
}
--fcc-editable-region--

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

/*for (let i = count; i > 0; i--) {
  rows.push(padRow(i, count));
}*/

let result = ""

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

console.log(result);