Guess four-bit digital intelligence game JavaScript source code implementation

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.