一、Frequency Number处理前准备
-
Git准备
- 通过连接https://git-for-windows.github.io下载Windows版本git并安装
- 安装后打开GitBash,通过命令
npm install
安装nmp包。
- 通过命令
npm install -g jasmine
安装jasmine测试框架
- 初步练习创建版本库,并熟悉相关命令如
git init
、git add
、git status
、git commit -m
、git log
、git log --pretty=oneline
、git checkout
、git reset
。
-
Nodejs准备
二、Frequency Number要求
三、Frequency Number处理过程
var format = function (word,count){
return word +" "+ count;
};
var group = function(wordArray){
return wordArray.reduce((array,word) => {
var 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(groupedWords){
groupedWords.sort((s,y)=>y.count - x.count);
};
function main(words){
if(words!== ''){
var wordArray = words.split(/\s+/);
var groupedWords = group(wordArray);
groupedWords.sort((x,y) =>y.count -x.count);
return groupedWords.map((e) => format(e.word,e.count)).join('\r\n');
}
return '';
}
module.exports = main;
var main = require("./main.js");
describe("Word Frequency",function(){
it("test1 returns empty string given empty string",function(){
var result = main('');
expect(result).toEqual('');
});
it("test2 returns string given one word",function(){
var result = main('he');
expect(result).toEqual('he 1');
});
it("test3 returns string given two different words",function(){
var result = main('he is');
expect(result).toEqual('he 1\r\nis 1');
});
it("test4 returns string given duplicated words",function(){
var result = main('he is he');
expect(result).toEqual('he 2\r\nis 1');
});
it("test5 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("test6 returns string given words splited by multiple spaces",function(){
var result = main('he is');
expect(result).toEqual('he 1\r\nis 1');
});
it("test7 returns string given words 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');
});
});
-
依次通过每一个测试
-
每通过一个测试都需要用git提交一次
-
最后通过git log 查看提交记录
四、Frequency Number处理结果