Sha256: 93f593956400640d6448022f5b82e06c35b622dfd52cb4758dfd0a01e4422680

Contents?: true

Size: 1.76 KB

Versions: 92

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 (!Array.isArray(palindromes[result])) {
          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

92 entries across 92 versions & 1 rubygems

Version Path
trackler-2.2.1.159 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.158 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.157 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.156 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.155 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.154 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.153 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.152 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.151 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.150 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.149 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.148 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.147 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.146 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.145 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.144 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.143 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.142 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.141 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.140 tracks/javascript/exercises/palindrome-products/example.js