Back to Freecodecamp

Step 45

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

latest1.5 KB
Original Source

--description--

Now you have a series of # characters, but the pyramid shape is still missing. Fortunately, the i variable represents the current "row" number in your loop, enabling you to use it for crafting a pyramid-like structure.

To achieve this, you will use the .repeat() method available to strings. This method accepts a number as an argument, specifying the number of times to repeat the target string. For example, using .repeat() to generate the string "Code! Code! Code!":

js
const activity = "Code! ";
activity.repeat(3);

Use the .repeat() method on your character, and give it i for the number.

--hints--

You should use the .repeat() method.

js
assert.match(__helpers.removeJSComments(code), /\.repeat\(/);

You should use the .repeat() method on your character variable.

js
assert.match(__helpers.removeJSComments(code), /character\.repeat\(/);

You should pass i to your .repeat() method.

js
assert.match(__helpers.removeJSComments(code), /character\.repeat\(\s*i\s*\)/)

You should use the .repeat() method in the .push() method

js
assert.match(__helpers.removeJSComments(code), /rows\.push\(\s*character\.repeat\(\s*i\s*\)\s*\)/);

--seed--

--seed-contents--

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

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

let result = ""

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

console.log(result);