Code Challenge: Triple Double

Share the love

Each week, I offer up a JavaScript code challenge. Want more? You can find others here.

Again, this week’s code challenge comes from my trusty favorite, Coderbyte:

Using the JavaScript language, have the function TripleDouble(num1,num2) take both parameters being passed, and return 1 if there is a straight triple of a number at any place in num1 and also a straight double of the same number in num2. For example: if num1 equals 451999277 and num2 equals 41177722899, then return 1 because in the first parameter you have the straight triple 999 and you have a straight double, 99, of the same number in the second parameter. If this isn’t the case, return 0.

Here are some test cases:

Input = 465555 & num2 = 5579 | Output = 1
Input = 67844 & num2 = 66237 | Output = 0

Here’s my solution collapsed:

[code language=”javascript” collapse=”true”]
function TripleDouble(num1, num2) {

// Split the numbers into arrays
var first = num1.toString().split(”);
var second = num2.toString().split(”);

// Loop over each item in the first array
for ( var i = 0; i < first.length; i++ ) {
var count = 0;
// Compare the current array item to other items in the first array
for ( var j = 0; j < first.length; j++ ) {
// If the items match, increment our counter
if ( first[i] == first[j] ) {
count ++;
};
};

// If we have a triple, let’s compare against the second array
if ( count >= 3 ) {
// Set a second counter for the second array
var dupe = 0;
// Loop over the second array
for ( var k = 0; k < second.length; k++ ) {
// If the item from the first matches the item in the second, increment dupe
if ( first[i] == second[k] ) {
dupe++;
};
};
// If dupe is greater than two, we have a double in the second array. Return 1.
if ( dupe >= 2 ) {
return 1;
};
};
};

// If we made it this far, everything failed. Return 0.
return 0;
};
[/code]

Full disclosure, this was done in one attempt. It’s not the cleanest code I’ve ever written, but it works (I think!).

First, we turn the numbers into strings and then split the strings into arrays so we can loop over them.

[code language=”javascript”]
var first = num1.toString().split(”);
var second = num2.toString().split(”);
[/code]

Next, we start a for loop to loop over the first array item:

[code language=”javascript”]
for ( var i = 0; i < first.length; i++ ) {
var count = 0;
for ( var j = 0; j < first.length; j++ ) {
if ( first[i] == first[j] ) {
count ++;
};
};
};
[/code]

Inside the for loop, we run another for loop to compare against other numbers in the first array. If the array items match, we increment the counter variable. This way, we can see if there are any doubles or triples.

Next, we put an if statement inside the for loop to check if the count variable is greater than or equal to three. If it is, we want to loop over the second array:

[code language=”javascript”]
if ( count >= 3 ) {
var dupe = 0;
for ( var k = 0; k < second.length; k++ ) {
if ( first[i] == second[k] ) {
dupe++;
};
};
if ( dupe >= 2 ) {
return 1;
};
};
[/code]

In the second array, we’re comparing the current item in the first array (first[i]) to the items in the second array (second[k]). If the items match, we increment our new counter variable dupe. After we’re done comparing the item to all items in the second array, we want to know if dupe is greater than or equal to 2. If it is, we’ve met our requirements (since we only enter this portion if count is at least 3). So, we can return 1.

If we make it to the very end, we can assume none of the conditions were true so we can return 0.

Share the love
Strategies on solving problems and wowing customers every Sunday 👉
Strategies for solving problems and wowing customers 👇