www.webdeveloper.com
Results 1 to 5 of 5

Thread: School project please help im stuck

  1. #1
    Join Date
    Feb 2013
    Posts
    18

    School project please help im stuck

    Convert Text to ASCII values (Part 2)

    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".

    This is what I have but not sure why it isnt giving me the values in switch case after I split them. I must be missing something.

    Code:
    <!DOCTYPE html>
    <html>
    	--><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    
    	<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("");
    				//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.
    		
    			//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(userinputtext)
    			{
    				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. #2
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    966
    I'm not sure if this was a copy/paste error or not but your switch() statement is not inside of a function at all. It's essentially just some global javascript that executes when the page loads. Thus when running your function to convert the characters it does not get called/triggered.
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  3. #3
    Join Date
    Feb 2013
    Posts
    18
    Is there a way to split the string and use switch case or no?

  4. #4
    Join Date
    Feb 2013
    Posts
    18
    here is my code:

    Code:
    <!DOCTYPE html>
    <!-- saved from url=(0079)https://s3.amazonaws.com/engradepro-myfiles/4099999667341744/lettertoascii.html -->
    <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"></head><body>--&gt;
    
    	<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>

  5. #5
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    966
    Alright, well it looks like you really just want to add your GetletterSelection() function where you loop through each character.
    Code:
    //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 + GetletterSelection(charArray[i]) + ", ";
    				}
    			
    				//Display the results
    				ShowOnScreen (displaytext);
    		}
    I also commented out a line where you use GetletterSelection() on the userinputtext variable as it didn't make sense. There you would be passing the entire string into your function, not a single character, so letterMessage would be equal to your default case ("You have not entered a lowercase letter a-z.").

    I didn't want to modify your code too much but I'll also note you are printing out "Your letter is the following in ascii: 00" for every character. Your example seemed to just list the numbers separated by a comma so you may or may not want to adjust your output to match that.

    Lastly, you also may want to make use to of the .toLowerCase() method. Applied to your userinputtext variable (userinputtext.toLowerCase()) you will automatically convert any uppercase letters to lowercase, which may or may not be outside of what is acceptable for this project.
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles