@danedavid danedavid
have you figured it out yet?
am keen to get some feedback on my solution:
function addTogether(a,b) {
if(![...arguments].every(arg => typeof arg === "number")){
return
}
if(b){
return a + b
} else {
return function(y, x = a){
return addTogether(y, x)
}
}
}
FYI, Have a look at the [ā¦Spread Operator ] (Spread syntax (...) - JavaScript | MDN) instead of this piece of code
@TeeJay
try running this in your browser to further understand:
function outerFunction(a){
console.log(`${a}, this is from the outer function`)
// outerFunction returns another *anonymous/unnamed* function here, which we call with the ("Gday")
return function(x){
console.log(`${a}, this is from the middle function`)
console.log(`${x}, this is from the middle function`)
// middle function returns another *anonymous/unnamed* function here, which we call with the ("Gday")
return function(y){
console.log(`${a}, this is from the inner function`)
console.log(`${x}, this is from the inner function`)
// y (which is "Goodbye" as we passed the argument below) is the final return which
return y
}
}
}
console.log(
outerFunction("Hello")("Gday")("Goodbye")
)
As you can see, the arguments passed to the outer functions are all accessible by the inner/callback functions
not really, somehow, I passed the test⦠But still I dont knw watās wrong with the above codeā¦
try removing typeof undeclared
typeof returns a string, so you were returning a string saying
āundefinedā rather than actual
undefined ```
Thanks! And now I know how I got it right earlierā¦
I did
return und;
instead of your
return
Hey good point. addTogether(āstringā) returns undefined, so youāre essentially telling the computer to call undefined(3), so itās just saying undefined isnāt a function.
You can address that by having addTogether return a function which returns undefined rather than just returning undefined. Hereās my answer with this taken into account:
function addTogether() {
var args = Array.prototype.slice.call(arguments);
if (args.every(function(a){return typeof a === 'number'})) {
if (arguments.length > 1) {
return arguments[0] + arguments[1];
} else if (arguments.length === 1) {
var b = arguments[0];
return function(a) {
if (typeof a !== 'number' || typeof b !== 'number') {
return undefined;
}
return b + a;
};
}
} else {return function(){return undefined};}
}
addTogether('string')(3);
never mind, now it doesnāt pass the other tests lol, let me think on this oneā¦
function addTogether() {
var firstArgument = arguments[0];
if (typeof firstArgument != 'number' || arguments[1] && typeof arguments[1] != 'number' )
return undefined;
return arguments.length == 2? (firstArgument + arguments[1]) : function(x) {
return (typeof firstArgument == 'number' && typeof x == 'number')? firstArgument + x : undefined;
};
}
function addTogether() {
var arg1 = arguments[0];
var arg2 = arguments[1];
provideArg = function(arg){
return (typeof arg == typeof arg1) ? arg + arg1 : undefined;
};
var flag = typeof arg1 == typeof arg2;
if(arguments.length>=2 && flag ===true) return arg1 + arg2;
else if (arguments.length===1 && typeof arg1 != 'number') return undefined;
else if(arguments.length===1) return provideArg;
else return undefined;
}
hereās my solution
function addTogether() {
var args = Array.from(arguments)
for(let i=0; i<args.length; i++){
if(typeof args[i]!=="number"){
return undefined;
}
}
if(args.length > 1){
if(args.every(num=>typeof num === "number")){
return args.reduce((acc, next)=>acc+next,0)
}else{
return undefined
}
}
return function(arg2){
for(let i = 0; i<args.length; i++){
if(typeof args[i] === "number" && typeof arg2 === "number"){
return args[i] + arg2
}else{
return undefined
}
}
}
}
console.log(addTogether(2, 3));