Solved

Help with constructor

Posted on 2014-09-22
3
272 Views
Last Modified: 2014-09-23
I have an object constructor which expects a string input (initialData). The object constructor returns an object that contains the following methods:
a.      getSentences: this method should return an array of the sentences contained in ‘initialData’
b.      getWords: this method should return an array of words in ‘initialData’ when there is no parameter passed in. Optionally, when there is a parameter passed in that is a number, return the words in the sentence indicated by the input parameter. Not sure how to do this?
0
Comment
Question by:894359
  • 2
3 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 40338156
example of input/outpu for both function please
0
 

Author Comment

by:894359
ID: 40338375
Sample input:

var sampleData = 'My experience in the nature study area was full of surprises. First of all, many unexpected creatures crossed our path! For example, as soon as we left the parking area and entered the grassy path, a long snake slithered along the edge of the high grass and quickly disappeared. In addition, I was surprised by how colorful the grasses, which from a distance all appear to be green, actually are.';

Sample outputs:
a.   array with sentence in each element
  res[0] = My experience in the nature study area was full of surprises.
  res[1] = First of all, many unexpected creatures crossed our path!
etc.

b. no parameter passed
   res1[0] = My
   res1[1] = experience
b if parameter passed 3 e.g., returns first 3 words of every sentence
   res1[0] = My experience in
   res1[1] = First of all
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40339395
var Class = function(strInitialData) {
    return {
        initialData: strInitialData,
        getSentences: function() {
            return this.initialData.match(/\n|([^\r\n.!?]+([.!?]+|$))/gim);
        },
        getWords: function(q) {
            var words = this.initialData.match(/\b[^\s]+\b/g);
            var arr = [];
            for(var i=0;i<words.length;i++) {
                var ary = [];
                ary.push(words[i]);
                for(var j=0;j<q-1;j++) {
                    i++;
                    if(typeof words[i] != "undefined") {
                        ary.push(words[i]);
                    }
                }
                arr.push(ary.join(" "));
            }
            return arr;
        }
    }
}

Open in new window


Use :
var sampleData = 'My experience in the nature study area was full of surprises. First of all, many unexpected creatures crossed our path! For example, as soon as we left the parking area and entered the grassy path, a long snake slithered along the edge of the high grass and quickly disappeared. In addition, I was surprised by how colorful the grasses, which from a distance all appear to be green, actually are.';
var c = new Class(sampleData);
console.log( c.getWords(0) ); // use firefox, chrome or recent IE
console.log( c.getWords(3) ); // use firefox, chrome or recent IE
console.log( c.getSentences() ); // use firefox, chrome or recent IE

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question