1.打印乘法口訣表數組
for(var i = 1; i<= 9;i++){
for (var j = 1;j <= i;j++){
document.write(j + "*" + i + "=" +j*i+" " );
}
document.write("</br>");
}
document.write("</br>");排序
2排列一組數組(這裏用冒泡排序)it
var a=[9,8,12,3,6,4,6,7];
var b=0;
for(var i = 1;i < a.length;i++){
for(var j = 0; j < a.length-1;j++){
if(a[j] < a[j+1]){
b=a[j+1];
a[j+1]=a[j];
a[j]=b;
}
}
}
document.write(a);技巧
3折半查找(以練習2排列好的數組爲例)腳本
var num=6;
document.write("查找的元素:"+ num);
document.write("</br>");
var f = 0 ;
var l = a.length - 1;
var m = 0;
while(f <= l){
m = parseInt((f+l)/2);
if(num == a[m]){
document.write("有");
break;
}
else if (num < a[m]){
f = m + 1;
}
else{
l = m - 1;
}
}
halfsearch(a,num);while
寫腳本小技巧:想到什麼寫什麼,寫完後一點一點修改!document