4 Replies - 616 Views - Last Post: 13 February 2013 - 12:21 AM

#1 tazzy0429  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 27-April 12

switch statements and alert msg not working the way i expected

Posted 11 February 2013 - 04:46 PM

I am programming this simple program and I have provided what it needs to do. First issue is that I am not getting any alert boxes to pop up and the second is i am not sure what to use to get the you lose statement. With if else statement that is easier. Could someone point me in the direction i need to go. You guys have been so helpful when i need a push to understand where i am messing up my code. I looked up switch statements but i dont think i am fully comprehending the basics of it. Thanks in advance.



•Displays a heading that says "Please enter a door and I will give you your prize! Enter 1, 2 or 3."

•User enters a door number in a text box

•When they click a button that says "Show Prize", a Javascript function alerts them what they have won in a pop up box.
•Use a switch statement to alert them what they've won. It's up to you what their prize is! Example: "Congratulations! You have won a new car!"
•If the person did not enter 1, 2, or 3, alert "You lose because you can't follow directions!"

.




<script type="text/javascript">
function calculatePrize()
{var number = parseInt(document.getElementById('door').value);
switch (number) {
   case '1': msg = "You won $1000.00"; break;
   case '2': msg = "You won a vacation"; break;
   case '3': msg = "You won a basket of spa products"; break;
}}

document.getElementById('door').value = '  Your prize is: ';  
alert(msg);
</script>
</head>

<body>
<p>"Please enter a door and I will give you your prize!  Enter 1, 2 or 3."


<br />
<input type='text' id='door' /> <br />

<input type="button" id="prize" 
value="Display Prize" onclick='calculateSPrize()'/></p> 



Is This A Good Question/Topic? 0
  • +

Replies To: switch statements and alert msg not working the way i expected

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3619
  • View blog
  • Posts: 12,481
  • Joined: 12-December 12

Re: switch statements and alert msg not working the way i expected

Posted 11 February 2013 - 04:54 PM

Spelling mistake: calculateSPrize()
Was This Post Helpful? 3
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4361
  • View blog
  • Posts: 12,180
  • Joined: 18-April 07

Re: switch statements and alert msg not working the way i expected

Posted 11 February 2013 - 04:57 PM

Your alert statement is outside of the function where you set msg. I think you want it inside the function. On top of that, you want to define it before you go into the switch because if none of the cases work in the switch, it still has something to alert() with. Lastly, you are using parseInt() so the result is going to be an actual number. Don't make your cases as strings (using the single quotes). Use the number directly. Check out the function below...


function calculatePrize()
{
   var number = parseInt(document.getElementById('door').value);
   var msg = "Invalid door";

   switch (number) {
      case 1: msg = "You won $1000.00"; break;
      case 2: msg = "You won a vacation"; break;
      case 3: msg = "You won a basket of spa products"; break;
   }

   // Message alert goes here
   alert(msg);
}



Play with this and I am sure you will get the idea what was wrong. :)

Edit: And of course Andrew is correct about the misspelling.

This post has been edited by Martyr2: 11 February 2013 - 04:59 PM

Was This Post Helpful? 2
  • +
  • -

#4 tazzy0429  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 27-April 12

Re: switch statements and alert msg not working the way i expected

Posted 11 February 2013 - 05:48 PM

Thanks you both for the replies. it always to be something simple that gets me in trouble and then i get lost. I got it working correctly now thanks to you both.
Was This Post Helpful? 0
  • +
  • -

#5 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: switch statements and alert msg not working the way i expected

Posted 13 February 2013 - 12:21 AM

Well, I know you have resolved the problem, nevertheless, here are my 2 cents.

switch (number)
{
   case '1': msg = "You won $1000.00"; break;
   case '2': msg = "You won a vacation"; break;
   case '3': msg = "You won a basket of spa products"; break;
  
}


The above switch case would certainly work if you declare number like this.

var number = document.getElementById('door').value;


In that way, the number will remain as a string and consequently, when the appropriate case is chosen, the alert box will be displayed.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1