I am trying to count all the times the letter x and the letter o appear in a string and I'm returning true if they appear the same number of times and false otherwise.
I've completed the problem, but when using a ternary statement it forced me to create an extra condition I didn't need since you seem to need 3 operations in a ternary statement so I just counted every time something else appears in a string. Is there any way I can use a ternary statement without creating an unnecessary condition or would I have to use if statements?
function ExOh(str) {
var ex = 0
var oh = 0
var other = 0
for (var i = 0; i < str.length; i++) {
str[i] === "x" ? ex++ : str[i] === "o" ? oh++ : other++
}
return ex === oh
}
console.log(ExOh("xooabcx$%x73o"));
console.log(ExOh("xooxxo"));
console.log(ExOh("x"));
console.log(ExOh(""));
//wouldn't let me just have one statement after the ternary so I created the other variable.