I need help with step 99 in JavaScript Algorithms and Data Structures. Nothing I’m doing is right and once again the program does not offer a proper guidance to a solution.
Your code so far
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
/*for (let i = 1; i <= count; i++) {
rows.push(padRow(i, count));
}*/
/*while (rows.length < count) {
rows.push(padRow(rows.length + 1, count));
}*/
// User Editable Region
for (let i = count; false; i--) {
}
// User Editable Region
let result = ""
for (const row of rows) {
result = result + row + "\n";
}
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/131.0.0.0 Safari/537.36 Edg/131.0.0.0
Challenge Information:
Learn Introductory JavaScript by Building a Pyramid Generator - Step 99
Let me try to guide you here without giving the answer away too easily.
First off, you really need to understand the code you are using before you use it.
Especially when learning, you don’t want to get things right by accident. The problem won’t go away. You’ll only encounter more complex code that leverages the knowledge of this simple task.
The for-loop has 3 parts (expressions): ( executed-once, condition, executed-every-time)
So, in your for loop, you are saying: i = count
condition is: false i-- , ie: decrease the value of i on each iteration.
The condition part is what decides if we keep looping. This is the fundamental part of a loop.
If that condition evaluates to false, then the loop is finished.
In your case, you just put false. Which means it’s always false. This loop will never run even once, and the fact that you set i = count will have no effect.
Ask yourself.
What is the use of i , and why are you decreasing it by 1 in each iteration?
What is the condition that should be met that decides the loop should be finished.
You also put no logic inside the loop code block, so even if the loop does run x-times, nothing would happen anyway.
Going forward I suggest you get in the habbit of:
researching every aspect of the problem you are about to solve - this will be tedious at first while you’re still learning everything. Use the Mozilla docs and find other resources that help you. Research is part of the craft. ( Learn about loops here: Loops and iteration - JavaScript | MDN )
When you do start to code, code with purpose. If you just write stuff and hope it will work, you will have learned nothing. That is not to say that trial and error is bad, as long as you use the outcome of trial and error (tinkering) to understand how things work. Not just taking random guesses.
Thank you so so much for all the advice. I’ve never done javascript before and I’m taking a Front End Web Development Diploma program so a few of us are struggling because our professor doesn’t explain things very well. I trying to use freeCodeCamp but when you’re stuck on a problem the program doesn’t help much. I’m finding Javascript really difficult to wrap my head around so I go over things several times to try to make sense of it and understand the logic! I’ll give it a try and see what works. I used false instead of i-- but that didn’t work either. I’ll keep trying!!!
I’m just going to start the section all over even though i’m close to the end. I find things sink in more after i do them a few times. I’m really stuck on this one and getting incredibly frustrated and i can’t keep wasting time on one problem. That’s not efficient or helping me learn either. Thank you for trying to help. It’s greatly appreciated!