Surya
Surya

Reputation: 67

Return the sum of all palindromic numbers in an array

I'm trying to get the sum of the palindrome numbers in an array with javascript

The code gets outputs 0 instead of getting the sum of palindrome values:

function reverse(n) {
    var rem, res = 0
    while (n > 0) {
        rem = n % 10
        res = res * 10 + rem
        n = n / 10
    }
    return res
}

function isPalindrome(n) {
    if (n == reverse(n)) return true
}

function sumArray(arr) {
    var sum = 0
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] > 10 && isPalindrome(arr[i])) {
            sum += arr[i]
        }
        
    }
    console.log(sum);    
}


sumArray([12, 313, 11, 44, 9, 1])

Upvotes: 1

Views: 722

Answers (3)

KooiInc
KooiInc

Reputation: 122956

You can simplify the palindrome determination and use Array.reduce to determine the sum

const isPalindrome = n =>
  n > 10 && `${n}` === [...`${n}`].reverse().join("") ? n : 0;
const sumOfPalindromes = arr =>
  arr.reduce((acc, val) => acc + isPalindrome(val), 0);
console.log(sumOfPalindromes([12, 313, 11, 44, 9, 1]));

Upvotes: 1

Kenny Sexton
Kenny Sexton

Reputation: 383

The issue lies inside your reverse() function. n = n / 10 is leaving some messy decimals behind causing the loop to not terminate.

while (n > 0) {
    rem = n % 10
    res = res * 10 + rem
    n = n / 10
}

Just rounding off this result causes your code to run as expected.

while (n > 0) {
    rem = n % 10
    res = res * 10 + rem
    n = Math.round(n / 10)
}

Below is the code with that rounding change

  function reverse(n) {
    var rem, res = 0
    while (n > 0) {
      rem = n % 10
      res = res * 10 + rem
      n = Math.round(n / 10)
    }
    return res
  }

  function isPalindrome(n) {
    if (n == reverse(n)) return true
  }

  function sumArray(arr) {
    var sum = 0
    for (var i = 0; i < arr.length; i++) {
      if (arr[i] > 10 && isPalindrome(arr[i])) {
        sum += arr[i]
      }

    }
    console.log(sum);
  }

  sumArray([12, 313, 11, 44, 9, 1])

Upvotes: 1

CertainPerformance
CertainPerformance

Reputation: 371049

Your reverse function isn't implemented properly. Eg for 12 it gives Infinity. I think an easier approach would be to convert the number to a string first, then reverse the string:

function isPalindrome(n) {
    const str = String(n);
    return str === [...str].reverse().join('');
}

function sumArray(arr) {
    var sum = 0
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] > 10 && isPalindrome(arr[i])) {
            sum += arr[i]
        }
        
    }
    console.log(sum);    
}


sumArray([12, 313, 11, 44, 9, 1])
console.log(  313 +11+ 44);

or

function isPalindrome(n) {
    if (n < 10) return false;
    const str = String(n);
    return str === [...str].reverse().join('');
}

function sumArray(arr) {
  return arr
    .filter(isPalindrome)
    .reduce((a, b) => a + b, 0);
}


console.log(sumArray([12, 313, 11, 44, 9, 1]));
console.log(              313 +11+ 44);

Upvotes: 2

Related Questions