Ok, what's wrong with my code?


#1

Hi! I tried to keep my code simple, but it's so simple the computer is in wonderment. Help!

var countTwo = function();
for (var i=0; i<61; 1+=2); {
console.log(i);
}
var countTwo = true
while (understand);{
console.log("Middle of the way!");
}
countTwo=false
do {
console.log("Almost done");
i+=2
}
while (i<30);
};
countTwo();


#2

Could you give me the instructions for this exercise? Your code is full of mistakes everywhere :frowning: Please give me the instructions and I will correct it according to that. I can't understand what you tried to do there. You defined a var countTwo inside itself, changing it from storing a function to a boolean... you also wrote many ; where it must not be... and the first for will never end, as the 3rd part is simply saying 1+2 one and over again and again and again... You should use i+=2


#3

Er, yes! I tried to write a code to count by twos to 60 while printing out a few things at the beginning, halfway through, also. It's the Review to writing loops, number 11. The 1+=2 is an error, but there are quite a few.


#4

Okay let me write the code I would use.


#5
    var countTwo = function() {
       for (var i=0; i<61; i+=2) {
          console.log(i);
          if (i == 30 || i == 31) {
             console.log("Middle of the way!");
          } else if (i < 30) {
             console.log("Still far away");
          } else {
             console.log("Almost done");
          };
       };
};
countTwo();

The function has its name. You use a for inside which will increase the number 0 by 2 everytime that the code inside the for has been run.
While the code inside is being run, it will check if the number is 30 or 31. If it is any of those, it will say its half way through. If it is lower than 30, it says that its far. If it is anything else (different from 30 or 31, or greater than 30) it will say almost done. At the end you call the function


#6

Sorry, I forgot to mention that all three types of loops should be included; for, while and do while. But what you wrote makes loads more sense. I got confused trying to include all three in one running code.


#7

I don't really get an idea of how to include all three. Could you give me the exact thing it asks you for?


#8

Yes, here are the instructions.
"To finish up and prove your loop mastery, write three syntactically correct loops in the editor: one for, one while, and one do. Beware of infinite loops!" That's all it says!


#9

Sorry it took me so long to answer back. I imagine that for this time you may have solved it. In the case you haven't, then try to use 3 different loops, not all in one. The instructions don't say that you must put all the loops in 1 function.


#10

Thank you! I did get past it!