+ 2

How do I make something that determines actions in JavaScript?

I would not call myself a web developer, but I have a problem in my site that I'm trying to make a little game where you try to guess the password. It's supposed to say that you're right when you guess it right, and it should say you got it wrong if you guessed it wrong. Here's what I tried. <script> function guess() { prompt("what's the password?"); var password = "pineapple" if (password = "pineapple") { alert("you got it right!"); } if (password != "pineapple") { alert("wrong."); } } https://code.solole

29th Aug 2017, 10:45 PM
Parsec
Parsec - avatar
12 Réponses
+ 6
As Nomeh said, if-else if-else loops are useful. Your case assumes two OPPOSITES happen. What if you have to write a code that gives you your grade? If we check from A+ to F using just ifs, we would get multiple answers. If we use if-else if-else loops, we would save hassle and get intended output
29th Aug 2017, 11:19 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 5
Here's a code that might help you! function guess() { var usersInput = prompt("what's the password?"); var password = "pineapple" if (usersInput == "pineapple") { alert("you got it right!"); } if (usersInput != "pineapple") { alert("wrong."); } } window.onload = function() { guess(); } ...you can as well replace the last if statement with else { ... }
29th Aug 2017, 11:16 PM
Nomeh Uchenna Gabriel
Nomeh Uchenna Gabriel - avatar
+ 4
That's what I do when I don't get challenges, go around posting on some posts. :P
29th Aug 2017, 11:32 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 2
In the if statement you need to extract the value of, for example, an input element that you can compare to the var password. You're essentially saying here, if password = password (which it always will) then alert "you got it right". You see what you need to do to correct this?
29th Aug 2017, 11:00 PM
James Cooke
+ 2
type some code down telling me how I should do that.
29th Aug 2017, 11:07 PM
Parsec
Parsec - avatar
+ 2
that was helpful!
29th Aug 2017, 11:28 PM
Parsec
Parsec - avatar
+ 1
You need to put a call to the function somewhere. It can either be the click action from a button on an html page or put guess() at the end of your script block.
29th Aug 2017, 10:58 PM
Kevin Gilkey-Graham
Kevin Gilkey-Graham - avatar
+ 1
I was going to write that, but I ran out of characters.
29th Aug 2017, 10:59 PM
Parsec
Parsec - avatar
+ 1
You need something like, if guessedPassword = correctPassword then it's correct
29th Aug 2017, 11:02 PM
James Cooke
+ 1
so on your current playground you have if guessed Password = correctPassword. You should have the prompt be returned to a variable and then compare that variable to something. The other thing I noticed is you have a few single = where you are trying to do comparisons. Comparisons are done with doubles ==.
29th Aug 2017, 11:06 PM
Kevin Gilkey-Graham
Kevin Gilkey-Graham - avatar
0
do you have a link to your code playground? I am seeing a couple other errors but I wanted to make sure it wasn't just typos in this forum.
29th Aug 2017, 11:01 PM
Kevin Gilkey-Graham
Kevin Gilkey-Graham - avatar