Tell us what’s happening:
Describe your issue in detail here.
I keep getting a reference error here:
ReferenceError: assignment to undeclared variable i
Not sure why I’m getting that, because it’s stopping me from seeing my console.log statement…so I can’t even check what it’s giving me because it seems like something may be wrong with my for loop. Not too confident in the code I have here either, feels like I’m missing a few things anyway.
Your code so far
function addTogether() {
if (arguments.length > 1) {
for (i = 0; i < arguments.length; i++) {
if (typeOf(arguments[i]) === "number") {
return i += arguments[i];
} else {
return undefined;
}
}
} else if (arguments.length === 1) {
arguments[0] += addTogether(arguments);
}
}
addTogether(2,3);
// addTogether(5)(7);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/113.0
Challenge: Intermediate Algorithm Scripting - Arguments Optional
Link to the challenge:
You didn’t declare i
in your for
loop.
typeOf
is (strangely) not the correct casing for this method.
Your code won’t pass yet but that clears up the syntax errors.
It can be tricky to wrap your head around this one but essentially you need to return a new function inside your current function, rather than returning the same function.
ohhh, okay, I’ll see if I can switch that up…and I see where I missed the “let” keyword lol…smh
Also, you’re returning out of the function inside your for
loop again.
If you want to check if all arguments are numbers, there are useful methods which can iterate for you (e.g. .some()
or .every()
).
1 Like
I tried the every function in an implementation of code for a different problem, but I think I need to look at some documentation for its proper usage again, because I think I’m fundamentally missing how to properly use it. Some works in the direct opposite way of Every, so if I need to look one up, the same is true of the other one. I’ll see about doing that and maybe trying to pseudo code the problem a bit better before tackling it a second time. Might need to post about it again later if it doesn’t work on the next official try lol.