Stuck on Coding Exercise: Help Needed!

0
16
Asked By CuriousCoder89 On

Hey everyone! I'm a coding newbie and currently learning through freeCodeAcademy. I'm stuck on an exercise where I'm supposed to replace a log statement with a command to push the variable `i` to my `rows` array. Here's what I have so far:

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

for (let i = 0; i < count; i = i + 1) {
console.log(i);
}
```

I thought to replace `console.log(i)` with `rows.push(i)`, but I'm getting an error saying that I should call `.push()` on my `rows` array. I also tried to put `rows.push(i)` directly after `const rows = []`, which gave me a TypeError about reading properties of undefined. I even attempted placing it in the for loop as `for (let i = 0; i < count; i = i + 1; rows.push(i))`, but that didn't work either.

Can anyone help me understand what I'm missing? Thanks!

2 Answers

Answered By CodeCraze27 On

Just to add on, it looks like your issue might be related to how you're calling `push`. Avoid putting a space between `rows.push` and the parentheses. So it should be `rows.push(i)` without a space. Those little details can sometimes trip you up! Let me know how it goes.

Answered By HelpfulHarry42 On

You're on the right track! Just make sure to replace `console.log(i)` with `rows.push(i)` inside your for loop as you've planned. It should look like this:

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

That way, `i` will be pushed to the `rows` array in each iteration. Give it another shot!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.