数组
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>数组</title>
<style type="text/css">
#box{
width: 200px;
height: 200px;
}
</style>
</head>
<body>
<div id="box">
</div>
<script>
//数组 属于引用类型,属于对象数据类型
//什么是数组
//就是用来存放数据结构的集合
//集合有长度,length
//集合有索引或者下标,数组的第一个元素用arr[0]
// 1.使用new关键字
var arr=new Array(); //创建一个空数组
var arr1=new Array("red","yellow","blue");
// 创建一个有数据的数组,每一个数据可以称为数组的元素
console.log(arr1);
var oBox=document.getElementById('box');
oBox.style.backgroundColor=arr1[0];
// 数组有长度
var arr2=new Array(10); //创建一个包含十个元素的数组,每项元素都是空的
console.log(arr2);
// 2.可以省略new关键字
var arr3=Array(); //空数组
arr3[0]="red";
arr3[1]="blue";
// 3.用自变量的形式定义数组方式
var arr4=["red","blue"];
console.log(arr4.length);
for (var i = 0; i < 4; i++) {
oBox.innerHTML+="<span>"+i+"</span>";
}
var oSpan=document.getElementsByTagName('span')
for(var j=0;j<oSpan.length;j++){
oSpan[j].style.color=arr4[j];
}
</script>
</body>
</html>
中等计算器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
.box input:nth-child(2n+1){
width: 100px;
}
.pox input{
width: 50px;
height: 50px;
margin: 12px;
}
</style>
</head>
<body>
<div class="box">
<input type="text">
<input type="button" value="+">
<input type="text">
<input type="button" value="=">
<input type="text">
</div>
<div class="pox">
<input type="button" value="+">
<input type="button" value="-">
<input type="button" value="*">
<input type="button" value="/">
<input type="button" value="%">
</div>
<script type="text/javascript">
// 先获取标签
var oInput = document.getElementsByTagName('input');
oInput[5].onclick=function(){
oInput[1].value=oInput[5].value;
}
oInput[6].onclick=function(){
oInput[1].value=oInput[6].value;
}
oInput[7].onclick=function(){
oInput[1].value=oInput[7].value;
}
oInput[8].onclick=function(){
oInput[1].value=oInput[8].value;
}
oInput[9].onclick=function(){
oInput[1].value=oInput[9].value;
}
oInput[3].onclick=function(){
if(oInput[1].value=="+"){
oInput[4].value=Number(oInput[0].value)+Number(oInput[2].value);
}else if(oInput[1].value=="-"){
oInput[4].value=oInput[0].value-oInput[2].value;
}else if(oInput[1].value=="*"){
oInput[4].value=oInput[0].value*oInput[2].value;
}else if(oInput[1].value=="/"){
oInput[4].value=oInput[0].value/oInput[2].value;
}else{
oInput[4].value=oInput[0].value%oInput[2].value;
}
}
</script>
</body>
</html>
for循环
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
// 需求:求出1-100之间偶数的和
// 2、4、6、8、10等等
// for四部曲
// 1、初始化变量 var i=0
// 2、条件判断
// 3、执行大括号中所有代码
// 4、条件更新
var num = 0; //先定义一个变量储存偶数的和
for(var i=0;i<=100;i++){
if(i%2==0){
num=num+i;
}
}
console.log(num);
</script>
</body>
</html>
逢七过游戏
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
// 需求:求出7的倍数打印出来
for(var i=1;i<=100;i++){
if(i%7==0 || i%10==7){
console.log(i);
}
}
</script>
</body>
</html>
双重for循环
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
/* 需求
⭐⭐⭐⭐⭐
⭐⭐⭐⭐⭐
⭐⭐⭐⭐⭐
⭐⭐⭐⭐⭐
⭐⭐⭐⭐⭐
*/
for(var i=1;i<=5;i++){ //大循环
for(var j=1;j<=5;j++){ //小循环
document.write("⭐");
}
document.write("<br>")
}
// 执行步骤:
/*
第一次大循环
i=1 i<=5条件成立 执行小循环
j=1 j<=5条件成立 document.write("⭐") j++
j=2 j<=5条件成立 document.write("⭐") j++
j=3 j<=5条件成立 document.write("⭐") j++
j=4 j<=5条件成立 document.write("⭐") j++
j=5 j<=5条件成立 document.write("⭐") j++
j=6 j<=5条件不成立 小循环结束
document.write("<br>") 添加一个换行 i++
i=2 i<=5条件成立 执行小循环
j=1 j<=5条件成立 document.write("⭐") j++
j=2 j<=5条件成立 document.write("⭐") j++
j=3 j<=5条件成立 document.write("⭐") j++
j=4 j<=5条件成立 document.write("⭐") j++
j=5 j<=5条件成立 document.write("⭐") j++
j=6 j<=5条件不成立 小循环结束
document.write("<br>") 添加一个换行 i++
i=3 i<=5条件成立 执行小循环
j=1 j<=5条件成立 document.write("⭐") j++
j=2 j<=5条件成立 document.write("⭐") j++
j=3 j<=5条件成立 document.write("⭐") j++
j=4 j<=5条件成立 document.write("⭐") j++
j=5 j<=5条件成立 document.write("⭐") j++
j=6 j<=5条件不成立 小循环结束
document.write("<br>") 添加一个换行 i++
i=4 i<=5条件成立 执行小循环
j=1 j<=5条件成立 document.write("⭐") j++
j=2 j<=5条件成立 document.write("⭐") j++
j=3 j<=5条件成立 document.write("⭐") j++
j=4 j<=5条件成立 document.write("⭐") j++
j=5 j<=5条件成立 document.write("⭐") j++
j=6 j<=5条件不成立 小循环结束
document.write("<br>") 添加一个换行 i++
i=5 i<=5条件成立 执行小循环
j=1 j<=5条件成立 document.write("⭐") j++
j=2 j<=5条件成立 document.write("⭐") j++
j=3 j<=5条件成立 document.write("⭐") j++
j=4 j<=5条件成立 document.write("⭐") j++
j=5 j<=5条件成立 document.write("⭐") j++
j=6 j<=5条件不成立 小循环结束
document.write("<br>") 添加一个换行 i++
i=6 i<=6条件不成立 大循环结束
*/
</script>
</body>
</html>
打印星星
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
/* 需求
⭐⭐⭐⭐⭐
⭐⭐⭐⭐
⭐⭐⭐
⭐⭐
⭐
*/
for(var i=0;i<5;i++){
for(var j=0;j<5-i;j++){
document.write('⭐');
}
document.write('<br>');
}
/* 需求
⭐
⭐⭐
⭐⭐⭐
⭐⭐⭐⭐
⭐⭐⭐⭐⭐
*/
for(var i=1;i<=5;i++){
for(var j=0;j<i;j++){
document.write('⭐');
}
document.write('<br>');
}
</script>
</body>
</html>
Math对象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
// Math 数学函数
// Math.min() 找出一组数组中的最小值
// Math.max() 找出一组数组中的最大值
// Math.floor() 向下取整
// Math.ceil() 向上取整
// Math.round() 四舍五入
// Math.random() 返回介于0和1之间的随机数,不包括0和1
console.log(Math.min(1,3,2,6,33,22,11))
console.log(Math.max(1,3,2,6,33,22,11))
console.log(Math.floor(1.9))
console.log(Math.floor(-1.9))
console.log(Math.ceil(1.1))
console.log(Math.ceil(-1.1))
console.log(Math.round(1.5))
// 需求:5-10
// 公式:Math.floor(Math.random() * (上限 - 下限 + 1)) + 下限;
console.log(Math.floor(Math.random()*(10-5+1))+5)
</script>
</body>
</html>
访问和修改数组元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
#box{
width: 200px;
height: 200px;
}
</style>
</head>
<body>
<div id="box">
</div>
<script type="text/javascript">
var arr1 = ['张三','李四',18,true];
// 访问数组中的元素
console.log(arr1[0])
console.log(arr1[4-2]) //访问索引为2的元素 18
// 修改数组中的元素
arr1[1] = '男'; //["张三", "男", 18, true]
arr1[1+2] = false; //["张三", "男", 18, false]
console.log(arr1)
</script>
</body>
</html>
添加和删除数组元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
#box{
width: 200px;
height: 200px;
}
</style>
</head>
<body>
<div id="box">
</div>
<script type="text/javascript">
var heroes = [];
// 通过索引去添加
heroes[0] = '孙悟空';
heroes[1] = '哪吒';
//使用 push() 方法在数组的末尾添加一个或多个元素
heroes.push('曹操'); //在数组末尾添加一个元素
heroes.push('刘备','孙策'); //在数组末尾添加多个元素
//在数组末尾添加一个元素给数组的长度索引赋值是一样的
heroes[heroes.length] = '韩信';
// 使用 unshift() 方法在数组的开始添加一个或多个元素
heroes.unshift('项羽');
heroes.unshift('刘邦','张良','萧何','马超');
// 通过改变数组长度去删除元素
// heroes.length = 5;
// 使用pop()方法在数组的结尾弹出一个元素
heroes.pop();
// 使用shift()方法在数组的开头弹出一个元素
heroes.shift();
// 使用 splice(开始删除的下标,删除的数量) 方法去删除数组中的某一段元素
// 开始删除的下标,包括自身
// heroes.splice(4,2)
// splice()方法还可以从截取的位置去添加一个或多个数组元素
heroes.splice(4,2,'你','我','他')
console.log(heroes)
// 截取的数组可以通过变量保存截取掉的数组,返回一个新的数组
// var arr1=heroes.splice(4,2,'你','我','他')
// console.log(arr1)
</script>
</body>
</html>