...

View Full Version : Question regarding Switch



tbmtbm
10-08-2012, 11:07 PM
I have a program I am trying to do where the user is prompted to enter either "Planets" or "Stars". Depending on their response, it then asks them to type in a number (1-5 or 1-8), and then outputs basic information.

I'm experimenting with using switch, but am lost as to how to get the second input to work. I was wondering if someone could help me out?



Code:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Stars</title>

<script type="text/javascript">
<!--
function userInput() {
var reply = prompt("Do you want information on Stars(S) or Planets(P)?", "");

if(reply == "Stars") {
prompt("Type in a number between 1 and 5, and I will tell you the name of the star. ");
}
else if(reply =="S") {
prompt("Type in a number between 1 and 5, and I will tell you the name of the star.");
}
else if (reply == "Planets"){

prompt("Type in a number between 1 and 8, and I will tell you the name of the planet. ");
}
else if (reply == "P"){
prompt("Type in a number between 1 and 8, and I will tell you the name of the planet.");

}


function num() {

var value;

switch (reply)
{


case ((reply == "Stars") && (value == "1")):
alert("The name of the star that is 1st closest to the Earth is Proxima Centauri");
break;

case ((reply == "Stars") && (value == "2")):
alert("The name of the star that is 2nd closest to the Earth is Alpha Centauri A");
break;

case ((reply == "Stars") && (value == "3")):
alert("The name of the star that is 1st closest to the Earth Alpha Centauri B");
break;

case ((reply == "Stars") && (value == "4")):
alert("The name of the star that is 1st closest to the Earth is Barnard's Star");
break;

case ((reply == "Stars") && (value == "5")):
alert("The name of the star that is 1st closest to the Earth is Wolf 359");
break;




case ((reply = "S") && (value == "1")):
alert( "The name of the star that is 1st closest to the Earth is Proxima Centauri");
break;

case ((reply == "S") && (value == "2")):
alert("The name of the star that is 2nd closest to the Earth is Alpha Centauri A) ");
break;

case ((reply == "S") && (value == "3")):
alert("The name of the star that is 1st closest to the Earth Alpha Centauri B");
break;

case ((reply == "S") && (value == "4")):
alert("The name of the star that is 1st closest to the Earth is Barnard's Star");
break;

case ((reply == "Planets") && (value <= "1")):
alert("The name of the Planet that is 1st closest to the Sun is Mercury");
break;
case ((reply == "Planets") && (value == "2")):
alert("The name of the Planet that is 2nd closest to the Sun is Venus");
break;

case ((reply == "Planets") && (value == "3")):
alert("The name of the Planet that is 3rd closest to the Sun is Earth");
break;

case ((reply == "Planets") && (value == "4")):
alert("The name of the Planet that is 4th closest to the Sun is Mars");
break;

case ((reply == "Planets") && (value == "5")):
alert("The name of the Planet that is 5th closest to the Sun is Jupiter");
break;

case ((reply = "Planets") && (value == "6")):
alert("The name of the Planet that is 6th closest to the Sun is Saturn");
break;

case ((reply == "Planets") && (value == "7")):
alert("The name of the Planet that is 7th closest to the Sun is Uranus ");
break;


case ((reply == "P") && (value == "1")):
alert("The name of the Planet that is 1st closest to the Sun is Mercury");
break;
case ((reply == "P") && (value == "2")):
alert("The name of the Planet that is 2nd closest to the Sun is Venus");
break;

case ((reply == "P") && (value == "3")):
alert("The name of the Planet that is 3rd closest to the Sun is Earth");
break;

case ((reply == "P") && (value == "4")):
alert("The name of the Planet that is 4th closest to the Sun is Mars");
break;

case ((reply == "P") && (value == "5")):
alert("The name of the Planet that is 5th closest to the Sun is Jupiter");
break;

case ((reply = "P") && (value == "6")):
alert("The name of the Planet that is 6th closest to the Sun is Saturn");
break;

case ((reply == "P") && (value == "7")):
alert("The name of the Planet that is 7th closest to the Sun is Uranus");
break;

case ((reply == "P") && (value == "8")):
alert("The name of the Planet that is 8th closest to the Earth is Neptune");
break;



}


}

}
//-->
</script>



