Different ways of reading this problem

Hello,

I’m a newbie to Javascript and this question popped up in class. Seems relatively straightforward but I struggle conceptually with where to begin a lot of the time so I’d be interested in different ways of writing the below problem for JS.

Practice if statements

  1. Record what a user would like to eat in a variable
  2. Check if they like pizza, and if they do then respond with “yay pizza!!”
  3. Otherwise check if they like pasta, and if they do then respond with “ooh I love pasta!!”
  4. If they don’t like either one, respond with “brussel sprouts for you!”

I currently have this:

const userLikesPizza = true
const userLikesPasta = true

if (userLikesPizza) {
    console.log("yay pizza!");
}   else {
    console.log("brussel sprouts for you!");
}
if (userLikesPasta) {
    console.log("ooh I love pasta!");
}   else {
    console.log("brussel sprouts for you!");
}

However I’d be keen to see if used with the Switch option or even with a let condition posing a question, i.e. “What do you like to eat”

I just want to explore different ways of writing JS
Thank you!

I believe your if-else statement may need to look more like this:

if (userLikesPizza) {
return 'yay pizza!!'
} else if (userLikesPasta) {
return 'ooh I love pasta!'
} else {
return 'brussel sprouts for you!'
}

Personally, I think using an if statement for this would be better than using a switch statement.

but if your user likes Pasta and not Pizza they get brussel sprouts anyway. Instead it should be only for if they don’t like either one


a switch statement is not the best, because you have two different variables to check. It can be made to work but in an ugly way, so I would not use this method

consider instead using an if/else if/else chain, where each one of those is one of your steps 2-3-4


If you wanted to ask what the user likes to it you can ask the user for input, so that they can answer that and the code execute. But you would also need more than a console for that, like an actual web page


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Thanks Michael. That’s what I originally had but my concern was this:

does the if statement account for the fact that the user likes both pasta and pizza, i.e. am I better phrasing this as a question in html so the user can respond and then if the conditional is not met for either:

///console.log("brussel sprouts for you!"); //appears

Thanks

Thanks for your help ilenia. Still learning the basics!

//If you wanted to ask what the user likes to it you can ask the user for input, so that they can answer that and the code execute. But you would also need more than a console for that, like an actual web page//

We are writing the code in VS Code with an index.html page. If you have any links for how to write that code or more examples fo that so I can get my head around that it would be great, similar to :

prompt("Do you like pasta?");

Thanks

The if statement does not account for the fact if the user likes both pasta and pizza, but I don’t think you need to do that. The way I read the question is that the user would only be asked about pasta if they do not like pizza. As far as how to write that page, I would take a look at this site’s Responsive Web Design curriculum and go through the HTML & HTML 5 part. It may help you understand it better.

You can use input elements, and the DOM API to have the html page get the input from the user, and then display the output

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.