Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-08-2012, 10:07 PM   PM User | #1
tbmtbm
New to the CF scene

 
Join Date: Sep 2012
Posts: 7
Thanks: 5
Thanked 0 Times in 0 Posts
tbmtbm is an unknown quantity at this point
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?



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>
tbmtbm is offline   Reply With Quote
Old 10-08-2012, 10:36 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,249
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 10-09-2012, 08:07 AM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
var reply = prompt("Do you want information on Stars(S) or Planets(P)?", "");
if(reply == "Stars") {

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

Code:
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:-
Code:
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) 

}
__________________

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.

Last edited by Philip M; 10-09-2012 at 08:24 AM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
tbmtbm (10-09-2012)
Old 10-09-2012, 08:37 AM   PM User | #4
shyagrawal
New Coder

 
Join Date: Sep 2012
Posts: 22
Thanks: 0
Thanked 6 Times in 6 Posts
shyagrawal is an unknown quantity at this point
Please use below code:
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>
shyagrawal is offline   Reply With Quote
Users who have thanked shyagrawal for this post:
tbmtbm (10-09-2012)
Old 10-09-2012, 08:55 AM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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()
__________________

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.
Philip M is offline   Reply With Quote
Old 10-09-2012, 11:43 AM   PM User | #6
tbmtbm
New to the CF scene

 
Join Date: Sep 2012
Posts: 7
Thanks: 5
Thanked 0 Times in 0 Posts
tbmtbm is an unknown quantity at this point
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.
tbmtbm is offline   Reply With Quote
Old 10-09-2012, 12:10 PM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by tbmtbm View Post
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.
Philip M is offline   Reply With Quote
Old 10-09-2012, 12:30 PM   PM User | #8
tbmtbm
New to the CF scene

 
Join Date: Sep 2012
Posts: 7
Thanks: 5
Thanked 0 Times in 0 Posts
tbmtbm is an unknown quantity at this point
Quote:
Originally Posted by Philip M View Post
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.
tbmtbm is offline   Reply With Quote
Old 10-09-2012, 12:50 PM   PM User | #9
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
__________________

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.

Last edited by Philip M; 10-09-2012 at 12:53 PM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
tbmtbm (10-09-2012)
Old 10-09-2012, 01:19 PM   PM User | #10
tbmtbm
New to the CF scene

 
Join Date: Sep 2012
Posts: 7
Thanks: 5
Thanked 0 Times in 0 Posts
tbmtbm is an unknown quantity at this point
Quote:
Originally Posted by Philip M View Post
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.
tbmtbm is offline   Reply With Quote
Old 10-09-2012, 01:38 PM   PM User | #11
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by tbmtbm View Post

Weird about those things being obsolete, our textbook uses them a lot.
Yes, most Javascript textbooks seem to be fixed in the last century.
__________________

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.
Philip M is offline   Reply With Quote
Old 10-09-2012, 01:44 PM   PM User | #12
tbmtbm
New to the CF scene

 
Join Date: Sep 2012
Posts: 7
Thanks: 5
Thanked 0 Times in 0 Posts
tbmtbm is an unknown quantity at this point
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?
tbmtbm is offline   Reply With Quote
Old 10-09-2012, 03:33 PM   PM User | #13
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
__________________

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.

Last edited by Philip M; 10-09-2012 at 03:36 PM..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 08:52 AM.


Advertisement
Log in to turn off these ads.