Have the function ExOh(str) take the str parameter being passed and return the string true if there is an equal number of x's and o's, otherwise return the string false. Only these two letters will be entered in the string, no punctuation or numbers. For example: if str is "xooxxxxooxo" then the output should return false because there are 6 x's and 5 o's.
Enter a string of x's and o's in the field below..
$(document).ready(function() {
function ExOh(str) {
var xNum = 0,
oNum = 0,
len = str.length;
str = str.split('');
for (var i = 0; i < len; i++) {
if (str[i] === 'x') {
xNum++;
} else if (str[i] === 'o') {
oNum++;
} else return 'Bad string!';
}
if (xNum === oNum) return true;
return false;
}
$('.test').click(function() {
var string = $('.data input').val(),
message = '';
if (string === '') {
message = 'Please enter a string of x\'s and o\'s in the field.';
} else {
message = 'Number of x\'s and o\'s is equal: <span>' + ExOh(string) + '</span>';
}
$('.result p ').html(message);
});
});