For Loop Only Running The First Line


#1

So, I have this code here:


var count = i;

for(var i = 1; i < 6; i++){
if (i === 1){
i = "first";
}
else if (i === 2){
i = "second";
}
else if (i === 3){
i = "third";
}
else if (i === 4){
i = "fourth";
}
else if (i === 5){
i = "fifth";
}
else {
console.log("Error");
}
console.log("This is the " + i + " sentence.");
};


When I run it, it only shows the first line and then stops. Can anybody tell me what I am doing wrong?


#2

Ah,

I'm not expert but the way I see it is you're using the variable i for the same thing and increment it.
So your for loop starts at i=1 then it gets set to "first" then you increment first+1.At this point it's greater then 6 and your for loop is over.

Hope that makes sense, you can fix it by changing the variables to be different.


#3

True you could fix it by declaring a new variable to represent the numbers first, second, third and so on. Your code showed the first line but stopped there because the first if statement assigned the string "first" to your variable i and i++ simply won't work so the condition i<6 or first<6 will return false.