Github链接
TDD作业划分
1.分单词
2.分组
3.排序
4.多个空格
Frequency number 的最终代码
![
![QQ图片20170519232420.png](http:https://img.haomeiwen.com/i5392874/9419b0cfeab70d69.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)]VF(%36@ZVYHO5`S.png](http:https://img.haomeiwen.com/i5392874/878cfe35c0671ebd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
var format = function (words)
{
return words ;
}
function main(words)
{
var t,s;
var i,string='',string2,count = 0;
var str = new Array();
var str2 = new Array();
var num = new Array();
str = words.split(/\s+/);
str2[0] = str[0];
for (i=0 ;i<str.length ; i++) {
if (str[i] !== str2[count]) {
count++;
str2[count] = str[i];
}
}
for (i=0 ;i<str2.length ; i++) {
var h = 0;
for (j = 0; j < str.length; j++) {
if (str2[i] === str[j]) {
h++;
}
}
num[i] = h;
}
for (i=0 ;i<num.length-1 ; i++) {
for (j=i+1 ;j<num.length ; j++) {
if(num[i]>num[j])
{
t=num[i];
num[i]=num[j];
num[j]=t;
s=str2[i];
str2[i]=str2[j];
str2[j]=s;
}
}
}
for (i=0 ;i<str2.length ; i++) {
if(i < str2.length-1 ) {
string2 = str2[i] +' '+ num[i] + '\r\n';
}
else
{
string2 = str2[i] +' '+ num[i];
}
string += string2;
}
return format(string );
}
module.exports = main;
网友评论