6/11 Solo flight


#1

I'm struggling to see where I've gone wrong here...
//Remember to make your condition true outside the loop!
var bool = true
var soloLoop = function(bool){
while bool = true{
console.log("Looped once!");
bool = false;
}
};

soloLoop();

#2

Hi,

while bool = true should probably be var bool === true, i don't know javascript though, so I could be wrong.

I meant while bool === true, with while instead of var


#3

It threw up a different error saying var bool === true, didn't like that I included var.

The issue I'm having is an 'Unexpected identifier' if that helps.


#4

Hey @systemplayer60038! So your issues were that. When you put a while statement you do it like this:

while(bool===true){
console.log("Looped once!");
bool = false;
}

Its not because of the var. Its your syntax of your while loop and your opperator(suppose to be === not =) one equal sign is a assignment operator, where you assign values.

Hope this helped!


#5

@amanuel2 That makes a lot of sense, but it's still not working.

Here's my current code:

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

#6

missing a semi colon after defining your bool variable. Don't be hasty! Write your code properly!


#7

@emgo_dev That was probably something that needed fixing, but it is still not working :frowning:
No errors either.


#8

Where you are defining the parameter bool, even though it shares the same name with an existing variable, it doesn't simply bring it into the function whenever it's called. This is a problem when naming parameters that share names with existing variables too, it's confusing. However calling bool in the function will bring it from the global scope, because that's how global scope works, you don't even need that parameter, however the proper way to use that parameter would not be to rely on the variable at all, because when a parmeter is defined, the function creates a local scope variable from that placeholder value to be used.

So defining bool as a parameter you could simply do this soloLoop(true); and your function will assign true to bool on the local scope. you aren't getting a syntax error because this isn't a syntax issue, it's a problem with the return, which isn't returning what you need, because you haven't given it the value it needs. It's basically skipping over the loop, because your function bool is equal to null.

Now what you could also do is this soloLoop(bool); see at least here you are passing the value from bool into the function. What you were doing earlier, is just stating the structure of the function, but when you call it, it's important to remember that structure.

I hope that clarifies it a bit.


#9

Tu problema es que dejas la variable var bool=true fuera de la funcion, además escribis por gusto bool dentro del parametro de la funcion y un pequeño consejo siempre ahorrate codigo, si la variable ya es true no tienes porque escribir: while(bool === true) solo escribi while(bool) En conclusion el ejercicio te tiene que quedar así.

var soloCiclo = function(){
//¡Tu código va acá!
var bool = true;
while(bool){
console.log("¡Un ciclo!");
bool = false;
}
};

soloCiclo();


#10

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

soloLoop();

-it's like that.


#11

you just answer my problem! but i got another question that might be easy but just to be clear, is there any reason why the variable outside the function need to be true? i was doing it the other way around:

var java = false;

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

};

soloLoop();

but as soon as i changed the true values for false and the other way around everything went perfect with the variable outside the loop set to false i had to add the === false to the while loop in order to log, but with the variable true outside the while loop settle with while(myVariable) and not while(myVariable === false)


#12

ok i just read the instructions and i often try to not look at them cause it makes it easier but the instructions says it needs to be true, this is because of the instruction or its simply how javascript works?


#13

Well this is how while loops work, they run the code in {} as long as, or better while the condition is true. If you want to start with false you need to make this condition become true e.g. !java (read as not the value of java, here java is false so --> true) or java === false or something like that.


#14

thanks


#15

Algo muito importante de que não podemos nos esquecer:
1°) A condição a ser chamada fora do laço, deve ser verdadeira.

//Lembre-se de fazer sua condicao true fora do laco!
var bool = true;

var soloLoop = function() {
while (bool === true) {
console.log("Repetiu uma vez!");
bool = false;
}
};
soloLoop


#16

Try putting the variable (in this case "bool" ) into the soloLoop. SO, soloLoop(bool); which will give u the final result and print it in the console.

Cheers,
Siddharth