Question Bank

JavaScript X and O Balance: Two Approaches Quiz

Difficulty: Medium

Check whether a string contains an equal count of `x` and `o` (case-insensitive), two ways (regex match-array length and filter + length), plus companions on counting any pair of characters and treating empty input.

Question Bank
/

JavaScript X and O Balance: Two Approaches Quiz

JavaScript X and O Balance: Two Approaches Quiz

Check whether a string contains an equal count of `x` and `o` (case-insensitive), two ways (regex match-array length and filter + length), plus companions on counting any pair of characters and treating empty input.

Question Bank
Medium
JavaScript
4 questions
quiz
strings
string-manipulation
fundamentals

636 views

15

Implement hasEqualXO(str) using String.prototype.match with the /g and /i flags. Compare the lengths of the match arrays for x and o. Treat "no x and no o" as balanced (return true).

Examples

Example 1:

Input: 'xoxo'
Output: true
Explanation: 2 x's and 2 o's.

Example 2:

Input: 'loovex'
Output: false
Explanation: 2 o's but only 1 x.

Example 3:

Input: 'abc'
Output: true
Explanation: zero of each is still equal.