Solved

Help with constructor

Posted on 2014-09-22
3
258 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
Comment Utility
example of input/outpu for both function please
0
 

Author Comment

by:894359
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article will show, step by step, how to integrate R code into a R Sweave document
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now