11. Review (For/While/Do)


#1




I keep getting SyntaxError: Unexpected token and I'm such a noob I'm not sure what this means. Also my code isn't working correctly and I'm worried I am not picking this up well! Please can someone help me with this code?

Thanks


Replace this line with your code.


#2

this code has several problems. why did you do this:

cities.length < 4

in your for loop? You wanted to compare the length of cities, with 4, why?

I would do:

i < cities.length

this will ensure, all city's are listed. i will keep increasing, till it passed all city's

Your while loop:

while (count <= 3) {
    console.log("I hope I go soon."); 
}

is a infinity loop, nowhere in the loop is the count increased, it will stay zero forever.

General syntax for a do while loop:

do { 
  // do something
} while (condition);

the whole while part of your do while loop is missing. You might want to revisit some exercises, to improve your understanding of loops


#3

First problem I see would be the infinite for loop:

cities.length <4

cities.length is a constant (as the length doesn't change in your code) and it has a value of 3 so 3 < 4 is and will always be true. Instead you might want to check your counting variable i against the length of the array. And the other condition seems to be weird as well. count is false so you check if false is less than three. If you google it you might find out that false has a numerical value of 0 and therefore 0 <3 is again always true. But even without this information it is obvious that comparing false and 3 is not a good idea.

Maybe have a look at this thread for a review on loops:


Also think what these loops do and how you could use this feature to solve your problem.
e.g. in the first case you use that the array indices are integer values with a step-width of 1. So you create a loop that is as well has a step-width of 1 e.g.

for(var i=0;i<array.length;i++)

now this loop does nothing else then counting up in steps of 1. In the first loop it is 0 after the first loop it increases to 1. So in the next loop it is 1 and so on until i is bigger than or equal to array.length then the condition is false and the loop stops. So what you want to do is to use the value of i to get the array value so array[i] (where array is a placeholder name for an array) would now give you all the entries in order.