函数和数组

函数定义 function 函数名 ([参数1],[参数2]...){
代码实现
返回值 return:

return 值;
}

 

array 数组文档

数组的定义:一系列通数据类型的数据集合
1,2,3,4,5
“a”,“b”,“c”
先定义后赋值:var aa = new Attay();
aa[0] = 2;
aa[1] = 3;
定义并赋值:
var aa = new Array(1,2,3,4,5);

先定义后赋值:
var aa = [];
aa[0] = "aa"
aa[1] = "bb"

定义并赋值:

var aa = ["aa", "bb","cc","dd"];


常用方法:
length 长度
push 添加元素
concat 连接数组
join 数组转字符串
sort排序

var aa = ["aa","bb","cc","dd"];
var bb = ["ee","ff","gg","hh"]
aa.push("nn")
console.log(aa.concat(bb));
console.log(aa.join("-"));
console.log(aa.length);

var num = [1,5,6,9,4];
console.log(num.sort());

var a = ["cccc"]
for(var i = 0; i < a.length; i++){
document.write(a[i]);
}

 

输入语文数学英语的成绩,并计算最高分最低分平均分。

var score = [];
for(i = 0; i < 3; i++){
var num = prompt("请输入成绩");
score.push(num);
document.write("第" + (i+1) + "门成绩是" + num + "<br>")
}
var max = score[0];
var min = score[1];
var sum = 0;
for(i = 0; i < score.length; i++){
if(+max < score[i]){
max = score[i];
}
if(+min > score[i]){
min = score[i];
}
sum = +sum + +score[i];

}
document.write("最高分:" + max + "最低分:" + min + "平均分" + (sum/score.length));

posted on 2017-10-11 16:27  acaca  阅读(142)  评论(0编辑  收藏  举报