daco2020 2022. 2. 24. 09:55

Description:

 

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.



Examples input/output:

 

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




Solution:

 

  1. Check if 'str' contains 'o' and 'x' respectively.
  2. If included, assign it to the 'o' and 'x' variables.
  3. Returns the result by checking whether the variables 'o' and 'x' are equal.



function XO(str) {
  let o = 0 
  let x = 0

  for (const i of str) {
    if (['o','O'].includes(i)){
      o += 1
    }
    if (['x','X'].includes(i)){
      x += 1
    }
  }
  return o == x
}