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

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

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