JS - Combination based on MMDD

by Zacc206

JavaScript

// two tumblers
// maxValDay = 31
// maxValMonth = 12
// test duplicates in self
// test duplicates against 2 tumblers

// Add zero-padding function to all number objects in JS runtime (low risk)
Number.prototype.toPaddedString = function(){
    return this.toString().length > 1 ? this.toString() : "0" + this.toString();
}

// Init env vars
let tumblerMonth = 12,
    tumblerDay = 31;

// Iterate over months available
while(tumblerMonth > 0){
    logMonth(tumblerMonth);
    tumblerMonth--;
}

// Log all valid combos in a month based on unique digits in month + day combo
function logMonth(currentMonth){
    let currentDay = tumblerDay;
    
    while(currentDay > 0){
        logCombo(currentMonth, currentDay);
        currentDay--;
    }
}

// Only log if both pairs of digits are unique
function logCombo(month, day){
    if(valUniqueDigits(month, day)){
        console.log(month.toPaddedString() + day.toPaddedString());
    }
}

// Test if two numbers are both individually unique and unique pairs
// return false: all characters are not unique in both pairs
// return true: both pairs have unique numbers
function valUniqueDigits(num1, num2){
    if(!testSingleNumPair(num1) || !testSingleNumPair(num2)){
        return false;
    } else {
        if(!testDualNumPairs(num1, num2)){
            return false;
        } else {
            return true;
        }
    }
}

// Test a single pair of numbers to make sure they are unique
// return false: pair of digits has a reoccurring character (e.g. 11, 00, 22, 33, etc.)
// return true: pair of digits has no reoccuring characters (e.g. 12, 49, 29, 01, etc.)
function testSingleNumPair(num){
    let arr = num.toPaddedString().split('');
    if(arr[0] === arr[1]){
        return false;
    } else {
        return true;
    }
}

// Test two pairs of numbers to make sure they are unique
// return false: at least one number was reoccuring between the first and second arguments
// return true: all digits in both arrays are...