Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    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>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,536
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    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.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,145
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    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) 
    
    }
    Last edited by Philip M; 10-09-2012 at 08:24 AM.

    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.

  • Users who have thanked Philip M for this post:

    tbmtbm (10-09-2012)

  • #4
    New Coder
    Join Date
    Sep 2012
    Posts
    22
    Thanks
    0
    Thanked 6 Times in 6 Posts
    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>

  • Users who have thanked shyagrawal for this post:

    tbmtbm (10-09-2012)

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,145
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    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.

  • #6
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,145
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    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.

  • #8
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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.

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,145
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    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.
    Last edited by Philip M; 10-09-2012 at 12:53 PM.

    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.

  • Users who have thanked Philip M for this post:

    tbmtbm (10-09-2012)

  • #10
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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.

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,145
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    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.

  • #12
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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?

  • #13
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,145
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    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.
    Last edited by Philip M; 10-09-2012 at 03:36 PM.

    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •