环境要求
- Git
- node.js
- jasmine
- TDD
git安装及命令学习
- 安装直接在Git官网下载默认选项安装
- 命令行学习https://try.github.io/levels/1/challenges/1
node.js 安装
- 直接下载默认安装,需要注意npm的安装位置
- 运行一个Hello World的js程序
- 学习js语法 http://codefordream.com/courses/js_basic/sections
jasmine
- 在npm下安装jasmine
npm install -g jasmine
- 熟悉jasmine命令
jasmine init
TDD
- TDD测试开发了解 http://www.jianshu.com/p/62f16cd4fef3
Frequency Number需求:
我想要一个nodejs小程序,它可以帮我处理一段字符串信息,这段字符串信息是由英文单词组成,每两个单词之间有空格,处理结果也为一段字符串,这个字符串应该每行只显示一个单词和它的数量,并且按出现频率倒序排列
example:
- input:
“it was the age of wisdom it was the age of foolishness it is”```
- **output:**
it 3
was 2
the 2
age 2
of 2
wisdom 1
foolishness 1
is 1
###分解问题
![279826-b97e82b391b62775.jpg](http://upload-images.jianshu.io/upload_images/5365889-da633dcbc50de8f8.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
###测试分块
- "" => ""
- "it" => "it 1",一个单词,驱动出格式化字符串的代码
- "it was" => "it 1\r\nwas 1",两个不同单词,驱动出分割单词的代码
- "it it was" => "it 2\r\was 1",有相同单词,驱动出分组代码
- "it was was" => "was 2\r\nit 1",驱动出分组后的排序代码
- "it was" => "it 1\r\nis was",多个空格,完善分割单词的代码
###测试代码
- **main.js**
function main(words) {
if(words!=='')
{
let wordArray=words.split(/\s+/)
let groupWords=group(wordArray)
sort (groupWords);
return groupWords.map((e)=>format(e.word,e.count)).join('\r\n')
}
return ''
}
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 sort =function(groupWords){
groupWords.sort((x,y)=>y.count-x.count)
};
var format=function(word,count)
{
return word+' '+count
}
module.exports = main
- **test.js**
describe("Word Frequency", function() {
var main = require('../../lib/my_spec/main.js')
it('returns empty string given empty string',function(){
var result=main('')
var expect_string=''
expect(expect_string).toEqual(result)
})
it('returns string given one word',function()
{
var result=main('it')
expect(result).toEqual('it 1')
})
it('returns string given two different words',function()
{
var result=main('it was')
expect(result).toEqual('it 1\r\nwas 1')
})
it('returns string given duplicated different words',function()
{
var result=main('it it was')
expect(result).toEqual('it 2\r\nwas 1')
})
it('returns string given duplicated different words need to be sorted',function()
{
var result=main('it was was')
expect(result).toEqual('was 2\r\nit 1')
})
it('returns string given words splited by multiple spaces',function()
{
var result=main('it was')
expect(result).toEqual('it 1\r\nwas 1')
})
it('returns string given full words ',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**
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/5365889-ae4de390bf0c5d7f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
- **jasmine**
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/5365889-0a70c3b57ab7fedc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
##总结
- 在环境安装上花费了大量时间,总是出现很细小的错误导致自己 不断重来浪费了时间,对于英语比以前弱了,好多东西都要看好久才知道。
- 在写实现函数时构思不够好,好多都是处于初级阶段,优化方法很少甚至没有,看了教练的视频才着手改正优化自己的函数。
- 对js语法非常不熟练,会因为找资料浪费时间打断思路
- 发现写东西时不够细心,会因为一个小错误找半天,而且是一般不犯的错误。
##仓库链接
https://github.com/heeray/words