+ 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
12 odpowiedzi
+ 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
+ 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 { ... }
+ 4
That's what I do when I don't get challenges, go around posting on some posts. :P
+ 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?
+ 2
type some code down telling me how I should do that.
+ 2
that was helpful!
+ 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.
+ 1
I was going to write that, but I ran out of characters.
+ 1
You need something like, if guessedPassword = correctPassword then it's correct
+ 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 ==.
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.