Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 每秒钟调用一次函数т;d时间?_Javascript_Closures - Fatal编程技术网

Javascript 每秒钟调用一次函数т;d时间?

Javascript 每秒钟调用一次函数т;d时间?,javascript,closures,Javascript,Closures,最近我在项目中得到了测试任务,它相当简单: const _ = require('underscore'); // Challenge: // // Write a function that accepts as argument a "function of one variable", and returns a new // function. The returned function should invoke the original parameter function on

最近我在项目中得到了测试任务,它相当简单:

const _ = require('underscore');

// Challenge:
//
// Write a function that accepts as argument a "function of one variable", and returns a new
// function.  The returned function should invoke the original parameter function on every "odd"
// invocation, returning undefined on even invocations.
//
// Test case:
//  function to wrap via alternate(): doubleIt, which takes a number and returns twice the input.
//  input to returned function: 1,2,3,4,9,9,9,10,10,10
//  expected output: 2, undefined, 6, undefined, 18, undefined, 18, undefined, 20, undefined

const input = [1,2,3,4,9,9,9,10,10,10];
const doubleIt = x => x * 2;

const alternate = (fn) => {
  // Implement me!
  //
  // The returned function should only invoke fn on every
  // other invocation, returning undefined the other times.
}

var wrapped = alternate(doubleIt)

_.forEach(input, (x) => console.log(wrapped(x)))
// expected output: 2, undefined, 6, undefined, 18, undefined, 18, undefined, 20, undefined
我的解决方案是:

const alternate = (fn) => {
  let odd = false;

  return (x) => {
    odd = !odd;

    if (odd) {
      return fn(x);
    }

    return undefined;
  };
};

// An alternate solution if ternary operator (?) is allowed according to coding standards used on the project.
// Sometimes it's treated as bad practise.
const alternateShort = (fn) => {
  let odd = false;

  return (x) => (odd = !odd) ? fn(x) : undefined;
};

我得到的答复是,技术负责人根本不喜欢我的解决方案,而且我也没有被雇佣参加这个项目。
我真的很困惑,你知道他还能期待什么吗?

你的代码运行得很好。我唯一要改变的是条件中的赋值(这通常会让人困惑,并且被linters禁止),但这并不是那么糟糕——它是基于意见的。我会问代码审查员他会做什么不同的事情,我没有看到任何有意义的改变。我肯定会改变第一个片段,如果那是测试用例代码-导入一个大库只是为了迭代数组是非常愚蠢的,但这听起来好像不在你们的控制范围之内。你们实际上是在要求代码审查,这是离题的。但你可以在话题之外问,但如果是我,如果面试官没有解释就拒绝了我的代码,而我的代码看起来像你的解决方案,我会认为这是一个信号,表明我很可能不想在那家公司工作。
不是三元运算符,而是一个三元运算符<代码>?:一起是,ECMAScript中唯一的三元运算符。;-)您的代码运行良好。我唯一要改变的是条件中的赋值(这通常会让人困惑,并且被linters禁止),但这并不是那么糟糕——它是基于意见的。我会问代码审查员他会做什么不同的事情,我没有看到任何有意义的改变。我肯定会改变第一个片段,如果那是测试用例代码-导入一个大库只是为了迭代数组是非常愚蠢的,但这听起来好像不在你们的控制范围之内。你们实际上是在要求代码审查,这是离题的。但你可以在话题之外问,但如果是我,如果面试官没有解释就拒绝了我的代码,而我的代码看起来像你的解决方案,我会认为这是一个信号,表明我很可能不想在那家公司工作。
不是三元运算符,而是一个三元运算符<代码>?:一起是,ECMAScript中唯一的三元运算符。;-)