Tell us what’s happening:
Why it won’t return undefined when i test:
addTogether(2,[3]);
but the code works when it gets to the same point with console.log
Code still return undefined for
addTogether(2, “3”)
Your code so far
function addTogether() {
if (typeof arguments[0]=="number"){
if (arguments.length == 1){
let a = arguments[0]
return function(b){
return(a+b)
}
}
else if(typeof arguments[1]=="number") {
return(arguments[0]+arguments[1])
}
else {
return undefined}
}
else {return undefined}
}
addTogether(2,[3]);
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36.
Challenge: Arguments Optional
Link to the challenge:
Learn to code. Build projects. Earn certifications.Since 2015, 40,000 graduates have gotten jobs at tech companies including Google, Apple, Amazon, and Microsoft.
Hi and welcome to the forum.
Your code is pretty close.
The function that you return needs to check if b is a valid input.
hi thanks for pointing out my one mistake, i almost hardcoded them lol…
but the first part of else return undefined , it doesn’t work. when i change them to console.log(“test”), it was logged, have idea why? thanks for replying
well i just did what you told me and it works, care for explanation?
The last test case is checking that your returned function checks that the new argument is also a number.
That’s why addTogether(2)([3]) didn’t pass. Your function previously returned whatever JS computes 2 + [3] as instead of returning undefined.
1 Like