Comments (4)
输出数组中的最大值和最小值和平均值
<!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>
from cv.
随机点名
<!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>
from cv.
数组的复制
<!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>
from cv.
创建一个包含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>
from cv.
Related Issues (20)
- day2 HOT 10
- day3 HOT 5
- day4 HOT 5
- day6 HOT 8
- day12
- day13 HOT 2
- 正则表达式
- JavaScript工具函数大全(持续更新)
- JavaScript工具函数大全(持续更新) HOT 2
- 大厂面试题分享:如何让(a===1&&a===2&&a===3)的值为true? HOT 3
- 55 个提高你 CSS 开发效率的必备片段
- 继 NPM 被收购后,微软宣布推出 Github 口袋版!
- 给自己点时间再记记这200条Git命令 HOT 2
- 36个工作中常用的JavaScript函数片段
- try catch引发的性能优化深度思考
- 30 个极大提高开发效率的超级实用 VSCode 插件
- 大型前端项目如何实现 UI 可配置化
- 腾讯文档给 VSCode 贡献的 400 多行核心代码都干了什么
- 在腾讯这一年,坚守初心持续单纯 | 2021年终总结
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cv.