www.webdeveloper.com
Results 1 to 11 of 11

Thread: How to build a sentence

  1. #1
    Join Date
    Sep 2013
    Posts
    26

    How to build a sentence

    I am trying to build a sentence by letting the user input one word at a time and displaying the words backwords into a textbox. I have to use a do/while, while, or a for loop. Which would be the best to use. Thank You. I have it to where it will prompt the user for a word and put it in a box but the next time it prompts the box has a textbow for input but the words "undefined" are above it. I am using a js page and a html page. before I say any more can someone please suggest the right loop for the problem. Thank You

  2. #2
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    Hi plat, is it possible for you to submit the code you already have? It's much easier to assist when the picture is clearer, instead of playing a guessing game of where the problem may be.
    JavaScript: Learn | Validate | Compact | bionoid

  3. #3
    Join Date
    Sep 2013
    Posts
    26
    Quote Originally Posted by bionoid View Post
    Hi plat, is it possible for you to submit the code you already have? It's much easier to assist when the picture is clearer, instead of playing a guessing game of where the problem may be.
    This is what I have so far. Just can't decide on what loop is best.

    Code:
    function reverseSentence()
    {
    var message = prompt("Enter a word for the sentence");
    var msplit = message.split(" ");
    var output = msplit.reverse().join(" ");
    
    document.reverseForm.output.value = output;
    
    
    }

  4. #4
    Join Date
    Sep 2013
    Posts
    26
    I have it to where you can enter a whole sentence and it displays backwords but I am still trying to figure out which loop I should use and how to let the user enter one word at a time. Any help would be appreciated and thank you

    function reverseSentence()
    {
    var message = prompt("Enter a word for the sentence");
    var msplit = message.split(" ");
    var output = msplit.reverse().join(" ");

    document.reverseForm.output.value = output;
    }

  5. #5
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    Sorry for the delay.

    Not that any one type of loop is exclusively correct, you could use a while loop for this:

    Code:
    var word;
    
    while (word = prompt('Enter a word for the sentence', '')) {
    
    	//DO SOMETHING WITH THE INPUT "word"
    
    }
    
    //DO FINAL PROCESSING AND DISPLAY RESULTS
    https://developer.mozilla.org/en-US/...atements/while

    I purposely didn't rewrite your code using the loop, just in case you still wanted to try if for yourself first.
    JavaScript: Learn | Validate | Compact | bionoid

  6. #6
    Join Date
    Sep 2013
    Posts
    26
    I don't know why I am having such a hard time with this. I am now trying this while loop like this but it will not do anything. Can someone look at this please and thank you. I again am trying to let the user enter one word at a time and then display the words backwords into a sentence. This is what I have in my js page.

    function sentenceReverser()
    {

    var message = prompt("Enter a word for the sentence! numeric to stop!");

    while(isNaN(message))
    {
    var msplit = message.split(" ");
    var output = msplit.reverse().join(" ");
    document.reverseForm.output.value = output;
    }
    end while;
    }

    My html page


    !doctype html>
    <html>
    <head>
    <title>Sentence Reverser</title>
    <meta charset="utf-8" />
    <script type="text/javascript" src="twist.js"></script>
    </head>
    <body>
    <form name="reverseForm" action="#">

    <label for="output">Reversed Sentence</label><br />
    <input type="text" id="output" />

    <input type="button" value="Run!" onclick="sentenceReverser()" />
    </form>
    </body>
    </html>

  7. #7
    Join Date
    Sep 2013
    Posts
    26
    when I use the console error page it keeps telling me that "sentenceReverser" is not defined" on the html page.

  8. #8
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    Umm, was thinking more like this...

    Code:
    <!DOCTYPE html>
    <html lang="en">
    	<head>
    		<meta charset="utf-8" />
    		<title>Sentence Reverser</title>
    
    		<script type="text/javascript">
    
    			function sentenceReverser()
    			{
    				var word, words = [];
    				while (word = prompt('Enter a word for the sentence, blank or Cancel to stop!', '')) {
    					words.unshift(word);
    				}
    				document.reverseForm.output.value = words.join(' ');
    			}
    
    		</script>
    
    	</head>
    	<body>
    
    		<form name="reverseForm" action="#">
    			<label for="output">Reversed Sentence</label><br />
    			<input type="text" id="output" />
    			<input type="button" value="Run!" onclick="sentenceReverser();" />
    		</form>
    
    	</body>
    </html>
    Whatever you input into the prompt box, the final result will be displayed in reverse order.
    JavaScript: Learn | Validate | Compact | bionoid

  9. #9
    Join Date
    Sep 2013
    Posts
    26
    I will give that a try and thank you so much for taking the time to help me. You guys are great.

  10. #10
    Join Date
    Sep 2013
    Posts
    26
    I have another question if I could ask. I now need to separate the two programs on this page because I want both of them on one page. I tried putting <hr > and some more things but it will not validate. Any suggestions?

    !DOCTYPE html>
    <html lang="en">
    <head>

    <meta charset="utf-8" />
    <title>Sentence Reverser</title>

    <script type="text/javascript">

    function sentenceReverser()
    {
    var word, words = [];
    while (word = prompt('Enter a word for the sentence, Cancel to stop!', ''))
    {
    words.unshift(word);
    }
    document.reverseForm.output.value = words.join(' ');
    }

    </script>

    </head>
    <body>

    <form name="reverseForm" action="#">
    <label for="output">Reversed Sentence</label><br />
    <input type="text" id="output" />
    <input type="button" value="Run!" onclick="sentenceReverser();" />
    </form>

    </body>
    </html>






    <!doctype html>
    <html>
    <head>
    <title>Temp Stats</title>
    <meta charset="utf-8" />
    <script type="text/javascript">
    function do_work()
    {
    var days = 0.0;
    var dailyTemp = 0.0;
    var highest = -Infinity;
    var lowest = +Infinity;
    var sum = 0.0;

    for (days = 1; days<= 5; days++)
    {
    dailyTemp = prompt("Enter the noon temp for the 5 days");
    dailyTemp = parseInt(dailyTemp);
    sum += dailyTemp;
    var average = sum / days;

    lowest = (dailyTemp < lowest) ? dailyTemp : lowest;
    highest = (dailyTemp > highest) ? dailyTemp : highest;
    }
    document.tempForm.highest.value = highest;
    document.tempForm.lowest.value = lowest;
    document.tempForm.average.value = average;
    }
    </script>
    </head>
    <body>
    <form name="tempForm" action="#">
    <label for="highest">Highest Temp: </label><br />
    <input type="text" id="highest" />
    <br />
    <label for="lowest">Lowest Temp: </label><br />
    <input type="text" id="lowest" />
    <br />
    <label for="average">Average Temp: </label><br />
    <input type="text" id="average" />
    <input type="button" value="Run!" onclick="do_work()" />
    <br />
    </form>
    </body>
    </html>

  11. #11
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    Hi, here are your two programs on 1 page.

    Please notice how things are laid out, for example you can only have 1 <doctype>, <head>, <body> per page. <script>'s don't really care where you place them, as long as they are available before the DOM decides to use them. If you had <style> elements in there, they must exist only in the <head> part of the page.

    Code:
    <!DOCTYPE html>
    <html lang="en">
    	<head>
    		<meta charset="utf-8" />
    		<title>Sentence Reverser / Temperatures</title>
    
    		<script type="text/javascript">
    
    			function sentenceReverser()
    			{
    				var
    					word,
    					words = [];
    
    				while (word = prompt('Enter a word for the sentence, blank or Cancel to stop!', '')) {
    					words.unshift(word);
    				}
    				document.reverseForm.output.value = words.join(' ');
    			}
    
    			function do_work()
    			{
    				var
    					days,
    					dailyTemp,
    					highest = -Infinity,
    					lowest = +Infinity,
    					sum = 0.0,
    					average = 0;
    
    				for (days = 1; days <= 5; ++days) {
    					dailyTemp = prompt('Enter the noon temp for the 5 days');
    					dailyTemp = parseInt(dailyTemp, 10);
    					sum += dailyTemp;
    					average = sum / days;
    					lowest  = (dailyTemp < lowest)  ? dailyTemp : lowest;
    					highest = (dailyTemp > highest) ? dailyTemp : highest;
    				}
    				document.tempForm.highest.value = highest;
    				document.tempForm.lowest.value  = lowest;
    				document.tempForm.average.value = average;
    			}
    
    		</script>
    
    	</head>
    	<body>
    
    		<form name="reverseForm" action="#">
    			<label for="output">Reversed Sentence</label><br />
    			<input type="text" id="output" />
    			<input type="button" value="Run!" onclick="sentenceReverser();" />
    		</form>
    
    		<hr />
    
    		<form name="tempForm" action="#">
    			<label for="highest">Highest Temp: </label><br />
    			<input type="text" id="highest" />
    			<br />
    			<label for="lowest">Lowest Temp: </label><br />
    			<input type="text" id="lowest" />
    			<br />
    			<label for="average">Average Temp: </label><br />
    			<input type="text" id="average" />
    			<input type="button" value="Run!" onclick="do_work();" />
    		</form>
    
    	</body>
    </html>
    JavaScript: Learn | Validate | Compact | bionoid

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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