...个4个长度的数组,每个位置装有0-9之间的随机数字,有10次输入的机会...
发布网友
发布时间:2024-10-10 04:18
我来回答
共2个回答
热心网友
时间:2024-10-10 06:28
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};
}
}
热心网友
时间:2024-10-10 06:25
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>猜数字</title>
<script>
var arr = [0,0,0,0];
var count = 0, right = 0;
function createData(){
count = 0;
document.getElementById('calNum').innerHTML = '';
document.getElementById('result').innerHTML = '';
for(var i=0;i<4;i++){
arr[i] = parseInt(Math.random()*10,10);
}
//下面是产生的随机数,可以打开让他显示,以便于进行测试
//document.getElementById('beginData').innerHTML = arr[0]+","+arr[1]+","+arr[2]+","+arr[3];
}
function startCal(){
if(count >= 10)return;
right = 0;
var data1 = document.getElementById('data1').value;
var data2 = document.getElementById('data2').value;
var data3 = document.getElementById('data3').value;
var data4 = document.getElementById('data4').value;
var msg = "";
if(data1 == arr[0]){
right++;
}
if(data2 == arr[1]){
right++;
}
if(data3 == arr[2]){
right++;
}
if(data4 == arr[3]){
right++;
}
count++;
document.getElementById('calNum').innerHTML = count;
document.getElementById('result').innerHTML = "正确:"+right+"个;错误:"+(4-right)+"个";
}
function IsDigit() {
if ((event.keyCode != 46) && (event.keyCode != 13)){
return ((event.keyCode >= 48) && (event.keyCode <= 57));
} else if (event.keyCode == 46){
return ((event.keyCode >= 48) && (event.keyCode <= 57));
}else{
return (event.keyCode);
}
}
function IsDigitUp() {
var arg0 = arguments[0];
var NumberRegExp = new RegExp("^\\d+$","g");
var myRegExp = new RegExp("[^0-9]+");
if (NumberRegExp.test(arg0.value) == false){
arg0.value = arg0.value.replace(myRegExp, '');
}
}
</script>
</head>
<body>
请输入四个整数(0-9)
<input type="text" id="data1" style="width:40px;" maxlength="1" onKeyPress="event.returnValue=IsDigit();" onkeyup="IsDigitUp(this);"/>
<input type="text" id="data2" style="width:40px;" maxlength="1" onKeyPress="event.returnValue=IsDigit();" onkeyup="IsDigitUp(this);"/>
<input type="text" id="data3" style="width:40px;" maxlength="1" onKeyPress="event.returnValue=IsDigit();" onkeyup="IsDigitUp(this);"/>
<input type="text" id="data4" style="width:40px;" maxlength="1" onKeyPress="event.returnValue=IsDigit();" onkeyup="IsDigitUp(this);"/>
</br>
<input type="button" onclick="startCal()" value="确认"/>
<input type="button" onclick="createData()" value="产生随机数"/>
</br>
猜的次数为:<span id="calNum"></span></br>
猜测结果:<span id="result"></span></br>
数据为:<span id="beginData"></span></br>
</body>
</html>