Hello, I was working on the Magic Eight Ball project and I stumbled into a question. I deviated a little bit from what the original question wanted me to do. The original question asked me to print the userQuestion onto the console. Before this line there is a line where you input a userName or not. What I am trying to do here is to have different statements printed onto the console depending on whether you have input your username or not.
So if you have a username: “userName has asked - userQuestion”
If not: “userQuestion”
if (userName === true) {console.log(`${userName} has asked - ${userQuestion}`)}
else {console.log(userQuestion)}
I figured an empty string like: let userName = ’ ’ would be false which is how I decided to make the line.
but the line only seems to print the else/false statement even if I have an userName input. Any help? (the line of code does seem to be working when I edit the line like so:)
if (userName === '') {console.log(userQuestion)}
else {console.log(`${userName} has asked - ${userQuestion}`)}
Why is this??? And what am I doing wrong in the first if statement?
let userName = 'Isaac'
//i forgot to all the template literals (`) these when you do a string interpolation
userName ? console.log(`Hello, ${userName}!`) : console.log('Hello!');
let userQuestion = 'Will I find the love of my life at Calvin?'
//how would this work for both options of no username and username
if (userName === true) {console.log(`${userName} has asked - ${userQuestion}`)}
else {console.log(userQuestion)}
I’ve slightly altered your code to check whether or not the userName variable has a length to it rather than if it is true or false.
let userName = 'Isaac'
//i forgot to all the template literals (`) these when you do a string interpolation
userName ? console.log(`Hello, ${userName}!`) : console.log('Hello!');
let userQuestion = 'Will I find the love of my life at Calvin?'
//how would this work for both options of no username and username
if (userName.length) {console.log(`${userName} has asked - ${userQuestion}`)}
else {console.log(userQuestion)}
If you remove the userName and just leave empty quotes as the userName it will run the else condition. You can try above.
Hello! Thank you so much for the quick response. It does seem to work. But I really am curious to ask why my own doesn’t seem to work. Could you be able to explain? Thank you!
My first assumption would be that you are checking whether or not a String value is strictly equal to a Boolean value. If you remove the === true from the conditional and just check if userName this will fix the issue you are having.
when you check to see if the value “===” you are checking strict equality, so the code is expecting your string value to be a boolean. If you use “== true”, it will check to see if your string value equates to or creates the string “true”. Thus, if you want to check if the boolean value of a string is true (meaning it has a length) you would have to check the value of the string as I have mentioned before, like so: if (userName). conditionals will inherently check if something is true or false.
So when you use if (userName === true) you are asking the code to evaluate if it is true that the string userName is
strictly equal to true, which is not true because it is a String and not a Boolean.