Lesson 6/11 - Not printing to console


#1

I feel like I'm close, but can't seem to break out of the loop and print to console. I'm feeling a bit lost on this concept in general! Please advise:

//Remember to make your condition true outside the loop!
var soloLoop = true;
var soloLoop = function(){
  //Your code goes here!
  while(soloLoop = true) {
  console.log("Looped once!");
  soloLoop = false;
  }
};

soloLoop();

Thanks in advance!


#2

hi,

your while condition is not right
There you should compare

while(soloLoop === true)

or

while(soloLoop)

#3

@legend79:

This is an important concept:

= in JavaScript is the assignment operator, which means its only job is to assign things. For example, var i = 5; only ever sets the variable i to 5. It will never check values.

Next, there are two sets of two equality operators, which check values and return true or false.

  • == and != are a one set of equality operators: these will check for equality after doing type conversions. This means that it will check for equality only after making sure both things being compared are the same type...for example, string vs string, int vs int, etc. This makes it, for the most part, a slower operator.
  • === and !== are the other set of equality operators: these will check for equality ignoring the type. This means it will check for equality, without regard to whether or not they are the thing type of object...for example, string vs int, int vs boolean, etc.

With these facts in mind, can you answer this: "what is wrong with your code?"?






The Answer Is...

You are using the wrong operator in your while loop.


#4

Btw it is not a good choice to name the variable used in the condition sololoop as well because after these two lines:

var soloLoop = true;
var soloLoop = function(){

soloLoop is most likely the function itself and not nice little boolean.


#5

Thanks for this. I see what you mean by using the wrong operator. If I understand correctly, in my while loop while(soloLoop = true) { the = is incorrect because it appears as though I'm assigning the value 'true' to soloLoop. Whereas the correct answer for this would be to use while(soloLoop === true) { instead. Is this right?


#6

100%! Great job, you're correct.


#7

@haxor789 - would you then recommend using myCondition instead as the var name?

ex:

var myCondition = true
var soloLoop =  function() {
  while(myCondition === true) {
    console.log("Looped once!");
    myCondition = false;
  }
}

Still seems like I'm missing something...


#8

UPDATE: Just applied this code and it worked! Thanks guys!


#9

What he means is you named a variable soloLoop, but then you made another variable of the same name and defined it as a function effectively overwriting it.

Does that make sense?


#10

Yes...this makes perfect sense now. Thanks again @jacobandersen!


#11

One last thing...why put soloLoop(); at the very end??


#12

The loop is wrapped in a function and a function is kind of like a sub program that needs to be started and soloLoop() is the starting command. Wasn't the whole 3rd lesson on function?


#13

probably, I must need to go back to that part :smile: