Reputation: 99
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;
}
}
}
Upvotes: 0
Views: 5319
Reputation: 11
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;
}
}
Upvotes: 0
Reputation: 1
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
}
Upvotes: 0
Reputation: 337
You can use the Regex for finding those characters:
function XO(str) {
return str.match(/o/ig).length === str.match(/x/ig).length;
}
Upvotes: 0
Reputation: 22776
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;
}
Upvotes: 1
Reputation: 1
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
}
}
Upvotes: 0
Reputation: 1482
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.
Upvotes: 0
Reputation: 938
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'));
Upvotes: 1
Reputation: 10458
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
Upvotes: 0