I’m cleaning up a game of tic-tac-toe I made a while back because I’m bored but I ran into a problem. The items are different because whenever I use .push or .splice on them it adds "" around the numbers. Can anyone help me fix this?
const winningCombos = ["123", "456", "789"];
const myCombos = [];
myCombos.splice(0, 0, "2");
console.log(myCombos);
myCombos.splice(0, 0, "1");
console.log(myCombos);
myCombos.push("3");
console.log(myCombos);
if (winningCombos[0] === myCombos[0]) {
console.log("Success")
} else {
console.log("Fail")
}
All I want it to do is it to end with the console saying Success and the numbers being equal to 123. Can anybody help me?
2
Answers
If you want
myCombos
to be an array similar towinningCombos
, you shouldn’t be splicing and pushing onto it. You should concatenate to the individual strings in the array.As you can see, with strings it’s not so easy to insert a new character in a specific place, because they’re immutable. It would be easier if you used a 2-dimensional array.
in the mind of a simple admirer of George Boole