Tell us what’s happening:
Describe your issue in detail here.
I am not understanding why isn’t the ‘else’ parts executing when I call for addTogether(5) and addTogether(5, undefined) ?
Your code so far
function addTogether() {
let sum_result = sum(arguments[0], arguments[1]);
// console.log(sum_result);
// console.log(isNaN(sum_result));
if (typeof (sum_result) == 'number') {
return sum_result;
}
else if (arguments.length == 1) {
return sum;
}
else {
return undefined;
}
}
addTogether(2, 3);
function sum(param1, param2) {
return param1 + param2;
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36
Thank you. Made this change but my code isn’t passing for two test cases. I am not understanding the way the test cases are passed.
Why is 5 and 7 passed with 2 separate brackets in addTogether(5)(7) ?
The way that 5 and 7 are passed in addTogether(5)(7) is called “currying”.
You can think of addTogether(5) as a function itself and (7) as the argument being passed to that function because that’s exactly how you’re supposed to solve this:
addTogether(5) is supposed to return a function that can take another argument and then return a final sum when there are no more arguments to go.
Here is a good resource to learn more. Happy coding!
Yes. I made changes in the function to be returned but still that test case is not passing. I am not understanding what is actually asked to do. This is the updated code:
Could you please have a lot in these two code samples.
1st sample gives o/p as 5
2nd gives an error: SyntaxError: unknown: Unexpected token, expected “,” (22:27)
function addTogether() {
// function kay karta: sums two arguments.
// samajh ekach argument dili asel tar teh ek function return karaycha jechyaat ek parameter asel aani teh sum return karel.
if(arguments.length === 1) {
if(typeof(arguments[0]) === 'number' && !isNaN(arguments[0])) {
return sumOne(arguments[0]);
}
else if(typeof(arguments[0]) !== 'number') {
return undefined;
}
}
else if(arguments.length === 2) {
if(typeof(arguments[0]) !== 'number' || typeof(arguments[1]) !== 'number') {
return undefined;
}
else if(typeof(arguments[0]) ==='number' && !isNaN(arguments[0]) && typeof(arguments[1]) === 'number' && !isNaN(arguments[1])) {
return sumTwo(arguments[0], arguments[1]);
}
}
// return false;
}
addTogether(2,3);
function sumOne(param) {
return param;
}
function sumTwo(param1, param2) {
return param1 + param2;
}
console.log(sumOne(addTogether(5)));
Vs
function addTogether() {
// function kay karta: sums two arguments.
// samajh ekach argument dili asel tar teh ek function return karaycha jechyaat ek parameter asel aani teh sum return karel.
if(arguments.length === 1) {
if(typeof(arguments[0]) === 'number' && !isNaN(arguments[0])) {
return sumOne(arguments[0]);
}
else if(typeof(arguments[0]) !== 'number') {
return undefined;
}
}
else if(arguments.length === 2) {
if(typeof(arguments[0]) !== 'number' || typeof(arguments[1]) !== 'number') {
return undefined;
}
else if(typeof(arguments[0]) ==='number' && !isNaN(arguments[0]) && typeof(arguments[1]) === 'number' && !isNaN(arguments[1])) {
return sumTwo(arguments[0], arguments[1]);
}
}
function sumOne(arguments[0]) {
return param;
}
}
addTogether(2,3);
function sumTwo(param1, param2) {
return param1 + param2;
}
console.log(sumOne(addTogether(5)));