Learn Introductory JavaScript by Building a Pyramid Generator - Step 70

Tell us what’s happening:

Can anybody explain this process in detail
I want to know the detailed explanation of the below questions

In my POV the parameter “rowCount” carries the value of Count which was declared in the for loop
The value of count is the default value “8”
If the rowCount is 8,
How " ".repeat(rowCount - rowNumber) is able to properly divide the spaces??
Can somebody please explain the process with an example of rownumber or i = 1 ??

question 2
Also What change is this statement able to make??
function padRow(rowNumber, rowCount) {
return " ".repeat(rowCount - rowNumber) + character.repeat(rowNumber) + " ".repeat(rowCount - rowNumber);
}

In the above function,
with this line // character.repeat(rowNumber)
the output is

               #       
              ##      
             ###     
            ####    
           #####   
          ######  

How are the spaces adjusted just by updating the below line??
character.repeat(2 * rowNumber - 1)
Even it is gonna return the same output?

Your code so far

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


// User Editable Region

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

// User Editable Region


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

let result = ""

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

console.log(result);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36

Challenge Information:

Learn Introductory JavaScript by Building a Pyramid Generator - Step 70

You’re correct rowCount takes the value assigned to count

Can you see where rowNumber gets it’s value?

I think its the argument provided to the padRow function in the for loop
Which is i + 1

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

1 Like

Correct, so you can see that i increments and rowNumber changes and this affects how many spaces and # are printed?

I hope this answers your question?

I know that when i increments, the rowNumber changes
But how the process of subtraction works?
For suppose if we consider i = 1
Then " ".repeat(rowCount - rowNumber)
" ".repeat(8 - 1)

I’m sorry I got the clarity when I’m typing the above question
I didn’t realize that it is gonna leave the i spaces on each side
I actually took a pen,paper & confirmed that
Thank you for making me dwelve into the deep
Thank you!

1 Like

I think it’s great that you took the time to follow the logic through and really understand it. It can get very abstract, especially when you are following a guide like this.

If you are designing the program yourself I think it gets much easier to follow because you are really “inside” the problem. It’s more difficult to understand logic that’s already written (which is why it’s important to leave comments in your code for other people to follow)

1 Like