Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 278
  • Last Modified:

Help with constructor

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
894359
Asked:
894359
  • 2
1 Solution
 
leakim971PluritechnicianCommented:
example of input/outpu for both function please
0
 
894359Author Commented:
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
 
leakim971PluritechnicianCommented:
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now