** Post can also be found on virenb.cc **
Let's solve freeCodeCamp's intermediate algorithm scripting challenge, 'Arguments Optional'.
Starter Code
function addTogether() {
return false;
}
addTogether(2,3);
Instructions
Create a function that sums two arguments together. If only one argument is provided, then return a function that expects one argument and returns the sum.
For example, addTogether(2, 3)
should return 5
, and addTogether(2)
should return a function.
Calling this returned function with a single argument will then return the sum:
var sumTwoAnd = addTogether(2);
sumTwoAnd(3)
returns 5
.
If either argument isn't a valid number, return undefined.
Test Cases
-
addTogether(2, 3)
should return 5. -
addTogether(23, 30)
should return 53. -
addTogether(5)(7)
should return 12. -
addTogether("http://bit.ly/IqT6zt")
should return undefined. -
addTogether(2, "3")
should return undefined. -
addTogether(2)([3])
should return undefined.
Our Approach
After reading the instructions, starter code, and test cases more than once, this is what we're working with:
- We have at least one argument, a string or a number.
- We need to return a number or an
undefined
. - We need to evaluate the argument(s). Either sum it if there is more than one number, return undefined, or return a function.
So diving straight into this...we've worked with the arguments object in a previous challenge. Looking at the test cases, we deal with different arguments (different types, etc.).
We can set the argument(s) into a variable or variables, depending on how many. We can use destructuring.
function addTogether() {
let [arg1, arg2] = arguments;
console.log(`${arg1}, ${arg2}`);
}
addTogether(2, 3) // 2, 3
addTogether(2)(3) // 2, undefined
addTogether("http://bit.ly/IqT6zt") // ttp://bit.ly/IqT6zt, undefined
addTogether(2)([3]) // 2, undefined
So we are setting an undefined variable as arg2
. Reading back the instructions, we have to return a function if there is only one argument.
If only one argument is provided, then return a function that expects one argument and returns the sum.
Before writing that function, we should create a function to evaluate if the argument is a number. JavaScript has a built-in method to check this. It returns a true or false.
We can add a function like the below into addTogether()
.
function isNum(num) {
return Number.isInteger(num)
}
isNum(47) // true
isNum('47') // false
So we have our arguments variables and now a function to check if it is a number or not. From the instructions, if an argument is not a number, return undefined
.
function addTogether() {
let [arg1, arg2] = arguments;
function isNum(num) {
return Number.isInteger(num)
}
if (!isNum(arg1)) {
return undefined;
}
}
If we have two arguments which are both numbers, we can add them together. We can add that to an 'else if' statement.
function addTogether() {
let [arg1, arg2] = arguments;
function isNum(num) {
return Number.isInteger(num)
}
if (!isNum(arg1)) {
return undefined;
}
else if (isNum(arg1) && isNum(arg2)) {
return arg1 + arg2;
}
}
So if arg1
is not a number, we have it covered. If both arguments are numbers, we add them and have that case covered. If there is only one argument, what do we do?
If only one argument is provided, then return a function that expects one argument and returns the sum.
else if (!arg2) {
// return a function
}
We have to return a function that expects one argument and returns the sum.
else if (!arg2) {
return function(newArg) {
//
}
}
We have newArg
, we have to sum it with arg1
. But first, we must check if its a number.
else if (!arg2) {
return function(newArg) {
if (isNum(newArg)) {
return arg1 + newArg;
}
}
}
This if/else statement should be able to handle all of our test cases.
Our Solution
function addTogether() {
let [arg1, arg2] = arguments;
function isNum(num) {
return Number.isInteger(num);
}
if (!isNum(arg1)) {
return undefined;
}
else if (isNum(arg1) && isNum(arg2)) {
return arg1 + arg2;
}
else if (!arg2) {
return function(newArg) {
if (isNum(newArg)) {
return arg1 + newArg;
}
}
}
}
Links & Resources
'Arguments Optional' Challenge on fCC
Thank you for reading!
Top comments (3)
As you use Number.isInteger() , you'll miss float numbers.
You can use typeof / Object.prototype.toString.call() to detect number:
And , here another solution:
hi again, I made a correction to your code so it works:
function addTogether() {
let [arg1, arg2] = arguments;
function isNum(num) {
return Number.isInteger(num);
}
if (!isNum(arg1) || (arguments.length===2 && !isNum(arg2))) {
return undefined}
else if (isNum(arg1) && isNum(arg2)){
return arg1+arg2
}
else if (!arg2){
return function(newArg){
if(isNum(newArg)){
return arg1+newArg
}
}
}
}
hey, thank you for your full explanation and codes for all problems, your code here does not pass the last condition, addTogether(5, undefined) should return undefined.
can you explain?