js基础练习题--数组和对象
1、定义⼀个含有30个整型元素的数组,按顺序分别赋予从2开始的偶数;然后按顺序每五个数求出⼀个平均值,放在另⼀个数组中并输出
var evenArr = [];
var averageArr = [];
evenArr[0] = 2;
for (var i = 1; i < 30; i++) {
evenArr[i] = evenArr[i - 1] + 2;
}
document.write(evenArr);
var sum = 0;
for (var j = 0; j < evenArr.length; j++) {
sum += evenArr[j];
if ((j + 1) % 5 == 0) {
averageArr.push(sum / 5);
sum = 0;
}
}
document.write(averageArr);
2、设计⼀个程序,使⽤⼀个数组接收⽤户输⼊的7门课程的成绩,然后在页⾯上显⽰其总成绩和平均分,并列出⼩于60的成绩
var scoreArr = [];
var score = 0;
var sum = 0;js 二维数组
var str = '';
for (var i = 0; i < 7; i++) {
score = parseFloat(prompt("请输⼊您的成绩(150以内):"));
if (isNaN(score) || score > 150) {
alert("⾮法数字~");
break;
} else {
scoreArr[i] = score;
sum += score;
console.log(scoreArr);
}
}
document.write("总成绩:" + sum + "</br>");
document.write("平均分:" + sum / 7 + "</br>");
scoreArr.forEach(function (j, index) {
if (j < 60)
str += "第" + (index + 1) + "门功课:" + j;
})
document.write("</br>" + "不及格的成绩:" + str);
3、通过循环按⾏顺序为⼀个5×5的⼆维数组a赋1到25的⾃然数,然后在页⾯上输出该数组的左下半三⾓
var arr = [];
var num = 1;
for (var i = 0; i < 5; i++) {
arr[i] = [];//
for (var j = 0; j < 5; j++) {
arr[i][j] = num;
num++;
}
}
console.log(arr);
for(var i = 0; i< arr.length; i++) {
for(var j = 0; j<=i; j++) {
document.write(arr[i][j] + " ");
}
document.write("<br/>");
}
4、编写函数map(arr) 把数组中的每⼀位数字都增加30%
//forEach⽅法
var arr = [1, 2, 3, 4, 5];
var newArr = [];
arr.forEach(function (v, index) {
newArr.push(v + (v * 0.3));
});
console.log(newArr);
/
/  for循环⽅法
var arr = [1, 2, 3, 4];
function map(arr) {
for (var i = 0; i < arr.length; i++) {
arr[i] += arr[i]* 0.3;
}
return arr;
}
console.log(map(arr));
5.以下是某班级⼀次考试的成绩表。请计算每个学⽣总成绩,并按总成绩排名。统计各单科成绩第⼀名,输出其成绩与学号
var scoreArr = [{
sno: 1,
chinese: 105,
math: 62,
english: 118
},
{
sno: 2,
chinese: 89,
math: 78,
english: 120
},
{
sno: 3,
chinese: 86,
math: 64,
english: 80
},
{
sno: 4,
chinese: 78,
math: 99,
english: 91
},
{
sno: 5,
chinese: 107.5,
math: 97,
english: 70
},
{
sno: 6,
chinese: 112,
math: 61,
english: 92
},
{
sno: 7,
chinese: 101,
math: 79,
english: 104
},
{
sno: 8,
chinese: 71,
math: 72,
english: 105
},
{
sno: 9,
chinese: 56,
math: 68,
english: 61
},
{
sno: 10,
chinese: 98,
math: 83,
english: 77
}
];
var sum = 0;//总成绩
var maxChinese = 0;//语⽂单科成绩嘴⼤
var maxChineseSno = 0;//语⽂单科成绩最⼤的学⽣的学号
var maxMath = 0;
var maxMathSno = 0;
var maxEnglish = 0;
var maxEnglishSno = 0;
scoreArr.forEach(function (v) {
sum = v.chinese + v.math + v.english;
document.write("学号是 " + v.sno + " 的同学,总成绩为: " + sum + "</br>");
if (v.chinese > maxChinese) {
maxChinese = v.chinese;
maxChineseSno = v.sno;
}
if (v.math > maxMath) {
maxMath = v.math;
maxMathSno = v.sno;
}
if (v.english > maxEnglish) {
maxEnglish = v.english;
maxEnglishSno = v.sno;
}
})
document.write("语⽂单科成绩最⾼的是" + maxChinese + "学号为:" + maxChineseSno + "</br>");        document.write("数学单科成绩最⾼的是" + maxMath + "学号为:" + maxMathSno + "</br>");
document.write("英语单科成绩最⾼的是" + maxEnglish + "学号为:" + maxEnglishSno + "</br>");注:仅⽤作学习!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。