9月28日
前言
这是根据对于学校学习知识的一次复习,可能有许许多多的问题,同时也不会做过多的注释,请各位大佬看看就好(顺便提提意见),有想一起交流经验的可以私聊(随缘更新)
什么是数组
04数组基本用法2
使用数组的步骤
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var scores=new Array();
scores[2]=89;
scores[4]=92;
scores[5]=78;
scores[7]=92;
console.log(scores);
for(var i=0;i<scores.length;i++){
console.log(scores[i]);
// alert(scores[i]);
}
var nums1=new Array();
for(var i=1;i<=100;i++){
if(i%3==0){
nums1[i]=i;
}
}
console.log(nums1);
var nums2=new Array();
var index=0;
for(var i=1;i<=100;i++){
if(i%3==0){
nums2[index]=i;
index++;
}
}
console.log(nums2);
var nums3=new Array();
for(var i=1;i<=100;i++){
if(i%3==0){
nums3[nums3.length]=i;
}
}
console.log(nums3);
</script>
</head>
<body>
</body>
</html>
05.创建数组的两种方式
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var nums=new Array();
var nums=new Array(3);
console.log(nums.length);
nums[3]=13;
nums[4]=25;
console.log(nums);
console.log(nums.length);
var names=new Array("tom","jack","alice");
console.log(names);
var hobbies=[];
hobbies[0]="吃饭";
hobbies[1]="打豆豆";
hobbies[2]="打豆豆";
var hobbies=["吃饭","睡觉","打豆豆"];
console.log(hobbies);
var i=0;
while(i<hobbies.length){
console.log(hobbies[i]);
i++;
}
for(var i in hobbies){
console.log(i+"="+hobbies[i]);
}
</script>
</head>
<body>
</body>
</html>
数组练习
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var lens=[8,4,2,1,23,344,12];
document.write("数组值为:");
for(var i in lens){
document.write(lens[i]+"\t");
}
document.write("<br />");
var sum=0;
for(var i in lens){
sum+=lens[i];
}
document.write("数值和为:"+sum+"<br />");
var max=0,min=0;
var temp;
for(var i =1;i<lens.length;i++){
if(lens[i]>lens[max]){
max=i;
}
if(lens[i]<lens[min]){
min=i;
}
}
temp=lens[0];
lens[0]=lens[max];
lens[max]=temp;
temp=lens[lens.length-1];
lens[lens.length-1]=lens[min];
lens[min]=temp;
document.write("排序后为:");
for(var i in lens){
document.write((lens[i]+"\t"));
}
</script>
</head>
<body>
</body>
</html>
作业题目
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var ans=parseInt(Math.random()*99+1);
for(var i=1;i<=5;i++){
var count=prompt("请输入第"+i+"次猜测的数字");
if(count>ans){
alert("猜测数值过大!");
}else if(count<ans){
alert("猜测数值过小!");
}else{
alert("恭喜你猜中了!中奖数字为"+ans+",总所用次数为"+i+"次");
break;
}
}
if(i==6){
alert("很遗憾没猜中,本次中奖数字为"+ans);
}
var scores=[0,1,2,3,4,5,6,7];
document.write(scores[0]);
for(var i=1;i<scores.length;i++){
document.write("-"+scores[i]);
}
document.write("<br />");
var scores2=[];
for(var i in scores){
if(scores[i]!=0){
scores2[scores2.length]=scores[i];
}
}
document.write("非零元素组成的数组为:");
for(var i in scores2){
document.write(scores2[i]+"\t");
}
document.write("<br />");
var scores3=[];
for(var i=scores.length-1;i>=0;i--){
scores3[scores3.length]=scores[i];
}
document.write(scores3);
document.write("<br />");
var fir=0,sec=0,thr=0;
var num=Number(prompt("请输入班级人数"));
for(var i=1;i<=num;i++){
var count=Number(prompt("请输入每位学员成绩"));
if(count>fir){
thr=sec;
sec=fir;
fir=count;
}else if(count>sec){
thr=sec;
sec=count;
}else if(count>thr){
thr=count;
}
}
document.write("第一名成绩为:"+fir+",第二名成绩为:"+sec+",第三名成绩为:"+thr);
</script>
</head>
<body>
</body>
</html>