Hi Everyone,
I need some help with a switch case. No matter what I do, it keeps giving me the default answer for no. Here is the code...
<
html>
<head>
<title>Project 3</title>
<script type = "text/javascript">
<!--
var value1, value2, answer, studentanswer, figure, choice;
value1 = Math.floor( 1 + Math.random() * 9 );
value2 = Math.floor( 1 + Math.random() * 9 );
document.writeln( "How much is " + value1 + " times " + value2 + "?
");
//gets number from text field on form
function getAnswer()
{
var studentanswer = parseFloat (document.myform.studentanswer.value);
answer = value1 * value2;
figure = calculation (studentanswer);
}
//defines the function called calculation
function calculation (studentanswer)
{
if (studentanswer == answer)
{
choice = (Math.random() *5);
switch (choice)
{
case 0:
window.status = "Very Good!";
break;
case 1:
window.status = "Excellent!";
break;
case 2:
window.status = "Nice Work!";
break;
case 3:
window.status = "Keep up the good work!";
break;
default:
window.status = "Yes, Something is not right";
}
}
else
window.status = "No, please try again.";
choice = (Math.random() *5);
switch (choice)
{
case 0:
window.status = "No, please try again";
break;
case 1:
window.status = "Wrong. Try once more";
break;
case 2:
window.status = "Don't give up!";
break;
case 3:
window.status = "No, keep trying";
break;
default:
window.status = "No, Something is not right";
}
}
// -->
</script>
</head>
<body>
<form name = "myform" action = "">
Enter the answer here
<input name = "studentanswer" type = "text" />
<input type = "button" value = "Calculate" onclick = "getAnswer()" />
</form>
</body>
</
html>
Any help would be greatly appreciated!
Thanks!
Jennifer