Hi, guys.
I’m having a problem with my code and it’s failing in the last test.
Would be happy if you help me out.
Great day.
function addTogether(){
if(arguments.length==2 && (typeof arguments[0]=='number' && typeof arguments[1]=='number'))
return arguments[0]+arguments[1]
else if(arguments.length==1 && typeof arguments[0]=='number')
return value=>value+arguments[0]
else
return undefined
}
addTogether(2,3);
Here’s the link.
Yeah, it’s failing because in the function you’ve returned (the return value => value+arguments[0]
bit) you aren’t error-checking what might get passed to THAT function for numeric value.
Expand your arrow function there just a little:
return (value) => {
if (/* check that value is valid here */){
// What do you think this should be for a valid value?
else {
// And what do you return for a non-numeric value?
}
}
Or, if you like the single-line syntax (in this case, makes my brain itch):
return (value) => typeof(value)=='number' ? value + arguments[0] : undefined;
My goodness, that’s ugly. To expand that out completely, so that it may make a little more sense:
// I'll define a function as a variable, so I can simply toss it around by name!
let myReturnFunc = function(value){
if (typeof value == 'number'){
return value + arguments[0];
} else {
return undefined;
}
// Now, in the event that we only got a single value in the first parens, we are going to
// return that function we just made.
return myReturnFunc;
}
I didn’t understand it properly.
Could you elaborate it for me, please?
You need to check if value
is a number or no (if not, return undefined)
2 Likes