The Student Room Group

Quick JavaScript Problem.

how do i do if value 1,2 and 3 all equal to specific answer and then that if statement should work...



function checkanagram(){
var val = $("#anagramtext":wink:.val();
var val2 = $("#anagramtext2":wink:.val();
var val3 = $("#anagramtext3":wink:.val();

if (val == 'Millennium Wheel':wink: {

alert ('Correct Answer!':wink:; }

else { alert ('Incorrect answer, try again':wink:; }

}




i want it to say if value1 ,2 and 3 all equal to each specific string then it should do the if statement
(edited 6 years ago)

Scroll to see replies

any ideas @UWS
Reply 3
Just put all conditions into your if statement one after the other?
Reply 4
Use the && operator to represent AND


if (val == 'Millennium Wheel' && val2 == 'Millennium Wheel' && val3 == 'Millennium Wheel')

{...}
Original post by UWS
Use the && operator to represent AND



if (val == 'Millennium Wheel' && val2 == 'Millennium Wheel' && val3 == 'Millennium Wheel':wink:

{...}




i did that it didnt work
Reply 6
Original post by study beats
i did that it didnt work


You've done something else wrong then. Paste your code into a JSFiddle.
Reply 7
Original post by study beats
i did that it didnt work


Not sure then, some of that is jQuery which I've never used.

Have you considered asking on StackOverflow too?
https://stackoverflow.com/
Dont you wanna use "or" to check if one of those is the correct or do they all have to be correct...



if (val == 'Millennium Wheel' || val2 == 'Millennium Wheel' || val3 == 'Millennium Wheel':wink:
Original post by John10101
Dont you wanna use "or" to check if one of those is the correct or do they all have to be correct...



if (val == 'Millennium Wheel' || val2 == 'Millennium Wheel' || val3 == 'Millennium Wheel':wink:


all have to be correct
Original post by IWMTom
You've done something else wrong then. Paste your code into a JSFiddle.


how do i post into js fiddle ? can anyone do it?
Original post by UWS
Not sure then, some of that is jQuery which I've never used.

Have you considered asking on StackOverflow too?
https://stackoverflow.com/


i have tried it again. now only val 3 isnt working
Reply 12
Original post by study beats
how do i post into js fiddle ? can anyone do it?


Yes... Go to the website, pop you JavaScript into the JS box, and your HTML in the HTML box.

Make the smallest possible working example for your scenario.
Original post by study beats
how do i post into js fiddle ? can anyone do it?


https://jsfiddle.net/ go there put all your code there and save it and send link.
Reply 14
Original post by study beats
i have tried it again. now only val 3 isnt working


It should work if the first two work, maybe you're missing an apostrophe or the &&?

Original post by study beats
how do i post into js fiddle ? can anyone do it?


https://jsfiddle.net/
Original post by IWMTom
Yes... Go to the website, pop you JavaScript into the JS box, and your HTML in the HTML box.

Make the smallest possible working example for your scenario.


https://jsfiddle.net/a8xykcj9/4/

Original post by John10101
https://jsfiddle.net/ go there put all your code there and save it and send link.

https://jsfiddle.net/a8xykcj9/4/

Original post by UWS
It should work if the first two work, maybe you're missing an apostrophe or the &&?



https://jsfiddle.net/

https://jsfiddle.net/a8xykcj9/4/
Reply 16
Original post by study beats
*****


Here's a working POC: https://jsfiddle.net/wuxtubnw/
Original post by IWMTom
Here's a working POC: https://jsfiddle.net/wuxtubnw/


still not working, is it because i am using spaces?
Reply 18
Original post by study beats
still not working, is it because i am using spaces?


What's showing in your error log?
Original post by IWMTom
What's showing in your error log?


basically for value 3, when i choose it to equal to 'Tower of London' then it doesnt work and when i type it into the field box the dialog box shows up as incorrect answer even though i have spelt it properly.

also i have tried other words and they work

Quick Reply

Latest

Trending

Trending