ClearServe interview question

Write js code to handle `sum(1)(2)`.

Interview Answers

Anonymous

Jun 21, 2015

I didn't. This was the single point of failure which the lead js engineer let me know immediately.

Anonymous

Feb 2, 2018

/* They claimed this fails. I disagreed. They apologized. Glad I didn't work there. Purpose: * Write 1 function for calls: * sum(1,2) * sum([1,2]) * sum(1)(2) * Return: * 1 int, either number or String or primitive type */ function sum() { switch(arguments.length) { case(1): if(typeof(arguments[0])=="number") { var tmpsum = arguments[0]; function tmpf(a) { tmpsum += a; return tmpf; } tmpf.toString = function(){ return tmpsum; } return tmp_f; } else if(Array.isArray(arguments[0])) { return arguments[0][0]+arguments[0][1]; } else { return "Error: 1 argument is neither int nor array"; } break; case(2): return arguments[0]+arguments[1]; break; default: return "Error: incorrect number of arguments"; break; } }