I couldn’t find out what I was doing wrong, then I saw that the last statement had an exclamation point and I had put a period. Realistically, if I had made that mistake, I don’t think it would’ve prevented me from going forward. I think that’s a pain in the ■■■ to have to deal with. If it’s in the ’ ’ then you can put whatever you want, isn’t that correct??
A lot of mistakes are made in code where developers simply miss details. This causes code to be commented and sent back for review. So while I agree with you that it’s a pain in the ■■■, it teaches you to be attentive to the little details that make a difference in the end.
Hi! I’m having an issue where even though all the syntax is apparently correct and the code runs, it ignores? the > operator and always prints the else condition. Using the >= operator does print the if.
I’d suggest reading the docs on comparison operators. I’ll add a few comments to your code to explain a little about what is happening:
let test = 4; //assigns 4 to the variable test
//test =< 5; //SyntaxError **there is no =< operator, hence the syntax error
//test => 5; //no error **this line is meaningless. Technically it's a function, but it doesn't do anything.
//The => is referred to a a fat arrow, and has use in arrow functions.
if (test => 6){ //here you have if evaluating whether the value inside the parenthesis is truthy. Since the value is a function technically, it has a bool value of true, and the code inside the if block executes
console.log(test); //prints 4 as you've noted because test still points to that value
}
// prints 4
if (test = 7){ //here you are assigning the value 7 to the variable test, and then if is testing whether the result is true. It is (any numerical value other than 0 is truthy), so the code inside the if block executes.
console.log(test); //prints 7 as you've noted since you just assigned 7 to test
}
//prints 7
If you’re curious about which values are truthy or falsy, you can use the Boolean() function to check for yourself. Here are a few examples:
Hello,
I’m really not sure there’s hope for me, just a newbie. I get the basic concept of the IF, Else statement but guess my question is in the area of how to actually write the code? Not even sure how to explain my question…
Here, I’ve changed the comparison operator to < 7 instead of > 7 and the output is ‘We can eat later.’ But it’s confusing me because I feel like I should change the code to this:
let hungerLevel = 7;
hungerLevel = 10;
if(hungerLevel < 7) {
console.log('We can eat later!');
}
else {
console.log('Time to eat');
}
Instead of:
let hungerLevel = 7;
hungerLevel = 10;
if(hungerLevel < 7) {
console.log('Time to eat!');
}
else {
console.log('We can eat later!');
}
Hope this makes sense. If not, maybe we can work through it eventually…Thank you for your time…
You set up a condition for a certain situation and then depending on the answer that comes in from the user, depends on what outputs…?
So in this case, there would be no need to change the > to <. It would stay as it is.
I was just experimenting with changing the comparison operators around as they said to do after the exercise was done, but in doing so think I confused myself!
This might be a silly question, but what exactly tells the computer that one is greater than zero, or 8 > 7, such that it can evaluate that statement/condition/argument to be true?