Ad
  • Default User Avatar

    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 with 1234.

    When you firstly call console.log(letsGoo()), the number will be 1234. Sum will be 10, so in main() the else block will be used, which calls letsGoo(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 every letsGoo() call, without reflecting the sum of the digits of the actual number during each call.

    It looks like this:

    sum = 0;
    
    letsGoo('1234'); // sum = 10;
    letsGoo('10'); // sum = 10 + 1 + 0 = 11
    letsGoo('11'); // sum = 11 + 1 + 1 = 13
    letsGoo('13'); // sum = 13 + 1 + 3 = 17
    
    // and so on, it never gets smaller than 10.
    

    Also, look up on .reduce() method of the Array instance. Personally, I prefer using that.

  • Default User Avatar

    Same here, I either didn't know or forgot that I can use two ternary operators in one line.

  • Default User Avatar

    I used an array too, and I agree that this looks better. No idea why I didn't think about it :(

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution