-
Notifications
You must be signed in to change notification settings - Fork 203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
day4 #19
Comments
输出数组中的最大值和最小值和平均值<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>14输出数组中的最大值和最小值和平均值</title>
<script>
/*
[10,2,8,55,32,18,9,11,30,16,19,20]
分别计算出以上数组的最大值,最小值和整个数组的平均值,并输出他们的索引
*/
// 遍历数组
var arr = [10,2,8,55,32,18,9,11,30,16,19,20];
// 先假设最大值和最小值
var maxIdx = 0;
var minIdx = 0;
var max = arr[0];
var min = arr[0];
var sum = arr[0];
for(var i=1;i<arr.length;i++){
// 找出最大值
// 遍历过程中当前数字大于max,则把当前数字赋值给max
if(arr[i] > max){
max = arr[i];
maxIdx = i;
}else if(arr[i] < min){
min = arr[i];
minIdx = i;
}
// 计算所有数值的和
sum += arr[i];
}
var average = sum/arr.length;
console.log(maxIdx,max);
console.log(minIdx,min);
console.log('平均值:',average);
</script>
</head>
<body>
</body>
</html> |
随机点名<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>03随机点名简单版</title>
<script>
window.onload = function(){
//0 - arr.length-1
var arr = ['何国荣','黄耀华','司徒健伟','洪斌','黄博霖','肖清琳','杜凡','方洋','顾伟淘','胡子荣','谢孟宁','蒋涛','陈志权','谢霖辉','谢坤裕','梁喜龙','雷燕豪','江启民','胡泽浩'];
var name = document.getElementById('name');
var btn = document.getElementById('btnDM');
btn.onclick = function(){
// 得到一个随机整数
var idx = parseInt(Math.random()*arr.length);
// 把取到的名字写入#name输入框
name.value = arr[idx];
}
}
</script>
</head>
<body>
<input type="text" id="name">
<button id="btnDM">随机点名</button>
</body>
</html> |
数组的复制<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>08数组的复制</title>
<script>
var num = 20;
var res = num;
num = 100;
// console.log(num,res);
// 数据类型
// 引用数据类型:数组,对象
var arr = [10,20,30];
var arr2 = arr;
arr.push(100);
console.log(arr2,arr===arr2);//[10,20,30]
// 复制一个数组
var arr = ['广州','深圳','北京','上海'];
var city = [];
// 遍历arr中的每一项,把每个城市写入city数组
for(var i=0;i<arr.length;i++){
city[i] = arr[i];
}
console.log(city,arr,arr===city);//false;
function show(ar){
// var ar = city
ar.push('杭州')
}
show(city);
console.log('city:',city);
</script>
</head>
<body>
</body>
</html> |
创建一个包含50个三位数的随机数组<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>创建一个包含50个三位数的随机数组</title>
<script>
var arr = [];
// for循环添加元素
// 100-999
var arr = [];
for(var i=0;i<50;i++){
var num = parseInt(Math.random()*(999-100+1))+100;
arr[i] = num;
// arr[arr.length] = num;
//arr.push(num);
}
console.log(arr);
</script>
</head>
<body>
</body>
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
冒泡排序
The text was updated successfully, but these errors were encountered: