Javascript for Statement Question 5

Introduction

Create a function that returns the string "true" if there is an equal number of x's and o's.

For example, xooxxxxooxo = "false" - 6 x's vs 5 o's)

function exOh(str) {
    //your code here
}

// Output/*  w w  w  .  j a v  a  2 s .  c  om*/
console.log(exOh("xxoo"));
// => "True"

console.log(exOh("xxxo"));
// => "False"

console.log(exOh("Xylophone"));
// => "False"


function exOh(str) {
    var x = 0,
        o = 0;
    str = str.toLowerCase().replace(/[^xo]/g, "");

    for (var i = 0; i < str.length; i++) {
        if (str.charAt(i) === "x") {
            x += 1;
        } else {
            o += 1;
        }
    }

    return ((x === o) ? "True" : "False");
}

// Output
console.log(exOh("xxoo"));
// => "True"

console.log(exOh("xxxo"));
// => "False"

console.log(exOh("Xylophone"));
// => "False"



PreviousNext

Related