Programming Questions

  • Newest
  • Popular Tags
  • Ask A Question
  • Additive Persistence Problem
    Hello. I'm finding myself stuck on a problem, and this is probably an easy thing to fix for most, but I just can't figure out my mistake in this case. That in mind: if someone could please help me out, it would certainly save me hours more of struggle, and I'd really appreciate it. The Challenge: I'm trying to find the Additive Persistence of a number (ie, 4321 = 10 = 1, ergo its A.P. is "2", as this result required two iterations in order to transform the original number into a single digit.) The Problem: My code gets through two iterations (eg, 4-3-2-1=10=1), but then ends abruptly. It loads the recursive function one time, and on this second iteration, it fails after the for loop. The length is 1 so it should trigger the else statement, but does not. I checked and the length is indeed 1, yet the program fails to recognize this as I stated. However, the other problem is that if I were to enter in a large enough starting number to the point that it required more than 2 iterations to find the A.P. (eg, '9999999999999999999'), then it crashes on the first iteration. What am I doing wrong here? Please?
    var num = 9999999;
    //console.log(num);
    var counter = 0;
    //console.log(spl);
    
    function findAdd (num){
    counter++;
    var res = 0;
    var spl = num.toString(10).split("");
    console.log(spl);
    //console.log(spl.length + " len");
        //console.log(res +" res"); //=10
        //console.log(res.toString().length +" len"); //=2
                for (var n = 0; n < spl.length; n++) {
                    res += parseInt(spl[n],10);
                    console.log(res);
                }
                    if (res.toString().length > 1) {
                        //counter++;
                        console.log(counter + " counter");
                        findAdd(parseInt(res,10));
                    } else {
                        return counter + " FINAL ANSWER";
                    }
    }
    
    findAdd(num);
    nytek posted this question on 9/13/15 | javascript
    Answers
  • +
  • 4
  • -
  • The reason your code returns undefined for an A.P. greater than 1 is that you are not returning the recursive call to findAdd, thus, while the innermost iteration does in fact return the correct “FINAL ANSWER”, it only returns it into the parent function’s scope where nothing is done with it; it is not returned recursively down the call stack like you’re intending. To see that the innermost call is in fact returning the correct answer, try logging it to the console instead of returning it. There is one other problem, which is that your code returns the incorrect answer for inputs of A.P. 0. This is because you are incrementing counter before you check the length of your input. The easiest fix for this would be to move this check to the top, above counter++:
    function findAdd(num) {
        if (num.toString().length === 1) return counter;
        counter++;
        var res = 0;
        var spl = num.toString(10).split("");
        for (var n = 0; n < spl.length; n++) {
            res += parseInt(spl[n], 10);
        }
        return findAdd(parseInt(res, 10));
    }
    
    Two final recommendations: (1) you should really keep your counter variable within an enclosing function. You could either (1a)wrap the whole solution in another function, or (1b)add a counter parameter to findAdd, default it to 0 ( counter = counter || 0; ), and pass counter + 1 as a second argument to the recursive call. (2) You should really remove all the console.log’s and commented-out code when posting a question. That way the person answering doesn’t have to spend an extra minute cleaning it all up before they can see what’s going on :) Hope that helps!
  • +
  • 1
  • -
  • Aha! Thank you kindly! And also; my apologies: lesson learned, as well. Thanks again.
    Log in to write an answer.