2011-02-19 31 views
-1

我有一个函数说如何获取作为参数传递给javascript中的函数的句子和单词的数组?

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." 

感谢

+1

是这个家庭作业? – drudge 2011-02-19 00:40:01

+0

你究竟在这里要做什么? – 2011-02-19 00:42:38

回答

1

我认为你需要做的是分解你的argument(假设它是一个字符串)到array。例如:

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)); 

这是一个非常简单的答案,没有考虑周期,逗号和其他位。但这是一般的答案。有些for loops和/或RegEx可能发生在这一点之后,买家要小心。

0

在Javascript中你可以通过任何数据结构作为函数参数。

首先学习如何构造一个字符串数组来包含你的单词或句子。一旦你完成了这个,它将是直截了当的。

相关问题