Want to do if/else statement with function


this is for 4/13

i want to make a function that prompts the user to type in 'tacos' and then have the console print out "I want to eat tacos"

I understand this is not exactly what they are asking, I just want to see if its possible.

here's what I wrote:

var foodDemand= function(food){
prompt("what food do you want to eat?")
if (prompt= "tacos");{
console.log("I want to eat"+ " " + food);
console.log("I don't want that kinda food");

and the error I am getting is SyntaxError: Unexpected token else

so i am guessing it doesn't like my syntax in my if/ else statement?
Also, another thing I think I got wrong (and need help figuring out how to do) is to go from prompting the user to type in what kind of food they want, and then based on that response, have the console print out bla bla



In your prompt thing, you have to do a variable that equals it. For example, like this:

var tacos = prompt("what food do you want to eat?")
if(tacos = "tacos"){

Change those two, and I think it will work.


ok so in an if/ else statement you have to have a variable defined for the computer to look at and decide if the info provided agrees with or doesn't agree with that variable.

i'm trying to remember if that is the same may we did it for the Justin Bieber game, that had a bunch of if/else statements as well as prompts for the user.

even though I'm on the next section now, is there a way to go back to previous lessons and check how we did things before?

since we are repeating a lot on info, they should have it set up so that you can look back at your previous work and build off that: i don't like how its segmented into each lesson


Yeah, you can. You can go to your home page, and look under the "Continue" bar and see the word "JavaScript". You can click that and browse at everything you did in Javascript.


The idea behind if and else is pretty simple you want to make the execution of code being optional. For example the stuff after if ({…}) is only executed if what ever happens to be in the () after if has a value of true. As true and false would be boring (true executes it always, false never) you mostly stick to the use of a variable that is compared to a value, which might be masked in a variable as well or which might be just a value.

It seems like you already tried this in your code here:

if (prompt= "tacos");{

unfortunately this doesn’t compare the prompted value to “tacos” but reassigns the prompt function with “tacos”.
@ragezapper already gave you a hint in the right direction but better use == or === to actually compare them as the = operator means assignment and is therefor not suitable for condition. Unlike you’re really knowing what you want to do.
Last but not least no semicolons after conditions, here is a longer explanation what happens when you use a semicolon after a condition:

PS: It seems that if/else aren’t even part of the exercise. Is there a specific reason you use them? If you want to experiment above the exercise it’s probably better to run your code @ e.g. labs repl.it because the regular exercises have test function that might interfere with going beyond the exercise. But it is great if you do so rather choose a place without test function :slight_smile:

PPS: As you’ve overwritten prompt in your code it might be necessary to refresh the page to reset prompt to its orignial value.