Skip to content

Instantly share code, notes, and snippets.

@mojenmojen
Created September 24, 2016 03:26
Show Gist options
  • Save mojenmojen/2e8552f12cfcfffb9c3e2dd65f3c2e23 to your computer and use it in GitHub Desktop.
Save mojenmojen/2e8552f12cfcfffb9c3e2dd65f3c2e23 to your computer and use it in GitHub Desktop.
Codewars Kata: Check to see if a string has the same amount of 'x's and 'o's. The method must return a boolean and be case insensitive. The string can contains any char.
function XO(str) {
// make the string lowercase because we are case insensitive
str = str.toLowerCase();
// put the string into an array
var arrayOfCharacters = str.split("");
//count the x's
var countX = arrayOfCharacters.reduce( function( n, val ) {
return n + (val === 'x');
}, 0);
// count the o's
var countO = arrayOfCharacters.reduce( function( n, val ) {
return n + (val === 'o');
}, 0);
// do these numbers match? if so return true and if not return false
if ( countX == countO ) {
return true;
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment