Javascript Array sortDescending()

Description

Javascript Array sortDescending()



Array.prototype.sortDescending = function () {
  return this.sort((a, b) => {
    if (a[1] > b[1]) {
      return -1//from ww  w.  j a  v  a 2 s  .c  om
    }
    if (a[1] == b[1]) {
      return 0
    }
    if (a[1] < b[1]) {
      return 1
    }
  })
}

Javascript Array sortDescending()

function getLargestPalindrome() {
  products = fillMultiples().sortDescending();
  for (i = 0; i < products.length; i++) {
    if (products[i].toString().isPalindrome()) {
      return products[i];
    }/*from   w w  w .  j  a v a  2s  . c  o m*/
  }
}

function fillMultiples() {
  var products = []
  for (i = 100; i < 1000; i++ ) {
    for (j = 100; j < 1000; j++ ) {
      products.push(i*j)
    }
  }
  return products
}

Array.prototype.sortDescending = function() {
  return this.sort(function(a,b) { return b - a })
}

String.prototype.isPalindrome = function() {
  return this == this.reverse() ? true : false
}

String.prototype.reverse = function() {
    return this.split('').reverse().join('');
}

console.log(getLargestPalindrome());



PreviousNext

Related