If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

Question regarding Switch

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?

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 == "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;

Users who have thanked Philip M for this post:

<!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>

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'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.

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

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.