alert, prompt, and confirm, if/else statements
|
| View previous topic :: View next topic |
| Author |
Message |
babii_kaikz
Joined: 19 Oct 2007 Posts: 1
|
Posted: Fri Oct 19, 2007 4:15 pm Post subject: alert, prompt, and confirm, if/else statements |
|
|
consider this general question:
what comes after the letter c in the alphabet
answer is : d.
how can i check to see if the answer is correct or not without using a form, but using the alert,prompt, confirm methods. how would i incorporate the if/else statements
<script>
alert("what comes after the letter c in the alphabet?");
prompt("please enter the correct answer");
confirm("are you sure that is correct?");
if(...)
{
alert("that is correct");
}
else
{
alert("that is not correct");
}
</script>
what am i missing or doing incorrectly?
feedback is greatly appreciated  |
|
| Back to top |
|
 |
|
|
sachav
Joined: 03 Mar 2007 Posts: 32
|
Posted: Fri Nov 09, 2007 6:21 pm Post subject: Re: alert, prompt, and confirm, if/else statements |
|
|
You have to use a var "chars" into your code, for the client to recognize the alphabet (yes, stupid as it is, a computer does not know the alphabet!)
You also have to assign a variable to the answer.
So your code would be:
<script>
chars="abcdefghijklmnopqrstuvwxyz"
alert("what comes after the letter c in the alphabet?");
answer = prompt("please enter the correct answer","");
confirm("are you sure that is correct?");
reponseEnNombre=chars.indexOf("c",0)
reponseEnNombrePlusUn=(reponseEnNombre+(1*1))*1
if(chars.charAt(reponseEnNombrePlusUn)==answer.charAt(0))
{
alert("that is correct");
}
else
{
alert("that is not correct");
}
</script>
Or you can even make a random letter:
(a little bit harder):
<script>
chars="abcdefghijklmnopqrstuvwxyz"
random=Math.round(Math.random()*chars.length)
letterChosen=chars.charAt(random)
alert("what comes after the letter "+letterChosen+" in the alphabet?","");
answer = prompt("please enter the correct answer");
confirm("are you sure that is correct?");
reponseEnNombre=chars.indexOf(letterChosen,0)
reponseEnNombrePlusUn=(reponseEnNombre+(1*1))*1
if(chars.charAt(reponseEnNombrePlusUn)==answer.charAt(0))
{
alert("that is correct");
}
else
{
alert("that is not correct");
}
</script> |
|
| Back to top |
|
 |
|
Welcome to DEVPPL.com
You are not logged in, which means that you can't post in the forums. Click here to Register
If you are a current member here on DEVPPL, please login below:
|
|
|
|