Git Product home page Git Product logo

Comments (4)

Wscats avatar Wscats commented on July 16, 2024

输出数组中的最大值和最小值和平均值

<!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.

Wscats avatar Wscats commented on July 16, 2024

随机点名

<!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.

Wscats avatar Wscats commented on July 16, 2024

数组的复制

<!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.

Wscats avatar Wscats commented on July 16, 2024

创建一个包含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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.