Learning Javascript functions, and having trouble with the problem below.
Modify this function so that if it’s called with 1 or 0 arguments, it returns null
.
function divide(num1, num2) {
return num1 / num2;
}
let num1 = null;
let num2 = null;
let answer = divide(1,1)
console.log(answer);
Learning Javascript functions, and having trouble with the problem below.
Modify this function so that if it’s called with 1 or 0 arguments, it returns null
.
function divide(num1, num2) {
return num1 / num2;
}
let num1 = null;
let num2 = null;
let answer = divide(1,1)
console.log(answer);
Share
Improve this question
edited Mar 1, 2019 at 13:46
Jon
asked Feb 9, 2018 at 19:54
JonJon
1031 gold badge1 silver badge6 bronze badges
3 Answers
Reset to default 8TL;DR
Assuming we only need to apply a straight forward modification, I would avoid too much syntactical sugar and just write it this way:
function divide(num1, num2) {
if (arguments.length < 2) {
return null;
}
return num1/num2;
}
If we want to make it simple and elegant, I would write it this way (requires ES6 features):
const divide = (...args) => args.length < 2 ? null : args[0] / args[1];
Explanation
Improvement steps in vanilla JS (ES5 and before)
- Using function arguments: this is simply an array like object that will magically appear inside your function, which contains the arguments you have passed to the function.
function divide(num1, num2) {
if (arguments.length < 2) {
return null;
}
return num1/num2;
}
While this is a great solution for the problem there are a downsides to it, if you wanted to switch to arrow function arguments
object doesn't appear.
- **Using ternary operator**you can farther more reduce the code by using the ternary
?
which is best suitable for simpleif
statements
function divide(num1,num2) {
return arguments.length < 2 ? null : num1 / num2;
}
Improvement steps in ES6
- Using spread (in this case it is actually called rest): the
...
can be used to either collect items into an array known as rest, or expand an array known as spread.
function divide(...args) {
return args.length < 2 ? null : args[0] / args[1];
}
JavaScript will collect all the arguments passed to the function and put them into an array called args
, I like this better since the reader can see where is args
defined.
- using arrow function: there are many difference between arrow and normal function, but many prefer it since it is shorter, and since we have a one-liner function why not use it.
const divide = (...args) => args.length < 2 ? null : args[0] / args[1];
On a final note all the previous solutions has a downside that we are only checking for length of arguments but not contents of arguments, lets assume that someone sent undefined
into one of the first two arguments, you'll have 2 arguments but one of them is kinda missing and you'll get NaN
since number of arguments is 2.
function divide(num1, num2) {
if (num1 === undefined || num2 === undefined) {
return null;
}
return num1/num2;
}
Demo
function divide1(num1, num2) {
if (arguments.length < 2) {
return null;
}
return num1/num2;
}
function divide2(num1,num2) {
return arguments.length < 2 ? null : num1 / num2;
}
function divide3(...args) {
return args.length < 2 ? null : args[0] / args[1];
}
const divide4 = (...args) => args.length < 2 ? null : args[0] / args[1];
const test = (cb) => {
console.log("-------->" + cb.name)
console.log(cb());
console.log(cb(1));
console.log(cb(1, 2));
console.log(cb(1, undefined));
console.log(cb(1, null));
console.log(cb(1, 2, 3));
};
test(divide1);
test(divide2);
test(divide3);
test(divide4);
Peep this.
function divide(num1, num2) {
if(arguments.length < 2) return null;
return num1 / num2;
}
The arguments object is available in all (non-arrow) function and represents all the arguments passed into the function. It has a length property that tells you how many arguments there are.
Technically passing in null would be calling it with two arguments so your example doesn't exactly represent that anyway. If you want an answer that's safe for arrow functions as well (they don't have the arguments
object) you could just check to see if the second argument is a number. This would catch a lot of it.
function divide(num1, num2) {
if (typeof num2 !== 'number') return null;
return num1 / num2;
}
If you wanted to handle the edge case that the first passed argument was undefined you could check both arguments.
function divide(num1, num2) {
if (typeof num1 !== 'number' || typeof num2 !== 'number') return null;
return num1 / num2;
}
If you want to get fancy it could be a one line arrow:
const divide = (num1, num2) => typeof num2 !== 'number' ? null : num1 / num 2;