DEV Community

Cover image for JavaScript 🐲 challenges_5 βš”οΈ
Mahmoud EL-kariouny
Mahmoud EL-kariouny

Posted on

JavaScript 🐲 challenges_5 βš”οΈ

Exes and Ohs

  • 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 contain any char.

Example:

XO("ooxx")      => true
XO("xooxx")     => false
XO("ooxXm")     => true
XO("zpzpzpp")   => true // when no 'x' and 'o' is present should return true
XO("zzoo")      => false
Enter fullscreen mode Exit fullscreen mode

Task URL

My solution:

function XO(str) {

    let stringLower = str.toLowerCase();
    let charArr = stringLower.split('');
    let xoList = ['o', 'x'];
    let x = 0;
    let o = 0;

    let compare = charArr.filter((char) => {
        xoList.includes(char)
        if (char === 'x') return x += 1
        if (char === 'o') return o += 1
    });

    if (x !== o && compare !== []) {
        return false
    };
    if (compare !== [] || compare === []) {
        return true
    };

};
Enter fullscreen mode Exit fullscreen mode

Code Snapshot:

Image description

10 JavaScript Games to learn-Totally Cool & Fun πŸš€βœ¨πŸ”₯

πŸŽ₯

Connect with Me 😊

πŸ”— Links

linkedin

twitter

Top comments (0)