Frequency Number

Github仓库地址:
https://github.com/55533/balala

本次练习涵盖的主要知识点

1.git环境配置及常用命令使用
2.安装 Nodejs 环境,并学习使用 npm init, npm install 命令
3.Webstorm安装
4.用Nodejs和Git完成TDD编程: Frequency Number

思考过程

Frequency Number_第1张图片
Paste_Image.png

Git中的基本操作

git init ---------初始化仓库
git status--------查看仓库的状态
git add-----------向暂存区添加文件
git commit--------保存仓库的历史记录
git log-----------查看提交日志
git remote add----添加远程仓库
git push ---------推送至远程仓库
git clone---------获取远程仓库

测试+实现的代码


describe("Word Frequency", function () {
    it("returns empty 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 word", function () {
        var result = main('he is');
        expect(result).toEqual('he 1\r\nis 1');
    });

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

    it("returns string given two 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 two duplicated words splited by multiple spaces", function () {
        var result = main('he     is');
        expect(result).toEqual('he 1\r\nis 1');
    });
    it("returns string given long string splited by multiple spaces", function () {
        var result = main('it was the age of wisdom it was the age of foolishness it is');
        expect(result).toEqual('it 3\r\nwas 2\r\nthe 2\r\nage 2\r\nof 2\r\nwisdom 1\r\nfoolishness 1\r\nis 1');
    });
    
});
/*"use strict";
var =require ("lodash");
var chai = require("chai");
var sinon =require ("sinon");
var sinonChai = require("sinon-chai");
chai.use(sinonChai);
var main = require("../lib/main.js");*/
var formatWordAndCount = 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;
},[]);
};
var split = function (words) {
    return words.split(/\s+/);
};
var sort = function (groupWords) {
    groupWords.sort((x, y) => y.count - x.count);
};

var format = function (groupWords) {
    return groupWords.map((e) => formatWordAndCount(e.word, e.count)).join('\r\n');
};
function main(words){
    if(words !== ''){
        let wordArray=split(words);
        let groupWords = group(wordArray);
        sort(groupWords);
        return format(groupWords);
    }
    return ''
}

Git:


Frequency Number_第2张图片
Paste_Image.png

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