Created
September 24, 2016 03:26
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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