Back to Freecodecamp

Step 47

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

latest1.7 KB
Original Source

--description--

The logic for formatting this pyramid is likely going to get complicated, which means it's a great time to extract that code into a function.

A <dfn>function</dfn> is a block of code that can be reused throughout your application. Functions are declared with the following syntax:

js
function name(parameter) {

}

The function keyword tells JavaScript that the name variable is going to be a function. parameter is a variable that represents a value that is passed into the function when it is used. A function may have as many, or as few, <dfn>parameters</dfn> as you'd like. Like a for loop, the space between the curly braces is the <dfn>function body</dfn>.

Declare a padRow function. Do not create any parameter variables yet. The function body should be empty. Remember that you need to use camel case for your naming convention.

--hints--

You should use the function keyword.

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

You should declare a padRow function.

js
assert.isFunction(padRow);

Your padRow() function should not have any parameters.

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

Your padRow() function should have an empty body.

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

--seed--

--seed-contents--

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

--fcc-editable-region--

--fcc-editable-region--


for (let i = 0; i < count; i = i + 1) {
  rows.push(character.repeat(i + 1))
}

let result = ""

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

console.log(result);