Solved

Help with constructor

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how can display objects ? 3 22
reveal text field based on selected 10 24
jQuery not working on ELSE... why? 2 21
Loop and hide/show dropdown option with jQuery 4 20
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
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…

911 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

26 Experts available now in Live!

Get 1:1 Help Now