zoukankan      html  css  js  c++  java
  • JS 笔记

    http://eloquentjavascript.net/05_higher_order.html

     第五章作业题

    1. Use the reduce method in combination with the concat method to “flatten” an array of arrays into a single array that has all the elements of the input arrays.

    1 var arrays = [[1, 2, 3], [4, 5], [6]];
    2 // Your code here.
    3 function flatten(previousArray, currentArray){
    4       return previousArray.concat(currentArray);
    5 };
    6 // → [1, 2, 3, 4, 5, 6]
    7 console.log(arrays.reduce(flatten));

     2.

    Using the example data set from this chapter, compute the average age difference between mothers and children (the age of the mother when the child is born). You can use the average function defined earlier in this chapter.
    Note that not all the mothers mentioned in the data are themselves present in the array. The byName object, which makes it easy to find a person’s object from their name, might be useful here.function average(array) {

    function plus(a, b) { return a + b; }
      return array.reduce(plus) / array.length;
    }
    
    var byName = {};
    ancestry.forEach(function(person) {
      byName[person.name] = person;
    });
    
    // Your code here.
    function hasKnownMother(person){
        return byName[person.mother] != undefined;
    };
    function difference(person){
        return person.born - byName[person.mother].born;
    };
    console.log(average(ancestry.filter(hasKnownMother).map(difference)));

    3

    Historical life expectancy

    When we looked up all the people in our data set that lived more than 90 years, only the latest generation in the data came out. Let’s take a closer look at that phenomenon.

    Compute and output the average age of the people in the ancestry data set per century. A person is assigned to a century by taking their year of death, dividing it by 100, and rounding it up, as in Math.ceil(person.died / 100).

    // Your code here.
    var Centurys = {};
    function groupBy(person){
        var century = Math.ceil(person.died/100);
        if(Centurys[century] == undefined)
            Centurys[century] = new Array();
        Centurys[century].push(person.died - person.born); 
    };
    ancestry.forEach(groupBy);
    console.log(Centurys);
    for(century in Centurys){
        console.log(average(Centurys[century]));
    };
    // → 16: 43.5
    //   17: 51.2
    //   18: 52.8
    //   19: 54.8
    //   20: 84.7
    //   21: 94

    4. 

    Every and then some

    Arrays also come with the standard methods every and some. Both take a predicate function that, when called with an array element as argument, returns true or false. Just like && returns a true value only when the expressions on both sides are true, every returns true only when the predicate returns true for all elements of the array. Similarly, some returns true as soon as the predicate returns true for any of the elements. They do not process more elements than necessary—for example, if some finds that the predicate holds for the first element of the array, it will not look at the values after that.

    Write two functions, every and some, that behave like these methods, except that they take the array as their first argument rather than being a method.

    function every(datas,query){
      for(key in datas){
        if(!query(datas[key]))
          return false;
      }
      return true;
    }
    function some(datas,query){
      for(key in datas){
        if(query(datas[key]))
          return true;
      }
      return false;
    }
    console.log(every([NaN, NaN, NaN], isNaN));
    // → true
    console.log(every([NaN, NaN, 4], isNaN));
    // → false
    console.log(some([NaN, 3, 4], isNaN));
    // → true
    console.log(some([2, 3, 4], isNaN));
    // → falsetion every(datas,query){
  • 相关阅读:
    【2】通过Ajax方式上传文件(图片),使用FormData进行Ajax请求
    【1】mongoDB 的安装及启动
    第一篇博客
    Java Integer 进制转化的实现(附源码),对模与补码的理解
    筛法求素数(普通筛法与欧拉筛法) 这是个问题
    字典的拼接方法
    使用Selenium抓取百度指数一
    正则表达式30分钟入门教程
    微博抓取尝试
    __call__方法的最简要说明
  • 原文地址:https://www.cnblogs.com/xiexindut/p/4422095.html
Copyright © 2011-2022 走看看