ThoughtWorks暑期特训——<任务1~3>学习总结

结构化编程

结构化编程是一种编程典范。它采用子程序、程式码区块(英语:block structures)、for循环以及while循环等结构,来取代传统的 goto。希望借此来改善计算机程序的明晰性、品质以及开发时间,并且避免写出面条式代码。

结构化程序设计提出的原则:自顶向下,逐步细化;清晰第一,效率第二;书写规范,缩进格式;基本结构,组合而成

在eclipse che中测试程序

首先安装测试依赖 npm installnpm i
开始测试:

  • npm test 测试全部代码文件
  • node_modules/jasmine/bin/jasmine.js spec/section-1/prectice-1-spec.js 单独测试一个文件

for循环

1 ('选出A集合中与B集合中相同的元素')

module.exports = function collectSameElements(collectionA, collectionB){
    let result = [];
//循环A集合,得到每一个item:
    for( let item of collectionA){ 
        if(includes(collectionB,item)){
              result.push(item);
        }
    }
    return result;
}
fuction includes(collection,ch){
    for( let item of collection){
        if(item === ch){
              return ture;
        }
    }
    return false;
}

2 (collection = [ 'a', 'a', 'a', 'e', 'e', 'e', 'e', 'e', 'e', 'e',...],'把A集合中相同的元素统计出数量')

module.exports = function countSameElements(collection) {
          let result = [];
          for(let item of collection){
                    let obj = finds(result,item);
                    if(obj){
                              obj.count++;
                    }else{
                              result.push({key: item, count: 1});
                    }
          }
          return result;

}
function finds(collection,ch){
          for(let item of collection){
                    if(item.key === ch){
                              return item;
                    }
          }
          return null;//注意此处返回的是null,自己写的时候写成了false
}

3 (增加字符 'd-5' )

function summerize(array){//见上               
}
function finds(array,ch){//见上
}
function split(ch){
          let array = ch.split("-");
          return {key:array[0], count:parseInt(array[1],10)};
}
function push(result,key,count){
          for(var i=0; i<count; i++){
                   result.push(key); 
          }      //注意,此方法不需要返回值!    
}
function expend(collection){
          let result = [];
          for(let item of collection){
                    if(item.length > 1){
                              let {key,count} = split(item);    //ES6的语法,返回key-count键值对
                              push(result,key,count);
                    }else{
                              result.push(item);
                    }           
          }
          return result;
}
module.exports = function countSameElements(collection) {
          let array = expend(collection);
          return summerize(array);
}

4 (增加字符 'h[3]' 'c:98' 等)

其它部分同上
function split(ch){
          if(ch.includes("-")){
                    let array = ch.split("-");
                    return {name:array[0],summary:parseInt(array[1],10)};
          }
          if(ch.includes(":")){
                    let array = ch.split(":");
                    return {name:array[0],summary:parseInt(array[1],10)};
          }
          if(ch.includes("[")){
                    let name = ch.charAt(0);
                    let summary = parseInt(ch.substr(2,ch.length-1));
                    return {name,summary};
          }
}
//charAt() 方法从一个字符串中返回指定的字符 `str.charAt(index)`
//index 为介于0 和字符串长度减1之间的整数。 (0~length-1)
//substr() 方法返回一个字符串中从指定位置开始到指定字符数的字符。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,009评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,638评论 25 708
  • 在账务处理的过程中,也经常会遇到账实不符的情况,造成账实不符的原因是多方面的:在财产物资保管过程中发生的自然损耗;...
    求研闵阅读 445评论 0 0
  • 如果没有看过 基础篇,从这里进入iOS 静态库制作❣基础篇。提示:开源光荣 与学习静态库的制作没有任何关系。将自优...
    CoderHG阅读 532评论 0 7