Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
You have already got an explanation for the error.
What I want to add, is that your function
letsGoo
will always overwrite the value of its paramter with1234
.When you firstly call
console.log(letsGoo())
, the number will be 1234. Sum will be 10, so inmain()
theelse
block will be used, which callsletsGoo(10)
.But then 10 is changed to
1234
right at the start of the function, so you end up with a recursive function that calls itself for infinite. That's why you get the Stack Overflow error, because the Call Stack can hold maximum ~10.000 calls (limited by JS engine).The second mistake is that the
sum
is initialized with 0 outside the function, and it gets bigger with everyletsGoo()
call, without reflecting the sum of the digits of the actual number during each call.It looks like this:
Also, look up on
.reduce()
method of the Array instance. Personally, I prefer using that.Same here, I either didn't know or forgot that I can use two ternary operators in one line.
I used an array too, and I agree that this looks better. No idea why I didn't think about it :(
This comment is hidden because it contains spoiler information about the solution