6. Why else didn't this work?


#1

Hi @igorcaletacar,

Could you (or anyone else reading this) explain how to come up with the code that works? My attempts before I saw your code were completely off track, structurally.

For instance, this is what I was doing:

var sample = 0;

var soloLoop = function() {
while (sample < 1)
console.log("Looped once!");
sample = 2;
}

soloLoop();

I can see that I forgot the curly brace after the while condition, but why else didn't this work?

Thanks!


6. Solo flight
#2

Of course. I will try to explain it as good as I can.
So, this is your code.

var sample = 0;

var soloLoop = function() {
  while (sample < 1)
    console.log("Looped once!");
  sample = 2;
}

soloLoop();

I've written it with indents so you see what the problem is.
First you set some condition variable, that is called sample. Now that part is fine. You set initial condition variable to 0, set loop condition to be less than 1 and you wanted to change condition variable to 2 later. But you made a mistake there.
If you check my indents, you can see that without {} your while loop will only do console.log(). It wont reach to other parts of your code, because without {}, it only executes one command (that's why when you have only one command inside loop you don't have to use {}, but it is good practice to still use it).
So, what you have here is actually infinite loop. Your condition will always be 0, it will never change, and 0 < 1 will always evaluate to true.
Your code is not completely off and that is actually your only mistake. So to prevent infinite loop, you have to put {} after your while loop and put both console.log() inside and the part where you change your condition variable.

Since you know you forgot {}, I wont write the solution for you because you already know it. I think this is good explanation, but if you need more specific one let me know what part you don't understand and I'll try to explain it again in different way.


#3

Thank you, your explanation was very helpful! I appreciate you taking the time to answer at length. You cleared up some other things I was confused about, too. :slightly_smiling:

(Sorry for the delay in responding..)

Claire


#4

Glad I could help, if you need more explaining or help with other topics feel free to ask. :slightly_smiling:


#5

Hi Igor, could you tell me why my code isn't working? (also, what is the best website to use to test my code online):
var weather = "cold";

while (weather) {
console.log("It's cold today");
} else {
console.log("It's warm out!");
}

if (var i = 0; i < 10; i++) {
console.log("There have been " + i + " warm days");
}


#6

At least in javaScript while has no else condition and are you aware of what truth value "cold" has?
PS: and your if condition on the bottom should rather be a for loop, shouldn't it?


#7

@haxor789 explained it already, but I can do it too.

For start, your while loop looks wrong. You should either use while (or we can call it while do, because while condition is true we do something, but we first check the condition and then do something) or do while, while else shouldn't be used (at least not in JavaScript). You can write loop that will have similar effect if you really need it, but while else is a no-no.

Next, if it worked, it would be infinite loop. You have weather = "cold", and that will evaluate to true inside while condition (I think it will, but for more explanation why this is bad practice because we don't know what to expect in condition check this post, the first answer where tables are shown).

Finally, your if loop at the end should actually be for loop. You have everything set for for loop, but still used if. I believe you just made a little mistake here.

I hope this sums it for your, it's ok to try new things and do them wrong, glad you asked about what you didn't understand. If you have any more questions, feel free to ask.


#8

Thank you so much for your reply. I am having a challenging time trying to study Javascript on my own. Is there any other resource you could recommend to me?


#9

Thank you very much!


#10

Normally I wouldn't really recommend W3Schools, as there are better sources, but other than Codecademy, it's probably the best place to look if you're just starting. If you're interested in reading books about JS let me know, I can recommend some, otherwise, check this and try to write short and simple scripts of your own. They don't need to to anything complex, basic prompt(), log(), etc. functions are good start.


#11

Thank you again Igor, I really appreciate your input.