I was making the number baseball game with javascript.
I got the random 3 numbers in my console based on the code I wrote.
And I want to use the function play for comparing the randomIndexArray
which shown by computer randomly and the user input value.
However, I could see any numbers in my console after I put the 3 numbers in the input box.
Also, I want to put numbers like 123 and show to the console in array like [1, 2, 3]
How can I solve this?
let inputNumber = document.getElementById("input-number");
let goButton = document.getElementById("go-button");
let userInput = document.getElementById("user-input")
let resultArea = document.getElementById("result-area");
let triesArea = document.getElementById("tries-area");
let gameOver = false;
goButton.addEventListener("click", play);
let randomIndexArray = [];
for (i = 0; i < 3; i++) {
randomNum = Math.floor(Math.random() * 10);
if (randomIndexArray.indexOf(randomNum) === -1) {
randomIndexArray.push(randomNum)
} else {
i--
}
};
console.log(randomIndexArray);
function play() {
let userValue = userInput.value;
let strike = 0;
let ball = 0;
for (let i = 0; i < randomIndexArray.length; i++) {
let index = userValue.indexOf(randomIndexArray[i]);
if (index > -1) {
strike++;
} else {
ball++;
}
}
}
play();
<div>number baseball game</div>
<div id="tries-area">tries: 5times</div>
<div id="result-area">result: 1strike 1ball</div>
<input id="user-input" type="number" placeholder="input 3 numbers among 0 to 9">
<button id="go-button">Go!</button>
Aucun commentaire:
Enregistrer un commentaire