0

I hope everyone is having a good day.

This is my first ever post on Stackoverflow!

I have just completed the javascript course on codeacademy and have read a couple of books on it too. Now I am on codewars. I would classify myself as a beginner in Javascript.

I find myslef a little stuck on a challenge, please can someone shed some light on what I may be doing wrong? Many thanks in advance!

Here is the instructions:

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 contains any char.

And here is my code:

function XO(str) {
    var x = [];
    var o = [];

    for (var i = 0; i <= str.length; i++) {
        if (str(i).toLowerCase === "x") {
            x.push(i);
        } else if (str(i).toLowerCase === "o") {
            o.push(i);
        }

        if (x.length === o.length) {
            return true;
        } else {
            return false;
        }
    }
}
3
  • Move your length check out of the for loop Commented Aug 13, 2017 at 14:34
  • your codes are not good format yet Commented Aug 13, 2017 at 14:36
  • why not use inbuilt string functions ? Commented Aug 13, 2017 at 14:40

9 Answers 9

1

i corrected mistakes and use code comment to explain

function XO(str) {
    var x = [];
    var o = [];
    for (var i = 0; i < str.length; i++) { // i must be lower than length
        // str[i] or str.charAt(i), not str(i)
        if (str[i].toLowerCase() === 'x') { // toLowerCase is function, it must be called with toLowerCase()
            x.push(str[i]); // should push character
        } else if (str[i].toLowerCase() === 'o') {
            o.push(str[i]);
        }
    }
    // return statement must be located at the end
    if (x.length == o.length) {
        return true;
    } else {
        return false;
    }
}
console.log(XO('xo'));
console.log(XO('xxo'));
console.log(XO('xoX'));
console.log(XO('xoOX'));

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, and thank you everyone else who responded. I just wanted to also ask, what is the difference if i used [i] instead of (i)...I know [i] works in a way to go through the index of arrays but I get a bit confused when people use it in the (i) method. I know this may be a sily question to many but as mentioned I am just starting out in javascript. Thanks guys
Parenthesis are used for method calling. Like str[i].toLowerCase(). Here toLowerCase is a method. Angle brackets [] are used for accessing or changing the values in JSON or Array data objects. Both are totally different things.
1
function XO(str) {
    var x = 0, // numbers are better
        o = 0;

    for (var i = 0; i < str.length; i++) { // changed from '<=' to '<'
        if (str[i].toLowerCase() === "x") {
            x++;
        } else if (str[i].toLowerCase() === "o") {
            o++;
        }
    }
    return x === o;
}

Comments

0

str.match(/x/g).length==str.match(/o/g).length

Comments

0

function checkIfOequalsX(str){
  return str.match(/x/g).length==str.match(/o/g).length
}

console.log(checkIfOequalsX('xxooxo'));
console.log(checkIfOequalsX('xxooxoo'));

you can do it with

str.match(/x/g).length==str.match(/o/g).length

Comments

0

The third if else will never be executed because for a string there will be always a value.

If you want to return the count then the check of length should be performed after the for loop.

var xCount = 0; var oCount = 0;
for (var i = 0; i < str.length; i++) {
  if (str[i].toLowerCase() === "x") {
    xCount++;
  } else if (str[i].toLowerCase() === "o") {
    oCount++;
  }
}

return xCount === oCount;

About another solutions containing the check based on str.match method, the complexity of using that piece of code is twice compared to above because the str.match loop is performed twice to match both the strings.

6 Comments

This is the best answer since you only have to loop once.
the complexity is the same, if you loop once or twice. The number of instructions executed matters
As another answer already explained, there is another problem with <= str.length.
Sure but isn't the O time O(n) vs O(2n)?
You need to use toLowerCase() not toLowerCase, because the latter is a function code, while the former is the value that the function returns, in other words, str(i).toLowerCase === "x" is true for any character str(i).
|
0
function XO(str) {
  let strn = str.toLowerCase();
  let countX = []; 
  let countO = []; 
  for(let i=0; i<strn.length; i++) {
    if(strn[i] == 'x') {
      countX.push(strn[i])
    } else if(strn[i] == 'o') {
      countO.push(strn[i])
    }
  }

  if(countX.length == countO.length){
    return true
  } else if(countX.length !== countO.length) {
    return false
  }
}

Comments

0

You can use the Regex for finding those characters:

function XO(str) {
 return str.match(/o/ig).length === str.match(/x/ig).length;
}

Comments

0
function XO(str) {
   let letraO = 0
   let letraX = 0
   const myArray = str.toLowerCase();
   for (i=0; i<myArray.length; i++){
      myArray[i] === 'o'? letraO++ : letraX ++
   }
   return letraO===letraX? true : false    
}

Comments

0
function XO(str) {

    // make the string lowercase because we are case insensitive
    str = str.toLowerCase();

    // put the string into an array
    var arrayOfCharacters = str.split("");

    //count the x's
    var countX = arrayOfCharacters.reduce( function( n, val ) {
        return n + (val === 'x');
      }, 0);
      
    // count the o's
    var countO = arrayOfCharacters.reduce( function( n, val ) {
        return n + (val === 'o');
      }, 0);
    
    // do these numbers match? if so return true and if not return false
    if ( countX == countO ) {
      return true;
    } else {
      return false;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.