DEV Community

Discussion on: Daily Challenge #18 - Triple Trouble

alvaromontoro profile image
Alvaro Montoro • Edited


const tripleTrouble = (num1, num2) => {
  const min = Math.min(num1, num2);
  const strNum1 = num1.toString();
  const strNum2 = num2.toString();

  let found = 0;

  for (let x = 1; x < min && found === 0; x++)
    if (strNum1.indexOf(x*3) > -1 && strNum2.indexOf(x*2) > -1)
      found = 1;

  return found;

A quick solution using JavaScript, I need to improve it later. Live demo on CodePen.

alvaromontoro profile image
Alvaro Montoro

Hmmm.... reading other answers I might have misinterpreted what was required in this challenge. What is it exactly? For example, if num1 = 121 and num2 = 141, would they be triple troubled numbers? (because 7*3 = 21, and 7*2 =14) Or would it be num1 = 8777and num2 = 877 be triple troubled numbers? (because 7 repeats 3 times in the first one and 2 in the second one)

coreyja profile image
Corey Alexander

I'm getting caught up a few days behind, but I understood this is three digits in a row. Which I think is confirmed by the linked challenge examples.

So num1 = 8777 and num2 = 877 would return true for this!

Starting on my version now

Thread Thread
alvaromontoro profile image
Alvaro Montoro • Edited

To achieve that, the only change would be in the if statement. Instead of doing x*3 and x*2, it would be ''+x+x+x and ''+x+x respectively:

if (strNum1.indexOf(''+x+x+x) > -1 && strNum2.indexOf(''+x+x) > -1) {