# 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 => 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


And my solution was:

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



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 => {
let odd = false;
return (...args) => (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 => {
let odd = false;
return typeof fn === "function" ?
(...args) => odd = !odd ? fn(...args) : undefined :
() => {};
}


or

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


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

• the one function variable is literally part of the written challenge. That doesnt take away that your code would get extra bonus points. – konijn Jul 20 '19 at 20:13

A short review of things that could trigger me if I were the lead

• Why are you returning undefined? That is what a function does by default.
• All your functions are anonymous, a nightmare in stacktraces
• There is a time and place for arrow functions, this code implies that you never use function
• Though to be fair, the challenge over-uses arrow functions as well