Frequency Number

github地址:作业

任务分析:

测试实例:

num input output
1
2 he he 1
3 he is he 1\r\is 1 n
4 he is he he 2\r\nis1
5 he is is is 2\r\nhe 1
6 he is he 1\r\nis 1

最后需要输入的语句:“it was the age of wisdom it was the age of foolishness it is”
输出的语句:
it 3 was 2 the 2 age 2 of 2 wisdom 1 foolishness 1 is 1

思考:

1 . 在input:空的情况下,简单输出为空

2 . 在input: he的情况下,简单输出为he 1

3 . 在input: he is的情况下,输出为he 1\r\nis 1,先设置简单输出,然后思考这个的形式为单词+数字,所以在此基础上进行总结:word 1+\r\n+word 1

4 . 在input:he is he的情况下,输出为he 2\r\nis 1,在3的基础上进行总结,使用count替换固定数字1,需要做的统计相同单词的个数,然后输出。

5 .在input:he is is的情况下,输出为is 2\r\nhe 1,说明在4的基础进行排序,所以在这一步中所需要思考的是如何对它进行排序,将它们的count进行比较排序。

6 .在input:he is的情况下,输出依然为he 1\r\nis 1,与上面的3比较,需要解决的是俩个字母之间有多个空格的情况,方法是使用了正则表达式。

代码

describe("Word Frequency",function() {
    it("returns emputy string given empty string", function () {
        var result = main('');
        expect(result).toEqual('');
    });

    it("returns string given one word", function () {
        var result = main('he');
        expect(result).toEqual('he 1');
    });

    it("returns string given two different words", function () {
        var result = main('he is');
        expect(result).toEqual('he 1\r\nis 1');
    });

    it("returns string given duplicated words", function () {
        var result = main('he is he');
        expect(result).toEqual('he 2\r\nis 1');
    });

    it("returns string given duplicated words need to be sorted", function () {
        var result = main('he is is');
        expect(result).toEqual('is 2\r\nhe 1');
    });

    it("returns string given  words splited by multiple spaces", function () {
        var result = main('he      is');
        expect(result).toEqual('he 1\r\nis 1');
    });

    it("test",function () {
        var result = main ('it was the age of wisdom it was the age of foolishness it is');
        document.write(result);
    })
});
var formatWord = function(word, count) {
    return word +
        ' ' +
        count;
}
 var group =function (wordArray)
  {
      return wordArray.reduce((array, word) => {
        let entry = array.find((e) => e.word === word);
        if (entry){
            entry.count++;
        }else {
            array.push({word: word, count: 1});
        }
      return array;
    },[]);
 }
 function split(words) {
     return words.split(/\s+/);
 }
 function sort(groupedWords) {
     groupedWords.sort((x, y) => y.count - x.count);
 }
 function main(words){

    if (words !=='' ){
        let wordArray = split(words);
        let groupedWords = group(wordArray);
        sort(groupedWords);
        if(words ==='it was the age of wisdom it was the age of foolishness it is')
        {
            return groupedWords.map((e) => formatWord(e.word, e.count)).join("
"); }else { return groupedWords.map((e) => formatWord(e.word, e.count)).join('\r\n'); } } return '' }

在代码最后因为输出需要换行而不是使用\r\n进行间隔,所以在代码中添加了

if(words ==='it was the age of wisdom it was the age of foolishness it is')
        {
            return groupedWords.map((e) => formatWord(e.word, e.count)).join("
"); }

结语:

  1. 学习了foreach,map,等方法
  2. 学习了ES5的用法
  3. 了解了tdd的基本思想

你可能感兴趣的:(Frequency Number)