</head>




<input type="button" onclick="userInput()" value="Click Here to Begin!">



<body>
<style type="text/css">
body {background-image:url("https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQ-JIO0711Ym0A3talo5fEWy1AZpZ_27094CDp1zxXuIHls_Sa_");}
background-attachment:fixed;
background-position:center;
background-size:80px 60px;
background-repeat:no-repeat;
</style>
</body>
</html>

Old Pedant
10-08-2012, 11:36 PM
You have more problems than completely misusing switch.

You never GET a value for the number of stars/planets. You issue a prompt, but you ignore the user's response.

And your HTML is completely invalid.

You have your <style> in the <body> and your <input> in the <head>. Utterly backwards.

Philip M
10-09-2012, 09:07 AM
var reply = prompt("Do you want information on Stars(S) or Planets(P)?", "");
if(reply == "Stars") {

What if the user types "stars" or "planets"?


if (/^p/i.test(reply) { // if the first letter of reply is p (case insensitive)
alert ("Planets");
reply = "p";
}
if (/^s/i.test(reply)) { // if the first letter of reply is s (case insensitive)
alert ("Stars");
reply = "s";
}
else {
alert ("Please enter P(lants) or S(tars)");
}

prompt("Type in a number between 1 and 8, and I will tell you the name of the planet. ");

What if the user types "99" or "Mickey Mouse"?

A good proportion of any computer code consists of user input validation.


This should point you in the right direction, but still a lot to do:-


var reply = "s"; // obtained from user prompt;
var value = 3; // obtained from user prompt

if (reply == "s") {
switch(value) {
case 1: alert( "The name of the star that is 1st closest to the Earth is Proxima Centauri"); break;
case 2: alert("The name of the star that is 2nd closest to the Earth is Alpha Centauri A) "); break;
case 3: alert("The name of the star that is 3rd closest to the Earth is Alpha Centauri B"); break;
default : alert ("This is the default message");
}
}

if (reply == "p") {
switch (value)

}

shyagrawal
10-09-2012, 09:37 AM
Please use below code:


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Stars</title>

<script type="text/javascript">
var number;
var reply;
<!--
function userInput() {
reply = prompt("Do you want information on Stars(S) or Planets(P)?", "");

if(reply == "Stars" || reply =="S") {
number = prompt("Type in a number between 1 and 5, and I will tell you the name of the star. ");
}
else if (reply == "Planets" || reply == "P"){
number = prompt("Type in a number between 1 and 8, and I will tell you the name of the planet. ");
}

if(number != 0){
num();
}
}
function num() {

var value = parseInt(number);

if(reply == "Stars" || reply =="S") {
switch (value)
{
case 1:
alert("The name of the star that is 1st closest to the Earth is Proxima Centauri");
break;
case 2:
alert("The name of the star that is 2nd closest to the Earth is Alpha Centauri A");
break;

case 3:
alert("The name of the star that is 1st closest to the Earth Alpha Centauri B");
break;

case 4:
alert("The name of the star that is 1st closest to the Earth is Barnard's Star");
break;

case 5:
alert("The name of the star that is 1st closest to the Earth is Wolf 359");
break;
}
}
else if(reply == "Planets" || reply =="P") {
switch (number)
{
case 1:
alert("The name of the Planet that is 1st closest to the Sun is Mercury");
break;
case 2:
alert("The name of the Planet that is 2nd closest to the Sun is Venus");
break;

case 3:
alert("The name of the Planet that is 3rd closest to the Sun is Earth");
break;

case 4:
alert("The name of the Planet that is 4th closest to the Sun is Mars");
break;

case 5:
alert("The name of the Planet that is 5th closest to the Sun is Jupiter");
break;

case 6:
alert("The name of the Planet that is 6th closest to the Sun is Saturn");
break;

case 7:
alert("The name of the Planet that is 7th closest to the Sun is Uranus ");
break;
}
}
}


//-->
</script>

<style type="text/css">
body {background-image:url("https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQ-JIO0711Ym0A3talo5fEWy1AZpZ_27094CDp1zxXuIHls_Sa_");}
background-attachment:fixed;
background-position:center;
background-size:80px 60px;
background-repeat:no-repeat;
</style>

</head>
<body>
<input type="button" onclick="userInput()" value="Click Here to Begin!">

</body>
</html>

Philip M
10-09-2012, 09:55 AM
As I have said, what if the user types "stars" or "planets"?

var value = parseInt(number);
If you use parseInt() you ought to specify the radix. But better to use Number()

tbmtbm
10-09-2012, 12:43 PM
I'm a complete beginner, and I was experimenting with different things. I knew my code so far was missing a lot, but I was adding things in as I read about them, hoping I would be able to tweak it as I knew more.

If the user puts in "stars" or "planets", I am going to put in error messages, and if they put in an incorrect number, I have to pick a number for them.

I appreciate the the responses from the people who want to help.

Philip M
10-09-2012, 01:10 PM
I'm a complete beginner, and I was experimenting with different things. I knew my code so far was missing a lot, but I was adding things in as I read about them, hoping I would be able to tweak it as I knew more.

If the user puts in "stars" or "planets", I am going to put in error messages, and if they put in an incorrect number, I have to pick a number for them.

I appreciate the the responses from the people who want to help.

It is for more user-friendly to test for the first letter of the reposnse "s" or "p" (case insensitive) rather than reject the input, display an error message, and require the answer to be entered again.

tbmtbm
10-09-2012, 01:30 PM
It is for more user-friendly to test for the first letter of the reposnse "s" or "p" (case insensitive) rather than reject the input, display an error message, and require the answer to be entered again.

I thought that too, but my professor specifically wanted an error message. Probably to teach us error handling.

The code that shya did works for the first switch, but not the second. If I put in "Stars", or "S", and then a number, the correct answer comes up.

If I put "Planets" or "P", then a number, however, nothing comes up. I can't figure out why.

Philip M
10-09-2012, 01:50 PM
Here's a clue:-

switch (number)

You should get rid of the ugly "undefined" in your prompt boxes.

I know you are just learning, but be aware that both prompt(() and alert() are obsolete, and these days used only for debugging purposes.

tbmtbm
10-09-2012, 02:19 PM
Here's a clue:-

switch (number)

You should get rid of the ugly "undefined" in your prompt boxes.

I know you are just learning, but be aware that both prompt(() and alert() are obsolete, and these days used only for debugging purposes.

O.K. Thanks.

Weird about those things being obsolete, our textbook uses them a lot. We have talked about how annoying alert boxes are, but I wanted to focus on learning functions and the switch statement.

Philip M
10-09-2012, 02:38 PM
Weird about those things being obsolete, our textbook uses them a lot.

Yes, most Javascript textbooks seem to be fixed in the last century.

tbmtbm
10-09-2012, 02:44 PM
I'm trying to get the error handling down. I've added the following statement:

if(reply != "Stars" || reply != "S" || reply != "Planets" || "P" ||); {
number = prompt(reply + " is improper input!!- run the program again.")

But this makes it so an error message comes up when the user types in the number for the star or planet. I just saw something called try/catch/finally. Is that the correct way to handle invalid input?

Philip M
10-09-2012, 04:33 PM
if(reply != "Stars" || reply != "S" || reply != "Planets" || "P" || ); {

See the error here?

Try/catch is used to trap runtime errors. For example, function mis-spelled fucntion. Not a validation tool.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum