开发者

how to get array of sentences and words passing as parameter to a function in javascript?

开发者 https://www.devze.com 2023-02-11 19:47 出处:网络
I have a function say var init = function(data){ all sentences: \" I have to get all the sentences and return an array containing all sentences\"

I have a function say

 var init = function(data){
     all sentences: " I have to get all the sentences and return an array containing all sentences"
     all words : "this method should return an array of words in ‘Data’ 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"

     all reverse sentences: "this method is the same as all Sentences, except it should开发者_开发问答 return the sentences in reverse order"

     reverse words :" same as all words but in reverse order"
     countWordsBeginningWith:" this method should return the amount of words that begin with an inputted string. The optional second parameter should determine what sentence the words come from when present."

thanks


I think what you need to do is decompose your argument (assuming it's a string) into an array. For instance:

function getAllWords(sentences) {
    var result = sentences.split(' ');
    return result;
}

var init = function(data){
    var result = [];
    result['words'] = getAllWords(data.text);
    // result['sentences'] = getAllSentences(data.text);
    // result['sentencesreversed'] = getReverseSentences(data.text);
    // result['sentencewords'] = getReverseWords(data.text);
    // result['beginswith'] = getWordsBeginningWith(data.text, data.beginswith);
    return result;
}

var getIt = {
    'beginswith': 't',
    'text': 'This is stuff. I am a sentence. Stuff happens now.'
};

console.log(init(getIt));

This is a very simplistic answer, not taking into account for periods, commas, and other bits. But that's the general answer. Some for loops and/or RegEx's may occur after this point, buyer beware.


In Javascript you can pass any data structure as a function parameter.

First learn how to construct an array of strings to contain you word or sentences. Once you have accomplished this it will be straightforward.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号