初学jiavascript简单练习题
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排列一组数组(这里用冒泡排序)
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);
写脚本小技巧:想到什么写什么,写完后一点一点修改!