It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

alert, prompt, and confirm, if/else statements

alert, prompt, and confirm, if/else statements

Postby babii_kaikz on Fri Oct 19, 2007 3:15 pm

consider this general question:

:arrow: 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 :D
babii_kaikz
 
Posts: 0
Joined: Fri Oct 19, 2007 2:55 pm

Postby sachav on Fri Nov 09, 2007 5:21 pm

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>
sachav
 
Posts: 32
Joined: Sat Mar 03, 2007 3:40 pm


Who is online

Users browsing this forum: No registered users and 8 guests