Immediately-Invoked Puzzler

本文介绍了一道涉及函数调用及数组操作的编程谜题。通过定义四个不同的函数,并利用这些函数之间的相互调用来解答谜题。文章展示了如何使用一行代码实现复杂的功能调用流程。

 

The Poplar Puzzle-makers weren’t too impressed. They barely noticed your simple and beautiful array of functions, and were only sort of “meh” on the usage of the array as a queue. But now, they’ve sent you a puzzle, complete with a new set of functions to use on that puzzle. So, to display your overwhelming array and function expression mastery, alert the answer to the following question.

“What is obtained when the result of passing 9 into function 4 is then passed into the function whose array index matches the result of passing 3 into function 2?”

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; }
];

To really impress the puzzle builders, the expression used in your alert should:

  1. Involve no manual calculation or hard-coded math on your part.
  2. Use indices of arrays to access functions.
  3. Use parentheses to pass in parameters to immediately-invoking functions.
  4. Use just one line of code.

 

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; }
];

alert((puzzlers[(puzzlers[1])(3)])((puzzlers[3])(9)));

 

 

转载于:https://www.cnblogs.com/Answer1215/p/3887470.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值