bgardne7
bgardne7

Reputation: 93

tryin to return Boolean instead receiving undefined

function XO(str) {
  var lowerStr = str.toLowerCase();
  letter_count(lowerStr, "x", "o")
  function letter_count(str, letter1, letter2) {
    var x_count = 0;
    for (var position = 0; position < str.length; position++) {
      if (str.charAt(position) == letter1) {
        x_count += 1;
      }
    }
    var o_count = 0;
    for (var position = 0; position < str.length; position++) {
      if (str.charAt(position) == letter2) {
        o_count += 1;
      }
    }
    if (x_count == o_count) {
      return true
    } else return false
  }
  
}

console.log(typeof(XO("xo")))

Counting the number of "x" and "o" occurrences and if they are equal in total amount then return true if not then return false. This is a codewars challenge that I'm having issues with. If I console.log(true) or console.log(false) then it will show true and false but I cannot get it to return as a Boolean.

Upvotes: 1

Views: 55

Answers (3)

user6386114
user6386114

Reputation:

Using spread syntax to convert string to array and using Array.prototype.forEach().

function XO(str) {
    let numberofX = 0,
        numberofO = 0;
    [...str].forEach(ch => {
        if (ch === "x") numberofX++;
        if (ch === "o") numberofO++;
    });
    return numberofX === numberofO;
}
console.log(XO("xo"));
console.log(XO("xoaaaaaox"));
console.log(XO("aa"));
console.log(XO("aao"));
console.log(XO("xoo"));

Upvotes: 0

Miu
Miu

Reputation: 844

Before I finished writing this answer, the question had already been solved. But please let me share my code as other examples :) I used regular expressions.


Issue

  • function XO() doesn't return anything

Conditional (ternary) operator

function XO(str) {
  return !/x/gi.test(str) || !/o/gi.test(str) ? false :
    str.match(/x/gi).length === str.match(/o/gi).length ? true : false;
}

console.log(XO("xo"));
console.log(XO("xoaaaaaox"));
console.log(XO("aa"));
console.log(XO("aao"));
console.log(XO("xoo"));


If statement

function XO(str) {
  // When 'X'=0 or 'O'=0
  if (!/x/gi.test(str) || !/o/gi.test(str)) {
    return false; 
  }
  // When the number of 'X' equals to that of 'O'
  else if (str.match(/x/gi).length === str.match(/o/gi).length) {
    return true;
  }
  // Others (when the number of 'X' is different from that of 'O')
  else {
    return false;
  }
}

console.log(XO("xo"));
console.log(XO("xoaaaaaox"));
console.log(XO("aa"));
console.log(XO("aao"));
console.log(XO("xoo"));

Upvotes: 0

Belhadjer Samir
Belhadjer Samir

Reputation: 1659

in your code you didn't return the result from XO function ,you just executed the function letter_count inside it ,it print undefined because you function is return an undefined it like it return nothing ,you can try this

function XO(str) {
  var lowerStr = str.toLowerCase();
  return letter_count(lowerStr, "x", "o")
  function letter_count(str, letter1, letter2) {
    var x_count = 0;
    var o_count = 0;
    for (var position = 0; position < str.length; position++) {
      if (str.charAt(position) == letter1) {
        x_count += 1;
      }
      if (str.charAt(position) == letter2) {
        o_count += 1;
      }
    }
    if (x_count == o_count) {
      return true
    } else return false
  }
  
}

console.log(XO("xo"))

Upvotes: 2

Related Questions