Tell us what’s happening:
I didn’t do any research yet. Just tried to go off gut instinct. I was using if-else statements, although I think something much shorter should be performed. Not sure what yet. What I’m looking for is an indication of anything in my code that could be salvageable and carried over into subsequent drafts of the code.

Your code so far

function addTogether(a,b) {
let sum;
if(sum = a+b){
return sum
} else if(sum = a){
return addTogether;
} else if(typeof(a || b) != "number"){
return undefined;
}
}
addTogether(2,3);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36.

Looking at your code, my first question is have you written out your algorithm first before trying to the any code? If not, you need to think through the logic and make sure the steps you write out would work if you worked through any of the test cases.

I’m running into a problem with reduce.
I’m thinking it’s because I don’t have arguments in the right form to receive the reduce method.
I’ve tried it from 2 approaches:

I simply assigned variable args to arguments

I made args into an empty array and pushed arguments into it

Neither of them seem exactly right to me. The #1 approach doesn’t have arguments in the correct format and #2 Doesn’t place the array object in a functional relationship to the loop

Perhaps my use of reduce is totally off but I think it’s good. I welcome all challenges to my logic. I’m thinking I’m making a real bonehead mistake but can’t really see where it is.

Thank you.

function addTogether() {
/*
1. Use arguments object to obtain all perimeters for function, initialize args to contain arguments and sum variable
2. Run for loop on args
3. Within loop check if i is "number" If not return undefined
4. If i is "number", then return a sym of numbers using reduce method on args
*/
//1
let args = arguments;
let sum;
//2
for(let i = 0; i < args.length; i++){
//3
if(typeof (args[i]) != "number"){
return undefined;
//4
} else {
sum = args.reduce((acc, cum)=>{return acc+cum})
}
}
return sum;
}
addTogether(2)([3]);

I’m pretty confused right now. I don’t know what piece I’m missing.
Looking for some guidance.
1.My suspicions are that I am not using arguments object correctly. Perhaps I should be using a method on it?
2.Also, perhaps I should reorganize the order of my algorithm as it may be cancelling itself out in some places.
3.Also, perhaps I am not using recursion correctly?
Thanks for your assistance.

function addTogether() {
//Sum two arguments together
let values = [...arguments];
return values[0]+values[1];
//If there is only one argument, return a function that expects a single argument and return its sum
let first = values[0];
let moreValues = function(second){
return addTogether(first, second);
}
//Call on this function to return the sum
return moreValues;
//Check if argument is valid number, if not return undefined
if(values.every(value=>{
return typeof value === "number"
}))
{
return undefined;
}
}
addTogether(2,3);

I was able to solve this, but I want to make sure I’m explaining it correctly; that I’m understanding it correctly. The code is below with comments. If I am misunderstanding or explaining incorrectly what is happening, then please correct me. Thanks so much for your help!

function addTogether() {
let values = [...arguments];
//If arguments in values are not a valid number then return the argument that is a valid number; otherwise, return undefined.
if(!values.every(function(val){
return typeof(val)==="number"
})){
return undefined;
}
//If there are two arguments in “values” array then return the sum of those two arguments
if(values.length===2){
return values[0]+values[1];
}
//Otherwise, if there is only one argument in “values” array, then store the first value in a variable
else {
let first = values[0];
//Create a variable that holds a function containing a possible second argument as a perimeter
let addTwo = function(second){
//For this function, state that the original function contains the variables of two perimeters as a true
return addTogether(first, second);
}
//Run the variable containing call for the original function, which runs entire code applying newly applied perimeters of original function to the above code
return addTwo
}
}
addTogether(2,3);

If any value passed to the addTogether function is not a number, then return undefined. You could have written it slightly different and it would have been more readable in my opinion: