2 posts were split to a new topic: Old lesson checkpoints
Hello there!
When doing the exercise on “If Statements,” I made a mistake at step 2 and typed if (true) initially, rather than if (sale). ‘Time to buy!’ appeared in the console, which didn’t surprise me as let sale = true.
After changing to sale = false, if (false) yields nothing in the console, of course. But if I type, if (true), ‘Time to buy!’ does appear in the console, even though the variable has been changed to false.
Please can someone explain why this is? I would think that if sale = false, if (true) would not yield anything in the console because there isn’t a variable that is true. (Just experimenting to see what happens off the back of my initial mistake!)
EDIT: I asked my friend and they said I created an “infinite loop” by not defining what it is that’s true. So I understand my mistake now on a simple level, but would still appreciate more detailed feedback if anyone wants to give it
if (true) {
}
is an absolute condition so there is no way to change its state. Such a loop requires a breaking condition within to code body…
if (true) {
// some code creates a state
if (state === null) {
break
}
}
null
in the above is simply a placeholder for example.
It is late today and I am bit wrecked so thats probably why I read “a” as “o” in the code but having two different fonts for code and description is not helping.
I wondered why the code needs to be separately into different lines? Cant that 1 code exists on a single line?
I originally typed the code in 1 line and the result shows what is expected but didnt pass the exercise. Then i looked at the answer and is because we need to separate the code into 3 different lines.
I am probably making some sort of obvious error here but I cannot figure this out.
I am creating the sale variable and setting it to true.
This works.
Then I am setting the sale variable to false.
This also appears to work.
Finally, I am telling the program to print a message if sale is true.
And, the message prints.
If I look to see the value of the variable, it now says that it is true again…
Why? Why is the variable still seen as true, when I had set it to false most recently?
Screenshot included, and my code,
let sale = true;
console.log('first test' + '=' + sale);
sale = false;
console.log('second test' + '=' + sale);
if (sale = true) {
console.log('Time to buy!');
};
console.log('third test' + '=' + sale);
The condition given above is an assignment, not a comparison. The assignment does take place so the variable is now true
. Because the condition didn’t ‘fire’ it was treated as a statement, followed by an anonymous code block that also executed.
if (sale === true)
Note the comparison.
The condition given above is an assignment, not a comparison.
Ahhhhh, so the fact that I had set it to false, then did
if (sale = true)
had set it again to true!
Thanks so much! You are always so helpful!
Hello,
Can you help me. I can`t figure out what is error in my code, but platform get the error message.
let sale = true;
sale = false;
if (sale) {console.log(Time to buy!
)};
Thanks in advance.
There doesn’t appear to be a syntax error. What does the error message say?
when you create a condition
if (true) {
console.log(‘Time to buy!’);
},
what you are saying is that the code should run as long as the value (true) is true.
That means you can even delete the variable sale and the code will run because “true” is not a variable, it is a value and “true” will always be “true”
Rem: ‘sale’ is a variable holding a value called ‘true’ meaning sale depends on true.
But true is a value on its own and can function on its own
let sale = true;
if (sale) {
console.log(‘Time to buy!’);
}