美文网首页
ThoughtWorks暑期特训——<任务1~3>学习总结

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

作者: 沙子粒儿在西安 | 来源:发表于2017-08-01 01:43 被阅读0次

    结构化编程

    结构化编程是一种编程典范。它采用子程序、程式码区块(英语: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() 方法返回一个字符串中从指定位置开始到指定字符数的字符。
    

    相关文章

      网友评论

          本文标题:ThoughtWorks暑期特训——<任务1~3>学习总结

          本文链接:https://www.haomeiwen.com/subject/jmudlxtx.html