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

    I need help to split a user input.

    I need to do the following when a user types in lets say hello it will convert it to ascii characters by each letter.

    Thus us what I have but not sure what I am doing wrong.. I am lost. New to Javascript so any suggestions to improve it also. Thanks

    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 charArray;
    			
    			//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.");
    			charArray = userinputtext.split(",");
    			//Get the selected letter based on the user entry.
    			letterMessage = GetletterSelection(userinputtext);
    			
    			displayText = "";
    		
    				//Loop through the array, concatenating each character to the display variable with a comma.
    				for(i=0; i < charArray.length; i++)
    				{
    					displayText = letterMessage + charArray[i] + ", ";
    				}
    
    			//Display the result.
    			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>
    </head>
    
    <body>
    	<!--
    		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 rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,372
    Thanks
    11
    Thanked 591 Times in 572 Posts
    Code:
     alert("hello world".split("").map(function(a){return a.charCodeAt(0)}))
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #3
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rnd me View Post
    Code:
     alert("hello world".split("").map(function(a){return a.charCodeAt(0)}))
    I am not allowed to use that apparently. Instructions said I cant use Javascrip charCodeat

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,372
    Thanks
    11
    Thanked 591 Times in 572 Posts
    ok, without charCodeAt, you can use a LookUpTable:

    Code:
    "hello world".split("").map(
      Date.call.bind("".indexOf, "                                  !'#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~€‚ƒ„…†‡ˆ‰Š‹" )
    )
    in fact, i like that better as it doesn't need a custom function, allowing just fast native methods to perform the entire workload.
    Last edited by rnd me; 04-01-2014 at 07:54 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #5
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have to use the following:

    Students will create a utility that converts text to its equivalent ASCII codes. Users will be
    instructed to enter one English word. When they execute the function, they will see their text
    changed into ASCII digits separated by commas. For example, the word 'hello' will become
    104,101,108,108,111. The conversion should work for any English lower case word.

    Required structures and tools:
     Switch/Case
     Array (For this exercise, students may use the JavaScript "split" function to populate the
    array).
     Custom function
     Loops
     Error handling

    Restriction: Students may not use JavaScript conversion functions such as "CharCodeAt".

    I dont know why its not converting the letters to he code, I am missing something

  • #6
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tmccaff View Post
    I have to use the following:

    Students will create a utility that converts text to its equivalent ASCII codes. Users will be
    instructed to enter one English word. When they execute the function, they will see their text
    changed into ASCII digits separated by commas. For example, the word 'hello' will become
    104,101,108,108,111. The conversion should work for any English lower case word.

    Required structures and tools:
     Switch/Case
     Array (For this exercise, students may use the JavaScript "split" function to populate the
    array).
     Custom function
     Loops
     Error handling

    Restriction: Students may not use JavaScript conversion functions such as "CharCodeAt".

    I dont know why its not converting the letters to he code, I am missing something
    I fihured it out, I have to make a array list, dugh

  • #7
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tmccaff View Post
    I fihured it out, I have to make a array list, dugh

    I am still lost on what I need to do to be able to use the split function with switch case.

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Why did you have to start a new thread for this? Double-posting is against the forum rules.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #9
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    You'd think that there were no real world situations where loops and switch statements are required in JavaScript the way classes keep setting homework where the real world solution is nothing like what they are asking for. If this particular problem arose in the real world one of the two solutions proposed by rnd me would be used to solve it.

    There are plenty of situations where switch statements are useful in JavaScript and even a small number where loops are useful so why can't these classes set homework that actually requires these constructs to be used in producing a real world solution?

    Of course these homework questions without the stupid unrealistic limitations would make reasonable questions to ask someone studying intermediate level JavaScript where the real world solution is what would be expected as the answer - they are just inappropriate questions for beginners who have not yet learnt the code that is needed to properly solve the problem.

    It is getting more difficult to find decent questions to ask that require loops as part of the answer though now that you no longer need loops at all when processing arrays or anything that can be converted to an array.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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