Organize the document, loot out an instance code that uses dichotomy to find data in JavaScript, and take a note by the way
//Dialogue method to check data var arr=[41,43,45,53,44,95,23]; var b=44; var min=0; var max=; for(var i=1;i<;i++){ //The outer loop controls the number of sorting times for(var j=0;j<-i;j++){//The inner loop controls the number of loops if(arr[j]<arr[j+1]){ z=arr[j]; arr[j]=arr[j+1]; arr[j+1]=z; } } } // alert(arr[3]) while(true){ //Two-part search means first sorting a set of data in order, dividing the set of data into two from the middle to see which range you want to find the number in, and then dividing it into two until you find /* for(var i=0;i<;i++)*/ //Write this wrong? Until this number var zjs=parseInt((min+max)/2); //Because (min+max)/2 may be a decimal, add an integer to cast if(zjs==min){ When the array of numbers to be found is an array of even numbers, the number of numbers remaining is2When,Need to add a condition and make a judgment if(b=arr[zjs+1]){ alert(zjs+1); break; } } if(b==arr[zjs]){ alert(zjs) break;} else if(b>arr[zjs]){ min=zjs; } else{max=zjs}}
Find the total score for 10 scores, the highest score, the lowest score
var arr = new Array(80,70,86,58,90,35,89,67,50,100); var sum = 0; var maxd = 0; var mind = 100; //If you want to check the minimum number, it is best to use the maximum number of full scores as the base number that can be compared. for(var i=0;i<;i++){ sum = sum +arr[i]; if(arr[i]>maxd){ maxd = arr[i]; } if(arr[i]<mind){ mind = arr[i]; } } alert(sum); alert(maxd); alert(mind);
Add a non-repeat number to the array
//var a=7; var a=parseInt(prompt("Please enter a number")); var x=0; var arr=[1,2,3,4,5] for(var i=0;i<;i++){ if(a==arr[i]){ x=1; break;}} if(x==0){ (a)} alert()
I have never been exposed to similar questions before looking for data in dichotomy. I can't figure out the idea. After the teacher finished the lecture, he still seemed to understand it. It only then did he understand it after he typed it out. It seems easy to sort it in a bubble. But when I went to type the code myself, I made mistakes again. I can't always compare it with the same. I should have my own understanding. Why do I need to type like this? I type more code to avoid similar small mistakes in the future. Remember the variables you define, and echo the front and back. When you cannot define one, use another. When you use it, remember to add semicolons and braces.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.