101 JS questions #5

by Arnaud Buchholz

HTML

<a href="http://blog.canumeet.com/2016/09/16/101-javascript-interview-question/"><h1>101 Javascript Interview Questions.</h1></a>
<h2>Question 5</h2>
Write a mul function which will produce the following outputs when invoked:
<hr>

JavaScript

function log (text) {
	var line = document.createElement("div");
  line.appendChild(document.createTextNode(text));
  document.body.appendChild(line);
  return line;
}

function mul (x) {
	var result = function (y) {
  	return mul(x * y);
  }
  result.toString = function () {
  	return x;
  }
  return result;
}

log("mul(2)(3)(4) = " + mul(2)(3)(4));
log("mul(4)(3)(4) = " + mul(4)(3)(4));
log("mul(2)(3)(4)(5) = " + mul(2)(3)(4)(5));