Git+node.js+TDD

环境要求

  • Git
  • node.js
  • jasmine
  • TDD

git安装及命令学习

node.js 安装

jasmine

  • 在npm下安装jasmine
npm install -g jasmine
  • 熟悉jasmine命令
jasmine init

TDD

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,886评论 25 709
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,786评论 18 399
  • 一天又一天,我们慢慢走远; 一月又一月,我们渐渐长大; 一年又一年,我们长大成人; 慢慢地,我们踏上了人生的火车!...
    古泽平雨阅读 189评论 0 1
  • 《一切都很完美》 为什么不小心就伤到了别人, 你把自己太当回事儿了, 照着自己的标准和态度生活, 忽视别人的存在。...
    axjl如意阅读 169评论 0 0
  • 1. 忙碌的校招开始了,连读书写字的时间都被压缩,整个心思被网申宣讲会等事务占满,甚至无暇给家里打电话闲话家常。 ...
    HF的平方阅读 740评论 4 25