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.180 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.179 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.178 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.177 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.176 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.175 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.174 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.173 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.172 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.171 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.170 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.169 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.167 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.166 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.165 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.164 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.163 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.162 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.161 tracks/javascript/exercises/palindrome-products/example.js
trackler-2.2.1.160 tracks/javascript/exercises/palindrome-products/example.js