2017-01-07 4 views
1
function countWords(inputWords) { 
var sortArray = inputWords.sort(function (idx1, idx2) { return idx1.localeCompare(idx2) }); 
var resultArray = []; 
var counter = 1; 

sortArray.reduce(function (total, currentValue) { 
    if (total == currentValue) 
     counter++; 
    else { 
     resultArray.push(counter); 
     counter = 1; 
    } 
    return currentValue; 
}); 

    return resultArray; 
} 
module.exports = countWords 

現在、各文字列が配列内で発生する回数を取得しようとしています。私は上記の関数を書いたが、バグがあるようだ。各文字列が配列内で発生する回数を取得する

PS:これはhttps://nodeschool.io/の運動です。npm install -g functional-javascript-workshop。私は減らす機能を使用するために持っていると私は私の入力/出力された任意のための/ whileループ

を使用することはできませんよ。

input:  [ 'ad', 'ad', 'ad', 'aliqua', 'aliquip', 'aute', 'consectetur', 'consequat', 'culpa', 'culpa', 'culpa', 'do', 'do', 'dolor', 'dolore', 'eiusmod', 'elit', 'esse', 'esse', 'est', 'et', 'et', 'ex', 'excepteur', 'excepteur', 'exercitation', 'exercitation', 'id', 'id', 'id', 'incididunt', 'labore', 'laborum', 'lorem', 'magna', 'minim', 'minim', 'minim', 'mollit', 'nostrud', 'nulla', 'pariatur', 'proident', 'qui', 'qui', 'reprehenderit', 'reprehenderit', 'sint', 'ullamco', 'velit' ] 
submission: [ 3, 1, 1, 1, 1, 1, 3, 2, 1, 1, 1, 1, 2, 1, 2, 1, 2, 2, 3, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 2, 2, 1, 1 ] 

ワークショップを、これは間違っている「と言います」。

誰かが助けることができますか?

+0

_ "ワークショップ "は、" これは間違っている。" と言う_ "間違った" とは何ですか?結果は実際には間違っていますか?結果を確認しましたか? – guest271314

+1

は "ワークショップ"なしで簡単な入力でテストを開始します – RomanPerekhrest

+0

ワークショップのチェックにはおそらく 'Array#sort'のために結果の順序に問題があります。 –

答えて

1

機能の開始時に使用するArray#sortのため、ワークショップのチェックには、提出した結果の順序に問題がある可能性があります。

あなたは結果の順序を変更せずにカウントするためにこれらのメソッドを使用することができます。

A ES6ワードカウンタをarray spread構文とMapを使用していますArray#reduceと。 Mapは単語を数えます。 Mapは、Map#valuesによってイテレータに変換され、次にspreadを使用して配列に変換されます。

const words = [ 'ad', 'ad', 'ad', 'aliqua', 'aliquip', 'aute', 'consectetur', 'consequat', 'culpa', 'culpa', 'culpa', 'do', 'do', 'dolor', 'dolore', 'eiusmod', 'elit', 'esse', 'esse', 'est', 'et', 'et', 'ex', 'excepteur', 'excepteur', 'exercitation', 'exercitation', 'id', 'id', 'id', 'incididunt', 'labore', 'laborum', 'lorem', 'magna', 'minim', 'minim', 'minim', 'mollit', 'nostrud', 'nulla', 'pariatur', 'proident', 'qui', 'qui', 'reprehenderit', 'reprehenderit', 'sint', 'ullamco', 'velit' ]; 
 

 
const countWords = (words) => [...words.reduce((m, w) => m.set(w, (m.get(w) || 0) + 1), new Map).values()]; 
 

 
const result = countWords(words); 
 

 
console.log(result);

とハッシュオブジェクトにArray#reduceを使用していますES5バージョン。

var words = [ 'ad', 'ad', 'ad', 'aliqua', 'aliquip', 'aute', 'consectetur', 'consequat', 'culpa', 'culpa', 'culpa', 'do', 'do', 'dolor', 'dolore', 'eiusmod', 'elit', 'esse', 'esse', 'est', 'et', 'et', 'ex', 'excepteur', 'excepteur', 'exercitation', 'exercitation', 'id', 'id', 'id', 'incididunt', 'labore', 'laborum', 'lorem', 'magna', 'minim', 'minim', 'minim', 'mollit', 'nostrud', 'nulla', 'pariatur', 'proident', 'qui', 'qui', 'reprehenderit', 'reprehenderit', 'sint', 'ullamco', 'velit' ]; 
 

 
function countWords(arr) { 
 
    return words.reduce(function(m, w) { 
 
    // if the hash doesn't include the word 
 
    if (!m.hash.hasOwnProperty(w)) { 
 
     // push a new count of 0 to that word 
 
     m.counts.push(0) 
 
     
 
     // add the index of the word's count 
 
     m.hash[w] = m.counts.length - 1; 
 
    } 
 

 
    // increment the count at the index of the word's count 
 
    m.counts[m.hash[w]]++; 
 

 
    return m; 
 
    }, { hash: {}, counts: [] }).counts; // return the counts array 
 
} 
 

 
var result = countWords(words); 
 

 
console.log(result);

関連する問題