System given a random four-digit number
such as 4589
For the first time, the player guesses like 4385. Then the system prompts 2A1B, indicating that the guessed number has two digits and the position is the same as the number given by the system, i.e. 4 and 8
1 B means there's a number you guessed right, but the position is wrong, that's 5.
According to this prompt, the player can make a second guess, the system will give a hint
... Until you guess the right position
Use JS to achieve the following source code:
var Forth=init.splice ((Math.floor (Math.random () *7)), 1);
var b=[one[0],two[0],three[0],forth[0]];
Console.log (b);//The answer is on the console! Don't tell him the small story.
function Guessnumber () {
var A=document.getelementbyid (' GetNumber '). Value;
var first=a.charat (0);//take string to specify position element
var second=a.charat (1);
var third=a.charat (2);
var forth=a.charat (3);
var c=[first,second,third,forth];//string to array
var counta=0;
var countb=0;
if (b.tostring () ==c.tostring ()) {alert ("Congratulations, you guessed it!") ");} Array equality must first be converted into a string
else{
The number of Var newarray=[c[0]-b[0],c[1]-b[1],c[2]-b[2],c[3]-b[3]];//statistics a
alert (NewArray);
for (Var i=0;i<4;i++) {
if (newarray[i]==0) {counta++;}
};
alert (count);
var sum=[c[0]-b[1],c[0]-b[2],c[0]-b[3],
C[1]-B[0],C[1]-B[2],C[1]-B[3],
C[2]-B[0],C[2]-B[1],C[2]-B[3],
C[3]-B[0],C[3]-B[1],C[3]-B[2]];
The number of for (Var j=0;j<12;j++) {//statistic B
if (sum[j]==0) {countb++}
}
}
var result=counta+ "A" +countb+ "B";
$ ("#tips"). Append ("
};
Guess four-bit digital intelligence game JavaScript source code implementation