12. Loops and arrays II


#1


https://www.codecademy.com/courses/javascript-beginner-en-NhsaT/2/4?curriculum_id=506324b3a7dffd00020bf661


Hi, i just finished this task by writing code below, it was accepted, but the output was strange:

I know someone called 1
I know someone called 2
I know someone called 3
I know someone called 4
I know someone called 5
I know someone called function (testArr) {
if (this.length !== testArr.length) return false;
for (var i = 0; i < testArr.length; i++) {
if (this[i].equal) {
if (!this[i].equal(testArr[i])) return false;
}
if (this[i] !== testArr[i]) return false;
}
return true;
}

Why did this happen?


var names = ["1", "2", "3", "4", "5"];
for (var name in names) {
    console.log("I know someone called " + names[name]);
}


#2

Hi their,

That isn't the right way to make a for loop you make on like this:

for(var i = number; i < something; i++) {
   // code code code
}

so maybe if you did it like this:

var names = ["Steven","Will","Ezra","Patrick","Hannah"];

for(var i = 0; i < names.length; i++) {
    console.log("I know someone called " + names[i]);
}

it would work out,:relaxed:

hope this helps,

Steven


#3

Did you just used the code below? Couldn't reproduce it using the code on the exercise in the link.
Was there an error message or just this output?

PS: Also be careful to use "name" as a variable name because it is something like a keyword or at least has some features you would not expect it to have (e.g. it is always a string even if you assign a number or boolean).


#4

Thanks for the answers,

I know how it should be using the names[i] syntax, I just wondered, what if I make it another way =)

I just cheched, and found something: if I just click "Save and Submit", it works fine with no strange output. But if I previously finish task 11. Loops and arrays
with code (I don't know, mb result will be same with another code)

var cities = ["Melbourne", "Amman", "Helsinki", "NYC", ""];

for (var i = 0; i < cities.length; i++) {
console.log("I would like to visit " + cities[i]);
}

and then do task 12 with code I wrote in the first post, the output has strange ending.
Looks like a bug


#5

Thanks for your information. It seems like the test function of exercise 11 adds a new function to the array objects meaning all arrays have this new function that you can see. For a quick workaround you could just refresh the page that should get rid of this new "feature". Anyway nothing wrong with your code and there will hopefully be a fix to this bug someday!

PS: As you can see for in loops might also reveal properties that you might not be aware of :slight_smile:


#6

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