View Single Post
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,042
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)