I'm getting really discouraged with loops

here’s one last hint for the day.

the thing is that if you need 15 different random indexes, then this much is clear that you would have to calculate them 15 different times so shouldn’t you add the logic to calculate the randomIndex inside of the loop.

1 Like

I figured it out right before you responded! You have been so much help! Thank you so much!

I’m going to practice with this and really try to drill loops into my head!

haha that’s nice! also don’t worry about it. hit me up if you ever need my help with other coding stuff :wink:

I have one more simple question if anyone has the time to answer.

Would it be correct to think of loops as basically a function that you want to repeat x number of times?

It was really hard to wrap my head around loops but now that I look at it in that way it makes more sense.

so, is a loop basically the same thing as a function that you can repeat for as many times as needed?

A function is a way for a programmer to encapsulate (bundle together) a bunch of lines of code and probably give it a descriptive name so you can use it multiple times.

for example instead of typing

let num1 = 12
let num2 = 12
let num3 = 12

let sum  = num1 + num2 + num3
let squareOfSum = sum * sum

i can just write a function that will take in three numbers and return the result of this functionality even though i only define it once, after which i just have to use it like

sumAndSquare(12, 12, 12)

A loop however is similar in a way that it lets you bundle up code and repeat the code inside of it multiple times but still different such that you cannot run a loop multiple times by giving it a name if that makes sense :joy:

I see that the guys here managed to help you through your problem and this is why I really think FCC is a great place for beginners.
With that being said, my advice to you (and I am also a beginner so I understand your situation) is the same advice given to me by another professional developer :
If you can’t understand what’s going on inside your code and why it’s giving you weird or unexpected results, just console.log EVERYTHING!!!
Whether it was variables, functions, arrays, loops, methods, every iteration or anything. The console will show you exactly what’s really happening within and that (if it doesn’t give you an idea about how to fix the problem) will at least help you in your research for a solution and shows you what’s wrong with your code.
Happy coding

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.