-
Notifications
You must be signed in to change notification settings - Fork 0
Writing a function returns minimum value
Daisho Komiyama edited this page Apr 16, 2018
·
7 revisions
While reading Eloquent JavaScript, I was challenged an exercise.
Create a function which takes as many parameters as it can and returns minimum value among them. It is like writing Math.min()
from scratch. Sounded easy at first..
It was a lot harder than I had thought.
function superMin() {
var winner;
var i = 0;
var j = 1;
if (arguments[0] < arguments[1])
winner = arguments[0];
else
winner = arguments[1];
while (i < arguments.length) {
if (winner > arguments[j])
winner = arguments[j];
i++;
j++;
}
return winner;
}
myMathmin(2, 3, 1, -10);
// -10
Function above looks terrible for seasoned JS dev, but it gets the job done at least..