re: Daily Challenge #29 - Xs and Os VIEW POST

FULL DISCUSSION
 

Javascript with no loops:

function XO(str) {
    let str_arr = str.toLowerCase()
        .replace(/[^xo]/g, '')
        .split('')    //Convert String into an Array to allow using Array.sort()
        .sort();

    //All of the o's will be placed before the x's thanks to Array.sort()
    //Add 1 to account for 0-indexing and to turn -1 into 0 when there are no o's
    let oCount = str_arr.lastIndexOf('o') + 1;
    let xCount = str_arr.length - oCount;

    return !(xCount - oCount);
}

//Run the test cases
const testCases = [
        "ooxx",
        "xooxx",
        "ooxXm",
        "zpzpzpp",
        "zzoo"
    ];
for(str of testCases) {
    let result = XO(str);
    console.log(`"${str}" => ${result}`);
}
code of conduct - report abuse