0

I'm having problem with the conditional always returning true. I'm not sure if it's the value it's seeing as truthy in general but it's messing with me.

Problem: 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
function XO(str) {
  let on = 0
  let xn = 0 

  let result = ""
  for (let i = 0; i <=str.length; i++) {
    if (str[i] = "x" || "X"){
      xn++
    } if (str[i] = "o" || "O") {

      on++
    };

    if (xn == on || xn && on == 0){
      result = true
    }else  if  (xn !== on) {
      result = false

    }

    return  result
  } 

}

Seems the conditional is always returning true. Not sure if it's because the types are true (which is why I kept it strict).

mousetail
  • 7,009
  • 4
  • 25
  • 45
TeeP
  • 9
  • 2
  • problem 1 ... `=` is **assignment** ... so `str[i] = "x"` ASSIGNS the value `"x"` to `str[i]` - and `"x"` is "truthy"... if you were to fix that by using `==` instead, then problem 2 .. `str[i] == "x" || "X"` is not how you check if `str[1]` is equal to `"x"` or `"X"` ... `str[i] == "x" || str[i] == "X"` is ... or `str[i].toLwoerCase() == "x"` of course would work – Jaromanda X Oct 20 '22 at 05:54
  • I've just changed it and I still have the same problems here is the new code ``` function XO(str) { let on = 0 let xn = 0 let result = "" for (let i = 0; i <=str.length; i++) { if (str[i].toLowerCase() == "x"){ xn++ } if (str[i].toLowerCase() == "o") { on++ }; if (xn == on || xn && on == 0){ result = true }else if (xn !== on) { result = false } return result } } ``` – TeeP Oct 20 '22 at 06:02
  • You probably meant to put `return result` outside the loop. Consider using correct indentation to spot those types of issues easier. – mousetail Oct 20 '22 at 06:12
  • `i <= str.length` is a very common off-by-one error. – Sebastian Simon Oct 20 '22 at 06:14

2 Answers2

2

You have 3 mistakes in your XO function.

  1. You have used assignment operator : = instead of equality comparison operator : ==.
  2. You are checking condition str[i] == "x" || "X". The correct way to write this is : str[i] == "x" || str[i] == "X".
  3. You are checking xn==on inside the for loop. You should check that once your for loop is over.

Here is the correct code -

function XO(str) {
    let on = 0
    let xn = 0

    for (let i = 0; i < str.length; i++) {
        if (str[i] == "x" || str[i] == "X") {
            xn++
            continue;
        } 
        if (str[i] == "o" || str[i] == "O") {
            on++
            continue;
        };
    }
    return xn==on;
}
Aman Mehta
  • 303
  • 1
  • 12
0

You just use toLowerCase() and at the end return if on===xn

function XO(str) {
  let on = 0;
  let xn = 0;
  for (let i = 0; i < str.length; i++) {
    if (str[i].toLowerCase() === "x") {
      xn++;
    }
    if (str[i].toLowerCase() === "o") {
      on++;
    };
  }
  return xn === on
}


console.log(XO("ooxx"))
console.log(XO("xooxx"))
console.log(XO("ooxXm"))
console.log(XO("zpzpzpp"))
console.log(XO("zzoo"))
brk
  • 48,835
  • 10
  • 56
  • 78