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)