Avatar of brunnock

brunnock's solution

to Palindrome Products in the JavaScript Track

Published at Nov 24 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

Detect palindrome products in a given range.

A palindromic number is a number that remains the same when its digits are reversed. For example, 121 is a palindromic number but 112 is not.

Given a range of numbers, find the largest and smallest palindromes which are products of numbers within that range.

Your solution should return the largest and smallest palindromes, along with the factors of each within the range. If the largest or smallest palindrome has more than one pair of factors within the range, then return all the pairs.

Example 1

Given the range [1, 9] (both inclusive)...

And given the list of all possible products within this range: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 15, 21, 24, 27, 20, 28, 32, 36, 25, 30, 35, 40, 45, 42, 48, 54, 49, 56, 63, 64, 72, 81]

The palindrome products are all single digit numbers (in this case): [1, 2, 3, 4, 5, 6, 7, 8, 9]

The smallest palindrome product is 1. Its factors are (1, 1). The largest palindrome product is 9. Its factors are (1, 9) and (3, 3).

Example 2

Given the range [10, 99] (both inclusive)...

The smallest palindrome product is 121. Its factors are (11, 11). The largest palindrome product is 9009. Its factors are (91, 99).

Setup

Go through the setup instructions for Javascript to install the necessary dependencies:

https://exercism.io/tracks/javascript/installation

Requirements

Install assignment dependencies:

$ npm install

Making the test suite pass

Execute the tests with:

$ npm test

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by changing xtest to test.

Source

Problem 4 at Project Euler http://projecteuler.net/problem=4

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

palindrome-products.spec.js

import generate from './palindrome-products';

describe('Palindrome', () => {
  test('largest palindrome from single digit factors', () => {
    const palindromes = generate({ maxFactor: 9 });
    const largest = palindromes.largest;

    expect(largest.value).toEqual(9);
    const containsMatch = [[3, 3], [1, 9]].filter(el => numericalArraysMatch(el, [3, 3])).length > 0;
    expect(containsMatch).toBe(true);
  });

  xtest('largest palindrome from double digit factors', () => {
    const palindromes = generate({ maxFactor: 99, minFactor: 10 });
    const largest = palindromes.largest;

    expect(largest.value).toEqual(9009);
    expect(largest.factors).toEqual([91, 99]);
  });

  xtest('smallest palindrome from double digit factors', () => {
    const palindromes = generate({ maxFactor: 99, minFactor: 10 });
    const smallest = palindromes.smallest;

    expect(smallest.value).toEqual(121);
    expect(smallest.factors).toEqual([11, 11]);
  });

  xtest('largest palindrome from triple digit factors', () => {
    const palindromes = generate({ maxFactor: 999, minFactor: 100 });
    const largest = palindromes.largest;

    expect(largest.value).toEqual(906609);
    expect(largest.factors).toEqual([913, 993]);
  });

  xtest('smallest palindrome from triple digit factors', () => {
    const palindromes = generate({ maxFactor: 999, minFactor: 100 });
    const smallest = palindromes.smallest;

    expect(smallest.value).toEqual(10201);
    expect(smallest.factors).toEqual([101, 101]);
  });
});

function numericalArraysMatch(a, b) {
  if (a.length !== b.length) {
    return false;
  }
  const one = [...a].sort(numericalSort);
  const two = [...b].sort(numericalSort);
  let result = true;
  let index = 0;
  while (index < one.length) {
    result = result && one[index] === two[index];
    index++;
  }
  return result;
}

function numericalSort(x, y) {
  if (x < y) {
    return -1;
  }
  if (x > y) {
    return 1;
  }
  return 0;
}
function generate(args) {
  const smallest={value:0};
  const  largest={value:0};
  for (let x=(args.minFactor || 1); x<=args.maxFactor; ++x) {
    for (let y=x; y<=args.maxFactor; ++y) {
      let prod=x*y;
      let prodStr=prod.toString();
      if (prodStr === prodStr.split('').reverse().join('')) {
	if ((smallest.value===0) || (smallest.value===prod)) {
	  smallest.value=prod;
	  smallest.factors=[x,y]
	} else if (prod > largest.value) {
	  largest.value=prod;
	  largest.factors=[x,y];
	}
      }
    }
  }
  return {largest, smallest};
}
module.exports=generate;

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?