0

I have a task to make the function, which will find the next palidrome of the input value and return the object with will include the value and how many steps was made. I made the task, but now I need to make it via recursion. Can U help me, how to update the code. Thanks.

<script>

    polindrom = (num) => {
        const object = {
            'result': null,
            'step': 1
        }
        let reverse = Number(num.toString().split('').reverse().join(''));
        let sum = num + reverse;
        while (sum !== Number(sum.toString().split('').reverse().join(''))) {
            object.step++;
            sum = sum + Number(sum.toString().split('').reverse().join(''));
        }
        object.result = sum;
        return object;
    }

</script>
Mr. Oleg
  • 37
  • 3
  • Does this answer your question? [Recursive palindrome check with JavaScript](https://stackoverflow.com/questions/51567811/recursive-palindrome-check-with-javascript) – Rashomon Jan 25 '21 at 17:38
  • 1
    Show us what you've tried and where you're stuck. – eol Jan 25 '21 at 17:40

1 Answers1

0
function next_pal(num, count = 0) {
    if (isPal(num)) return {result:num, count};
    else {
        return next_pal(++num, ++count);
    }
}

function isPal(num) {
    let r = Number(num.toString().split('').reverse().join(''));
    if (r == num) return true;
    return false;
}

console.log(pal(1290)) // { result: 1331, count: 41 }
Karambit
  • 317
  • 4
  • 11