8

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);

3 Answers 3

8

TL;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 simple if statements

function divide(num1,num2) {
  return arguments.length < 2 ? null : num1 / num2;
}

Improvement steps in ES6

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);

Sign up to request clarification or add additional context in comments.

3 Comments

this is interesting.. could you explain a little bit about how this works
thanks @Iwrestledabearonce. ,I've added an explanation
Thank you @oqx! Of your examples, the last one that does not check for length of arguments worked for the results I needed to avoid getting any NaN results.
2

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.

2 Comments

Answering the question it would be if (arguments.length < 2)
I would add a note that this doesn't work for arrow functions, as they don't expose an arguments object.
0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.