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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Split function with switch case help?

    I have the following that I need to finish for a school project. I have no idea on how to split the string and then it will display each one according to the letter.

    This is my code:

    Code:
    <script>
    		//This is a function that serves as the base to convert the letter to ascii.
    		function convertlettertoascii()
    		{
    			//Set up variables.
    			var userinputtext;
    			var letterMessage;
    			var displaytext;
    			
    			//Get the letter from the user and assign it to a variable.
    			userinputtext = window.prompt("Please enter a lower case letter a-z.","Enter letter here.");
    			
    			//Get the selected letter based on the user entry.
    			letterMessage = GetletterSelection(userinputtext);
    
    			//Display the result.
    			charArray = userinputtext.split("");
    				//initialize the display variable with an empty string.
    					displaytext ="";	
    				//Loop through the array, concatenating each character to the display variable with a comma.
    				for(i=0; i < charArray.length; i++)
    				{
    					displaytext = displaytext + charArray[i] + ", ";
    				}
    			
    				//Display the results
    				ShowOnScreen (displaytext);
    		}
    
    		//This function performs the letter choice.
    		function GetletterSelection(letterChoice)
    		{
    			//set up variables
    			var returnascii;
    			var convertoasciiBase = "Your letter is the following in ascii: ";
       // this is based on what the user selected and will display the results
    			switch(letterChoice)
    			{
    				case "a":
    					returnascii = convertoasciiBase + "97";
    					break;
    				case "b":
    					returnascii = convertoasciiBase + "98";
    					break;
    				case "c":
    					returnascii = convertoasciiBase + "99";
    					break;
    				case "d":
    					returnascii = convertoasciiBase + "100";
    					break;
    				case "e":
    					returnascii = convertoasciiBase + "101";
    					break;
    				case "f":
    					returnascii = convertoasciiBase + "102";
    					break;	
    				case "g":
    					returnascii = convertoasciiBase + "103";
    					break;
    				case "h":
    					returnascii = convertoasciiBase + "104";
    					break;
    				case "i":
    					returnascii = convertoasciiBase + "105";
    					break;
    				case "j":
    					returnascii = convertoasciiBase + "106";
    					break;
    				case "k":
    					returnascii = convertoasciiBase + "107";
    					break;
    				case "l":
    					returnascii = convertoasciiBase + "108";
    					break;
    				case "m":
    					returnascii = convertoasciiBase + "109";
    					break;
    				case "n":
    					returnascii = convertoasciiBase + "110";
    					break;
    				case "o":
    					returnascii = convertoasciiBase + "111";
    					break;
    				case "p":
    					returnascii = convertoasciiBase + "112";
    					break;
    				case "q":
    					returnascii = convertoasciiBase + "113";
    					break;
    				case "r":
    					returnascii = convertoasciiBase + "114";
    					break;
    				case "s":
    					returnascii = convertoasciiBase + "115";
    					break;
    				case "t":
    					returnascii = convertoasciiBase + "116";
    					break;
    				case "u":
    					returnascii = convertoasciiBase + "117";
    					break;
    				case "v":
    					returnascii = convertoasciiBase + "118";
    					break;
    				case "w":
    					returnascii = convertoasciiBase + "119";
    					break;
    				case "x":
    					returnascii = convertoasciiBase + "120";
    					break;	
    				case "y":
    					returnascii = convertoasciiBase + "121";
    					break;
    				case "z":
    					returnascii = convertoasciiBase + "122";
    					break;
    // if user did not enter a letter between a-z show this					
    				default:
    					returnascii = "You have not entered a lowercase letter a-z.";
    					break;					
    			}
    			
    			return returnascii;
    		}
    		/*
    			The following will display your 
    			results on the screen
    		*/
    		function ShowOnScreen(someText)
    		{
    			document.getElementById("DisplayResultsArea").innerHTML = someText;
    		}
    
    	</script>
    
    
    
    	<!--
    		This code creates a button that you can use to execute your JavaScript code.   
    		You can copy and paste it to create as many buttons as you like.
    		Just change the button text from "Do It" to something that describes your program.
    		Then, change the function call to whatever function you with to call.
    	-->
    	<button onclick="convertlettertoascii()">Convert letter to Ascii</button><br>
    
    	<!--
    		This code creates a display area for results. It is popoulated by the function "ShowOnScreen", above.
    	-->
    
    	<p id="DisplayResultsArea" style="font-family:courier;color:blue;margin-left:20px;">You have selected letter:</p>
    
    </body></html>

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,087
    Thanks
    38
    Thanked 498 Times in 492 Posts
    Seems like a whole lot of code for such a simple task.
    Am I missing something here?

    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> HTML5 page </title>
    
    </head>
    <body>
    
    <script type="text/javascript">
    function str2charcode(str) {
      var code = '';
      for (var i=0; i<str.length; i++) {
        code += str.charCodeAt(i)+' ';
      }
      return code;
    }
    var resp = prompt('Words','Hello World!');
    alert(str2charcode(resp));
    </script>
    </body>
    </html>

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Double-posted!

    He said in the other post that using charCodeAt is not allowed.

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,087
    Thanks
    38
    Thanked 498 Times in 492 Posts
    Quote Originally Posted by jmrker View Post
    Seems like a whole lot of code for such a simple task.
    Am I missing something here?

    [code]...
    Ahh ... Never saw the double post.
    Guess that's what I was missing!


  •  

    Posting Permissions

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