Last active
February 16, 2018 17:12
-
-
Save jonurry/2adc03d943792fcb1619a69ec3af0bb4 to your computer and use it in GitHub Desktop.
3.1 Minimum (Eloquent JavaScript Solutions)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function min(a,b) { | |
var result = b; | |
if (a < b) | |
result = a; | |
return result; | |
} | |
console.log(min(0, 10)); | |
// → 0 | |
console.log(min(0, -10)); | |
// → -10 |
Hints
If you have trouble putting braces and parentheses in the right place to get a valid function definition, start by copying one of the examples in this chapter and modifying it.
A function may contain multiple return
statements.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Functions
3.1 Minimum
The previous chapter introduced the standard function
Math.min
that returns its smallest argument. We can build something like that now. Write a functionmin
that takes two arguments and returns their minimum.