Functions/String

Hi everyone! Could someone help me to find the solution to this exercise?Create a function that takes a string, checks if it has the same number of 'x’s and 'o’s and returns either true or false.
Notes:

  • Return a boolean value (true or false).
  • The string can contain any character.
  • When neither an x nor an o is in the string, return true.
    Examples:
  • isEqualNumXandO(“ooxx”) ➞ true
  • isEqualNumXandO(“xooxx”) ➞ false
  • isEqualNumXandO(“ooxXm”) ➞ true (case insensitive)
  • isEqualNumXandO(“zpzpzpp”) ➞ true (returns true if no x and o)
  • isEqualNumXandO(“zzoo”) ➞ false */

I don’t understand what’s wrong with my code: thank you very much in advance

const countOccurrences2= (str,letter)=>{
        count=str.split(letter).length-1;
    }
const isEqualNumXandO=(str)=>{
        str=str.toLowerCase();
        if(countOccurrences2(str,"o")===countOccurrences2(str,"x") || (countOccurrences2(str,"o")===0 && countOccurrences2(str,"x")===0)){
            console.log("true");
        }else{
            console.log("false");
        }
        
    }
``

`isEqualNumXandO(“ooxx”);//true
isEqualNumXandO(“xooxx”);//true
isEqualNumXandO(“ooxXm”);//true
isEqualNumXandO(“zpzpzpp”);//true
isEqualNumXandO(“zzoo”);//true

Hi there,
The countOccurrences2 function isn’t returning anything, so calling the function leads to the result ‘undefined’ rather than a number. You just need to add ‘return count’ before you close the function.

:sweat_smile: :grinning:Thanks very much!

1 Like

No worries, it’s the easiest thing in the world to forget.