'Show only one item from the array
I have this little Pacman game made in JavaScript
Whats is happening is when i call a random item from the array this is what the game return: Video here
The game show all the items from the array, before display only one of then...
A part of the code
if (gameOver || gameWin) {
//display this array if game win
text = textOptionsWin[Math.floor(Math.random() * textOptionsWin.length)];;
if (gameOver) {
//display this array if game over
text = textOptions[Math.floor(Math.random() * textOptions.length)];;
//go back to the first level if lost
setTimeout(function () {
if (tileMap.fase == 1) {
location.href = "/?fase=1";
// text = "O COVID-19 TE PEGOU! 😷";
} else if (tileMap.fase == 2) {
location.href = "/?fase=1";
} else if (tileMap.fase == 3) {
location.href = "/?fase=1";
}
}, 2500);
//Delay to change the level
}
}
I'm missing something to this happen? any tip can be useful
Solution 1:[1]
I don't fully understand, but I think this is the answer:
var a = ["end1", "end2", "end3"]
console.log(a[Math.floor(Math.random() * 3)])
This prints a random item in the array 'a' to console.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kai Garcia Lorincz |
