Skip to content

Instantly share code, notes, and snippets.

@jonurry
Last active February 16, 2018 17:12
Show Gist options
  • Save jonurry/2adc03d943792fcb1619a69ec3af0bb4 to your computer and use it in GitHub Desktop.
Save jonurry/2adc03d943792fcb1619a69ec3af0bb4 to your computer and use it in GitHub Desktop.
3.1 Minimum (Eloquent JavaScript Solutions)
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
@jonurry
Copy link
Author

jonurry commented Feb 16, 2018

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 function min that takes two arguments and returns their minimum.

@jonurry
Copy link
Author

jonurry commented Feb 16, 2018

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