Help me to understand


#1


Hi, I don't really understand about this while loop stuff... I just want to understand it better.
So... the problem is when I have submit the code, the error message pop up tells me that I forgot to log "Looped once!" even though I have put it inside while function... But when I log it outside while function the code is right... I don't really understand do I have to log it twice inside and outside while function? Or this is just a bug?


// this is the code when I got error message
var soloLoop = true;
var soloLoop = function(){
  //Your code goes here!
  while (soloLoop === true) {
     console.log("Looped once!");
     var soloLoop = false;
    }
};

soloLoop();


//this is the right one after I log it outside while function
var soloLoop = true;
var soloLoop = function(){
  //Your code goes here!
  while (soloLoop === true) {
     console.log("Looped once!");
     var soloLoop = false;
    }
};

soloLoop();
console.log("Looped once!");

Can anyone help me to understand this???


#2

I think there is a more pressing issue first. You use the same variable name for your function and your variable which holds the boolean value.

I would change sololoop here:

var soloLoop = true;

to something like loopCond, this then also goes for your while loop condition and where you re-assign soloLoop false

Using the same variable name can have annoying consequences (as you just experienced)


#3

Sorry I don't really get it what do you mean... I've tried to change var name as you said it, and delete the last log outside while function... But it's still the same error.. (Sorry if I take your word literally)

For while condition I don't really understand what kind of condition that I must put... Whenever I tried to put false condition the error message pop up tell that I didn't print the string to the console...

Sorry for idiot comment, it's just I'm not satisfied with my current code... So I want to learn from someone who are more knowledgeable than me...


#4

you have a function called soloLoop, agree?

You also have a variable called soloLoop, agree?

This results in a name conflict. Here:

while (soloLoop === true)

soloLoop is a variable, but containing what? A boolean value or a function?

thankfully JS has a function which can tell us:

var soloLoop = true;
var soloLoop = function(){
  //Your code goes here!
  console.log(typeof(soloLoop))
  while (soloLoop === true) {
     console.log("Looped once!");
     var soloLoop = false;
    }
};

soloLoop();

typeof() tells of the data type

It shows undefined. Js is not my area of expertise, but its seems your choice of naming variable has some unwanted/unintended consequences


#5

Ahh you were right.. It's make my var undefined...
But when I tried to changed var name as you said last time, it's still tells me that I forgot to log "Looped once!"... So I still wondering what's wrong with my code??

Last time I tried to edit my code, and put console.log above while function and it's works!!! Now I'm more confuse, isn't the instruction tells that I have to put log "Looped once!" inside the loop function??? and not above???


#6

Can i see an updated version of your code?


#7

var soloLoop = true;
var soloLoop = function(){
  //Your code goes here!
  console.log("Looped once!");
  while (soloLoop === true) {
     var soloLoop = false;
    }
};

soloLoop();

#8

here:

var soloLoop = true;
var soloLoop = function()

how can you have the same variable name for a boolean value and a function? Doing this causes problems, how is JS to know when to use the boolean value and when to use the function given they have the same variable name?


#9

Yes I understand... But still, I have tried to changed it var boolean name with your var name loopCond... But the error message still pop up...
Here's screenshot for the error... (Sorry if I persistent.. :worried: )


#10

that seems fine, can you copy paste your code to the forum so i can run it?

Try refreshing the page


#11

var loopCond = true;
var soloLoop = function(){
  //Your code goes here!
    while (loopCond === true) {
     console.log("Looped once!");
     var loopCond = false;
    }
};

soloLoop();

#12

it seems you have a variable scope problem. loopCond should be a global variable.

Place loopCond inside your function:

var soloLoop = function(){
  //Your code goes here!
    var loopCond = true;
    while (loopCond === true) {
     console.log("Looped once!");
     var loopCond = false;
    }
};

soloLoop();

#13

So the conclusions is that I should put var name above the loop... not above the function???


#14

js variable scope my strong suit, it should actually be:

var loopCond = true;
var soloLoop = function(){
  //Your code goes here!
    
    while (loopCond === true) {
     console.log("Looped once!");
     loopCond = false;
    }
};

soloLoop();

the problem was here:

var loopCond = false

by using var, you made loopCond a local variable, causing problems


#15

Now I understand what's wrong with my code... Thank you so much... :grin:


#16