Sha256: efeb8d9346e40b85d8b69fc71cb537f8311352d5872fefae3e102cc1595d4082

Contents?: true

Size: 1.76 KB

Versions: 255

Compression:

Stored size: 1.76 KB

Contents

'use strict';

module.exports = function Palindromes(options) {
  this.maxFactor = options.maxFactor;
  this.minFactor = options.minFactor || 1;

  this.generate = function() {
    var minFactor = this.minFactor;
    var maxFactor = this.maxFactor;

    var palindromes = [];
    var palindromeIndexes = [];

    for (var i = minFactor; i <= maxFactor; i++) {
      for (var j = minFactor; j <= maxFactor; j++) {

        var result = i * j;
        if ( ! this.isPalindrome(result) ) { continue; }

        var newFactor = [i,j].sort();

        if (palindromes[result] === undefined) {
          palindromes[result] = [];
          palindromeIndexes.push(result);
        }

        if ( ! arrayContainsArray(palindromes[result],newFactor) ) {
          palindromes[result].push(newFactor);
        }
      }
    }

    this.palindromes = palindromes;
    this.palindromeIndexes = palindromeIndexes;
  };

  this.largest = function() {
    var largestPalindrome = Math.max.apply(null,this.palindromeIndexes);
    var factors = this.palindromes[largestPalindrome];
    return { value: largestPalindrome, factors: factors };
  };

  this.smallest = function() {
    var smallestPalindrome = Math.min.apply(null,this.palindromeIndexes);
    var factors = this.palindromes[smallestPalindrome];
    return { value: smallestPalindrome, factors: factors };
  };

  this.isPalindrome = function(number) {
    var numberAsString = number.toString();
    var reversedString = numberAsString.split('').reverse().join('');
    return (numberAsString === reversedString);
  };
};

function arrayContainsArray(array,element) {
  var containsArray = false;

  for (var i = 0; i < array.length; i++) {
    if (array[i].join() === element.join()) {
      containsArray = true;
    }
  }

  return containsArray;
}

Version data entries

255 entries across 255 versions & 1 rubygems

Version Path
trackler-2.2.1.37 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.36 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.35 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.34 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.33 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.32 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.31 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.30 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.29 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.28 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.27 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.26 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.25 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.24 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.23 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.22 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.21 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.20 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.19 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.18 tracks/javascript/exercises/palindrome-products/example.js