Results 1 to 6 of 6

Thread: Write a function that takes a string as an input and returns an array containing the

  1. #1
    Join Date
    Jan 2008
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Write a function that takes a string as an input and returns an array containing the

    Write a function that takes a string as an input and returns an array containing the odd words. With the string: "I can sing bad songs while in the shower", the output in the alert should read: "I sing songs in shower". Below is the basic framework that you should use.

    <script type="text/javascript">
    var allWords = "I can sing bad songs while in the shower";

    function getOddWords(words){

    //place code here

    return oddWords;
    }

    alert(getOddWords(allWords));
    </script>

  2. #2
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    Code:
    <script type="text/javascript">
    var allWords = "I can sing bad songs while in the shower";
    
    function getOddWords(words){
    
    var oddWords = "";
    var splitWords = words.split(" ");
    var odd = true;
    for (var i=0;i<splitWords.length;i=i+1) {
    if (odd == true) {
    oddWords += splitWords[i] + " ";
    odd = false;
    } else {
    odd = true;
    }
    }
    
    return oddWords;
    }
    
    alert(getOddWords(allWords));
    </script>
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@ /\/\@|{
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  3. #3
    Join Date
    Jan 2008
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Take the function in above and create a String prototype so I can call the method using the following statement:

    alert(allWords.getOddWords());

  4. #4
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    Code:
    String.prototype.oddwords = function() {
    	var oddWords = "";
    	var splitWords = this.split(" ");
    	var odd = true;
    	for (var i=0;i<splitWords.length;i=i+1) {
    		if (odd == true) {
    			oddWords += splitWords[i] + " ";
    			odd = false;
    	} else {
    			odd = true;
    	}
    }
    
    return oddWords;
    }
    you would use it like this:
    Code:
    var allWords = "I can sing bad songs while in the shower";
    alert(allWords.oddwords());
    and it would alert I sing songs in shower
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@ /\/\@|{
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  5. #5
    Join Date
    Aug 2004
    Posts
    9,889
    Thanks
    3
    Thanked 964 Times in 953 Posts
    Blog Entries
    15

    Default

    This sounds like a homework assignment. If so, please do not simply dump the entire assignment and expect someone else here to solve it for you. That's called cheating. You need to work on it yourself, and if you have a specific question on a particular aspect of the assignment, you're certainly welcome to get help on that.

    I'll keep this thread open for now, but please be warned when starting new threads in the future.

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    String.prototype.oddWords = function() {
      for(var i = 0, w = this.split(" "), r = w && [w[0]]; i < w.length; ++i % 2 || w[i] && r.push(w[i]));
      return r.join(" ");
    };
    Agreed that it kind of sounds like a homework assignment though. Also note that this:
    alert(getOddWords(allWords));
    this:
    the output in the alert should read: "I sing songs in shower"
    and this:
    returns an array containing the odd words
    ... are mutually incompatible.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franais | entiendo espaol | ti t hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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