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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

๐Ÿ‘‹ Kindness is contagious

Please leave a โค๏ธ or a friendly comment on this post if you found it helpful!

Okay