Problem with a switch case

This is a discussion on "Problem with a switch case" within the JavaScript Forum section. This forum, and the thread "Problem with a switch case are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Aug 17th, 2005, 19:39
New Member
Join Date: Aug 2005
Location: California
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Problem with a switch case

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
Reply With Quote

  #2 (permalink)  
Old Aug 18th, 2005, 06:55
Most Reputable Member
Join Date: Jul 2003
Posts: 1,856
Thanks: 0
Thanked 0 Times in 0 Posts
choice = (Math.random() *5);

This won't give you integers, which is what you're testing for in your switch - it gives you things like 1.2357972257

You need to round it, like you did in this line
value1 = Math.floor( 1 + Math.random() * 9 );
Reply With Quote
  #3 (permalink)  
Old Sep 5th, 2005, 07:04
New Member
Join Date: Aug 2005
Location: USA
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to ncrider88
Quote:
Posted: Thu Aug 18, 2005 7:55 am Post subject:

--------------------------------------------------------------------------------

choice = (Math.random() *5);

This won't give you integers, which is what you're testing for in your switch - it gives you things like 1.2357972257

You need to round it, like you did in this line
value1 = Math.floor( 1 + Math.random() * 9 );
He is right, try this link if you need some refreshing...
http://www.w3schools.com/js/js_switch.asp
Reply With Quote
Reply

Tags
problem, switch, case

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Switch(score) warren1978 JavaScript Forum 3 Mar 24th, 2008 07:09
case : 'example' help jahphill PHP Forum 4 Nov 29th, 2007 19:43
Switch to linux? alexgeek Webforumz Cafe 7 Oct 10th, 2007 21:00
switch post alexgeek PHP Forum 12 Sep 25th, 2007 10:04
CSS style switch Heavensword Web Page Design 3 Jul 24th, 2007 15:24


All times are GMT. The time now is 04:24.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43