标签:
用数组解决王大爷养乌龟的问题:
var weights=[3,5,1,3.4,2,50];
var all_weight=0;
var avg_weight=0;
for (i=0;i<weights.length ;i++){
all_weight+=weights[i];
}
//如果想知道数据类型
//window.alert(avg_weight.constructor);
avg_weight=all_weight/weights.length;
document.writeln('乌龟的总体重是'+all_weight+" "+'平均体重是'+avg_weight.toFixed(2));
var myval=900;
function abc(val){
val=90;
}
abc(myval);
window.alert(myval);
var myarr=[456,50,90]
function abc2(arr){
arr[0]=35;
}
abc2(myarr);
window.alert(myarr);运行图:
案例:
var a=[23,'hello',4.4,'ture']; window.alert(a[3]); window.alert(a[4]);结论:不能访问不存在的元素。数组下标是从0开始编号的。
var a=[2,3];
alert('size='+a.length);
a[2]=56;//动态的增长
alert('size='+a.length);
window.alert(a[2]);
①
var str="hello world abc 顺平";
var arr=str.split(" ");
for (i=0;i<arr.length ;i++ )
{
document.write(arr[i]+" ")
}
document.write('</br>')②
var str="hello world abc 顺平";
document.write(str.split(" "));
标签:
原文地址:http://blog.csdn.net/lf1218/article/details/51207958