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

    sentence reverser

    I am trying to make a sentence reverser. Can you please tell me if I am way off on this. Everything works until I get to the output nothing happens. Thank You

    JavaScript Page

    Code:
     
    function  reverseSentence()
    {
    	
    var strSentence = prompt("Enter the sentence you want to reverse");
    
    String.prototype.string_reverse= function reverseSentence(){
     return this.split('').reverse().join('');
    }
    
    var array = string.split(" ");
    for(var word=0; word<arr.length; word++){
     array[word] = array[word].string_reverse();
    }
    
    alert(array.join(" "));
    document.reverseForm.sentence.value = sentence;
    }

    HTML Page

    Code:
    <!doctype html>
    <html>
    	<head>
    		<title>Sentence reverser</title>
    		<meta charset="utf-8" />
    		<script type="text/javascript" src="sentenceReverser.js"></script>
    	</head>
    	<body>
    		<form name="reverseForm" action="#">
    			<label for="sentence">Sentence</label>
    			<textarea id="sentence" rows="3" cols="24"></textarea>
    			<br />
    			
    			<input type="button" value="Run!" onclick="reverseSentence()" />
    		</form>
    	</body>
    </html>

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,065
    Thanks
    36
    Thanked 497 Times in 491 Posts

    Lightbulb

    A bit unclear if you are trying to reverse the sentence or reverse the words in the sentence.
    Going with you existing logic, but not sure from your description...

    Code:
    <!doctype html>
    <html>
    <head>
    <title>Sentence reverser</title>
    <meta charset="utf-8" />
    
    <script type="text/javascript">
    function  reverseSentence() {
      var strSentence = document.reverseForm.inputSentence.value;
      var array = strSentence.split(" "); 
      for(var word=0; word<array.length; word++){
        array[word] = array[word].string_reverse();
      }
      document.reverseForm.outputSentence.value = array.join(" ");
    }
    
    String.prototype.string_reverse = function () {
      return this.split('').reverse().join('');
    }
    </script>
    
    </head>
    <body>
    <form name="reverseForm" action="#">
    <label for="inputSentence">Input Sentence to be reversed</label> <br />
    <textarea name="inputSentence" rows="3" cols="24">This is a sentence</textarea>
    <p />
    <label for="outputSentence">Sentence</label> <br />
    <textarea name="outputSentence" rows="3" cols="24"></textarea>
    <p />
    	
    <input type="button" value="Run!" onclick="reverseSentence()" />
    </form>
    </body>
    </html>
    Compare this to your attempt.
    Note some of the more important changes:
    1. prompt() is not a very good way to collect user information
    2. you cannot reference an element your way with an 'id=', you needed a 'name='
    3. incorrect prototype definition and you never called it to display output.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Another way:-

    Code:
    <div id = "reversed"> </div>
    
    <script type="text/javascript">
    
    function outputbackwards() {
    var output = "";
    for (i=this.length-1; i>=0; i--) {
    output += this.charAt(i);  // or output += this.charAt(i).toLowerCase(); if desired
    }
    document.getElementById("reversed").innerHTML  = output;
    }
    
    var message = "The quick brown fox jumps over the lazy dog";
    String.prototype.writeback = outputbackwards;
    message.writeback();
    
    </script>
    If all is wanted is to reverse the word order:-


    Code:
    <script type = "text/javascript">
    
    var message = "The quick brown fox jumps over the lazy dog";
    var msplit = message.split(" ");
    var output = msplit.reverse().join(" ");
    alert (output);
    
    </script>
    Quizmaster: How many centimetres are there in 3.5 metres?
    Contestant: 115
    Last edited by Philip M; 10-03-2013 at 08:34 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.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,033
    Thanks
    0
    Thanked 248 Times in 245 Posts
    Quote Originally Posted by Philip M View Post
    Code:
    <script type = "text/javascript">
    
    var message = "The quick brown fox jumps over the lazy dog";
    var msplit = message.split(" ");
    var output = msplit.reverse().join(" ");
    alert (output);
    
    </script>
    This is what I was thinking too. But it gets complicated when there are punctuations in the middle and/or end of the sentence.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Quote Originally Posted by glenngv View Post
    This is what I was thinking too. But it gets complicated when there are punctuations in the middle and/or end of the sentence.
    As I see it the only way is to strip out punctuation. But is there any real-world or practical use for a sentence reverser in any case?

    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
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,033
    Thanks
    0
    Thanked 248 Times in 245 Posts
    None really. But I can see these types of script being asked/used in job interviews or programming classes to test your skills.

  • #7
    Regular Coder
    Join Date
    Mar 2006
    Posts
    726
    Thanks
    35
    Thanked 132 Times in 123 Posts
    Multiple sentences, capitalized words, contractions, and punctuation all make this exercise more like work.

    Here is a start- what you are going for is a method that will return the original text if you run it twice.


    <!doctype html>
    <html lang="en">
    <head>
    <meta charset= "utf-8">
    <title>Sentence Reverser</title>
    <script>
    Code:
    function reverseSentence(txt){
    	txt=txt.replace(/\s+/g,' ').trim();
    	txt=txt.charAt(0).toLowerCase()+txt.substring(1),	
    	var temp= (txt.match(/([^\w']+|[\w']+)/g)||[]).reverse().join('').trim();
    	return temp.charAt(0).toUpperCase()+temp.substring(1)+'.';
    }
    function reverseSentences(){
    	var i=0, t=reverseForm.output,
    	A=t.value.split(/\.( |$)/).map(function(itm){
    		if(/\w/.test(itm))return reverseSentence(itm).replace(/\bi\b/g,'I');
    		return '';
    	});
    	t.value=A.reverse().join(' ');
    }
    </script>
    </head>
    <body>
    <form name= "reverseForm" action= "#">

    <textarea name= "output" rows= "20" cols= "40">
    Paul Bunyan was born in Maine. When three weeks old he rolled around so
    much in his sleep that he destroyed four square miles of standing
    timber. Then they built a floating cradle for him and anchored it off
    Eastport. When Paul rocked in his cradle it caused a seventy- five foot
    tide in the Bay of Fundy and several villages were washed away. He
    couldn't be wakened, however, until the Navy was called out and
    fired broadsides for seven hours. When Paul stepped out of his cradle he
    sank seven warships and the government seized his cradle and
    used the timber to build seven more. That saved Nova Scotia from
    becoming an island, but the tides in the Bay of Fundy haven't subsided
    yet.</textarea>

    <input type= "button" value= "Reverse" onclick= "reverseSentences()">
    </form>
    </body>
    </html>
    Last edited by mrhoo; 10-05-2013 at 10:21 PM.

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,592
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by mrhoo View Post
    Multiple sentences, capitalized words, contractions, and punctuation all make this exercise more like work.
    With a question that is so complicated to answer you have to wonder why the students would be using prompt to collect the input. Even where the class teaches antiquated code like that first they should have gone way beyond that with forms and the document object model long before getting to this question (which obviously belongs in an intermediate level JavaScript course since it requires a good understanding of regular expressions to be able to produce the answer.
    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.


  •  

    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
    •