Js - rapper array

Hey !

So , inside the loop topic I was doing this exercise which requires me to " Log each element from rapperArray in a for loop with the iterator variable i " . I have done this :

It logs each element of the array , but still there is no ticker on my box to let me advance. I dont see what I am doing wrong ?

can you also provide the exercise url?

try using curly brackets:

for ( i = 0 ; i < 4 ; i++) {

also. i wouldn’t hard code the value 4 in the loop. If you now change the array, you introduce a bug.

Hey stetim !

I think the url is this : https://www.codecademy.com/courses/introduction-to-javascript/lessons/loops/exercises/break-keyword

edit : Unfortunately the curly brackets {} didnt solve it

edit 2 : I have also tried different stoping values but doesnt work anyway

ideally, use .length, this way the array can change without breaking the loop

the problem is actual here:

i = 0

now i leaks outside the loop/intended scope:

for ( i = 0 ; i < 4 ; i++) {
console.log(i); // pollution

use block scope.

1 Like

Hey stetim , So I’ve made this tweak

but still it doesn’t work ( the box doesnt give me a ticker to advance :frowning: ). Can i assume the exercise is bugged and I have done what I should have ?

The exercise works fine. My code only demonstrated the problem and suggested the fix (use block scope for i variable). I will agree with you that the error message of the lesson is a bit misleading, although I understand the exercise wanting to enforce good practice (using right scope for variable)

good that you decided to use rapperArray.length :slight_smile:

Thanks stetim ,

So I know , ideally variables should be defined within the body of the function or statement they are being used in and we should avoid global scope so that variables dont hit each other , but I dont see what you mean , in this case , by block scope ?

Yet, in your case, i ends up in the global scope.

a for loop is block, so block scope means the i variable should only exists within the loop. so trying to access i after the loop, should throw an error.

If i google: javascript block scope

I get some good explanations. Being able to research things you don’t know, is a good skill to have :slight_smile:

1 Like

Having been through this exercise recently, i seem to recall having the same issue with the exercise not marking as completed.

Try defining your i variable like this: (let i = 0 …)

1 Like