Jump to content

Need help with some javascript code. Just leaning

Hello I'm wondering if I can get some input on This very simple program I'm writing as part of my learning javascript.

what I'm trying to accomplish is to ask the user a question, using a prompt command.  Take the data they provide and give one of two responses.  This also might be the wrong place to ask this is so could you direct to a relevant source?

 

The problem is that after asking the user for Yes or No the desired response is not displayed

 

// I'm trying have the user enter Yes or No, this should be put into the variable answer, if im correct.
var answer = prompt("Jacob loves Pizza, do you like pizza to? Yes or No?.");

 

if (answer == Yes) {
    alert("Jacob is glad");
}
if (answer == No) {
    alert("Jacob doesn't believe you...");
}
//thanks in advance for any help!

 

 

I have included the .js file below

script.js

Link to comment
Share on other sites

Link to post
Share on other sites

Prompt returns a string, which you are storing in answer. So, you want to compare what is stored in answer to the string "Yes" or "No". You need quotation marks around the word to specify that it is a string. 

 

Link to comment
Share on other sites

Link to post
Share on other sites

if (answer == "Yes") {
    alert("Jacob is glad");
} else if (answer == "No") {
    alert("Jacob doesn't believe you...");
}

this should work, I added quotes around the answers and I made it into one if,else if construction instead of two different if constructions

 "Aeneas troianus est."

I'm allergic to social interaction in real life, don't talk to me in real life please.

don't forget to quote or tag (@marten.aap2.0) me when you reply!

Link to comment
Share on other sites

Link to post
Share on other sites

You should use 

answer.toUpperCase() === "YES"

So that it doesnt matter if you type it in caps.

PC: Case: Cooler Master CM690 II - PSU: Cooler Master G650M - RAM: Transcend 4x 8Gb DDR3 1333Mhz - MoBo: Gigabyte Z87x-D3H - CPU: i5 4670K @ 4.5Ghz - GPU: MSI GTX1060 ARMOR OC - Hard disks: 4x 500Gb Seagate enterprise in RAID 0 - SSD: Crucial M4 128Gb

Phone: Samsung Galaxy S6

Link to comment
Share on other sites

Link to post
Share on other sites

I see! Thank you so much for your help!!

Link to comment
Share on other sites

Link to post
Share on other sites

@LUUD18 Question, do I need to add the answer.toUpperCase() === "YES" to just the if statement or also to the original var answer at the top?

Link to comment
Share on other sites

Link to post
Share on other sites

17 minutes ago, HoennHero said:

@LUUD18 Question, do I need to add the answer.toUpperCase() === "YES" to just the if statement or also to the original var answer at the top?

The if statement. Don't modify the original variable unless you really have to.

Link to comment
Share on other sites

Link to post
Share on other sites

16 hours ago, HoennHero said:

@LUUD18 Question, do I need to add the answer.toUpperCase() === "YES" to just the if statement or also to the original var answer at the top?

Just like @M.Yurizaki said it's better to do it in the if statement. If you do toUpperCase() in the original var then you override the original input and you wouldn't know what letters he typed in capital. That would mean that if you want to check for capital letters in a later version you would have to rewrite that part again.

PC: Case: Cooler Master CM690 II - PSU: Cooler Master G650M - RAM: Transcend 4x 8Gb DDR3 1333Mhz - MoBo: Gigabyte Z87x-D3H - CPU: i5 4670K @ 4.5Ghz - GPU: MSI GTX1060 ARMOR OC - Hard disks: 4x 500Gb Seagate enterprise in RAID 0 - SSD: Crucial M4 128Gb

Phone: Samsung Galaxy S6

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×