Solved

Help with constructor

Posted on 2014-09-22
3
273 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

830 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