zoukankan      html  css  js  c++  java
  • [Javascipt] Immediately-Invoker 2

    Now the people at Poplar Puzzles would like you to treat an array of functions like a Queue, passing the result of each function into the next until the Queue is empty. They’ve sent you the new queue of functions, and a “simple directive”:

    In a variable called applyAndEmpty, build and store a function that takes in a single number and any Queue of functions as inputs and then applies the Queue’s functions in order to the input number, where the results of each function become the next function’s input. Additionally, the queue should be empty following your application. Lastly, because we are just that freaking awesome, any loops you use must only be for-loops. MWAHAHA. Then call your new function using the number 2 and the provided puzzlers queue as initial inputs, and alert the result.- PuZzLe MaSTeRs

    The new Queue of functions to use is below.

    var puzzlers = [
      function ( a ) { return 8*a - 10; },
      function ( a ) { return (a-3) * (a-3) * (a-3); },
      function ( a ) { return a * a + 4; },
      function ( a ) { return a % 5; }
    ];
    

    We’ve provided the queue and the start value for you.

    var puzzlers = [
      function ( a ) { return 8*a - 10; },
      function ( a ) { return (a-3) * (a-3) * (a-3); },
      function ( a ) { return a * a + 4; },
      function ( a ) { return a % 5; }
    ];
    var start = 2;
    var applyAndEmpty = function(num){
      puzzlers.map(function(func){
          var res = func(num);
        num = res;
        alert(res);
            return res;
      });
    };
    applyAndEmpty(start);
    function buildNow(){
      
      return (function(){
        
        alert("RUN NOW");
      })(); // <<---- We add '()', so that as soon as this function return, it will Immediately invoke.
    }
    buildNow();
    
    
    function buildWhenICall(){
      
      return function(){
        
        alert("You call me!");
      };
    }
    
    buildWhenICall()(); // <<--- Or we can do something like that
    // The same as
    //var run = buildWhenICall();
    //run();
    
    
  • 相关阅读:
    C#解析Json数据(利用Newtonsoft.Json库)
    巧妙解决百度地图加偏纠偏问题
    sql 日期时间函数+格式转换
    遇到sql server 遇到以零作除数错误
    js实现鼠标移到链接文字弹出一个提示层的方法
    sql语句 如何判断A表中的a列数据是否在B表中的b列中存在
    MVC使用Gantt Chart实现甘特图,管理事情进度
    jQueryGantt—集变态与惊艳于一身
    自动化测试
    小程序测试及一些限制
  • 原文地址:https://www.cnblogs.com/Answer1215/p/3887437.html
Copyright © 2011-2022 走看看