반응형
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:
- Check if 'str' contains 'o' and 'x' respectively.
- If included, assign it to the 'o' and 'x' variables.
- 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
}
반응형
'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글
Unique In Order (0) | 2022.02.26 |
---|---|
Find the next perfect square! (0) | 2022.02.25 |
Detect Pangram (0) | 2022.02.23 |
The Hashtag Generator (0) | 2022.02.22 |
Count characters in your string (0) | 2022.02.21 |