Function invocation every second time Recently I got test task at the project, it is fairly simple:


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 =&gt; x * 2;

const alternate = (fn) =&gt; {
  // 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) =&gt; console.log(wrapped(x)))
// expected output: 2, undefined, 6, undefined, 18, undefined, 18, undefined, 20, undefined



And my solution was:

const alternate = (fn) =&gt; {
  let odd = false;

  return (x) =&gt; {
    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) =&gt; {
  let odd = false;

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



And I got the reply that tech lead didn't like my solution at all and I'm not hired to the project.
I'm really confused, do you have any idea what else he could expect?
Do you see here any alternative solutions?
Code style is not the problem. The expectation is that a new coder uses the in house style guides when writing.

I think the problem is that you missed an important assumption. 

The test case passes one argument, however that does not mean that the function you are given will have only one argument.

Thus maybe (I am just guessing) if you wrote... 

const alternate = fn =&gt; {
    let odd = false;
    return (...args) =&gt; (odd = !odd) ? fn(...args) : undefined;
};


Also though not stated there could have been an expectation that it not throw on bad input. Thus first vet the input function.

const alternate = fn =&gt; {
    let odd = false;
    return typeof fn === "function" ?
        (...args) =&gt; odd = !odd ? fn(...args) : undefined :
        () =&gt; {};
}


or

function alternate (fn) {
    if (typeof fn === "function") {
        let odd = false;
        return (...args) =&gt; odd = !odd ? fn(...args) : undefined;
    }
    return () =&gt; {};
}


As the documentation does not say anything about throwing an error the safer bet is to silently deal with bad input