function gameStart() {
var randomArray = getRandomArray();
var chance = 10;
while (chance--) {
var num = prompt("请输入一个四位数:");
var result = judge(num, randomArray)
if (result.status == true) {
alert("正确!游戏结束!");
break;
} else {
alert("错误!(正确" + result.correct + "个,错误" + result.wrong + "个)\n你还有" + chance + "次机会!");
}
}
}
function getRandomArray() {
var arr = []
for (var i = 0 ; i < 4 ; i++) {
arr.push(parseInt(Math.random() * 10));
}
// console.log(arr);
return arr;
}
function judge(num, randomArray) {
var correct = 0;
var wrong = 0;
for (var i = 0 ; i < 4 ; i++) {
if (parseInt(num[i]) == randomArray[i]) {
correct++;
} else {
wrong++;
}
}
if (wrong == 0) {
return {status: true};
} else {
return {status: false, correct: correct, wrong: wrong};
}
}