Back to Freecodecamp

Step 118

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

latest1.6 KB
Original Source

--description--

Nice work! Experiment with different values for your character, count, and inverted variables.

When you are ready to move on to your next project, set character to "!", count to 10, and inverted to false to continue.

Congratulations on completing your first JavaScript project!

--hints--

You should set character to "!".

js
assert.equal(character, "!");

You should set count to 10.

js
assert.equal(count, 10);

You should set inverted to false.

js
assert.equal(inverted, false);

--seed--

--seed-contents--

js
--fcc-editable-region--
const character = "#";
const count = 8;
const rows = [];
let inverted = true;
--fcc-editable-region--

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

for (let i = 1; i <= count; i++) {
  if (inverted) {
    rows.unshift(padRow(i, count));
  } else {
    rows.push(padRow(i, count));
  }
}

let result = ""

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

console.log(result);

--solutions--

js
const character = "!";
const count = 10;
const rows = [];
let inverted = false;

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

for (let i = 1; i <= count; i++) {
  if (inverted) {
    rows.unshift(padRow(i, count));
  } else {
    rows.push(padRow(i, count));
  }
}

let result = ""

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

console.log(result);