?3.While 练习
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let i=1
let sum=0
while(i<=100){
if(i%2===0){
sum+=i
}
i++
}
document.write(sum)
</script>
</body>
</html>
2.4循环退出
?
循环结束:
?
continue:结束本次循环,继续下次循环
?
break:跳出所在的循环
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let i
let caozuo
let yue=100
while (i!==4) {
caozuo=+prompt(`
欢迎登陆世界上最牛逼的ATM
请选择你的操作
1.存款
2.取款
3.查看余额
4.退出
`)
switch (caozuo) {
case i=1:{
cun=+prompt(`请输入存款金额:`)
yue+=cun
}
break;
case i=2:{
qu=+prompt(`请输入取款金额:`)
yue-=qu
}
break;
case i=3:{
alert(`你的余额为:${yue}`)
}
break;
case i=4:{
alert(`欢迎下次光临`)
}
break;
default:{
alert('看清楚再输入')
}
break;
}
}
</script>
</body>
</html>
?2.5?for 循环-基本使用
1. for循环语法
?2?for 循环嵌套
?案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
for (let i= 0; i<5; i++) {
for (let i= 0; i<5; i++) {
document.write('?')
}
document.write('<br>')
}
</script>
</body>
</html>
?3.for循环和while循环有什么区别呢:
?
当如果明确了循环的次数的时候推荐使用for循环
?
当不明确循环的次数的时候推荐使用while循环
?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
display: inline-block;
width: 100px;
height: 30px;
border-radius: 15px;
text-align: center;
border: 1px solid rgb(207, 116, 31);
line-height: 30px;
color: rgb(212, 42, 155);
margin-top: 10px;
background-color: rgb(95, 209, 209);
}
</style>
</head>
<body>
<script>
for (let i=1; i<=9; i++) {
for(let j=1; j<=i; j++) {
a=(` <div class="box">${j}x${i}=${i*j} </div> `)
document.write(a)
}
document.write('<br>')
}
</script>
</body>
</html>
三. 数组
1.1什么是数组?
数组(Array)是一种可以按顺序保存数据的数据类型
2.1数组的基本使用
?
?
?
?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let array=[2,6,1,77,52,25,7]
let max=array[0]
let min=array[0]
for (let i = 1; i < array.length; i++) {
if(max<array[i]){
max=array[i]
}
if(min>array[i]){
min=array[i]
}
}
document.write(`最大值是${max}`)
document.write(`最小值是${min}`)
</script>
</body>
</html>
3.1?操作数组
?
?
?
?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let array=[2,0,6,1,77,0,52,0,25,7,12,18]
let newarr=[]
let arr=[]
for (let i = 0; i < array.length; i++) {
if (array[i]>=10) {
newarr.push(array[i])
}
if(array[i]!==0){
arr.push=array[i]
}
}
document.write(newarr)
document.write(arr)
</script>
</body>
</html>
?
?
?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let array=[2,8,6,4,5,3,1,7]
for (let j= 0; j< array.length-1; j++) {
for (let i = 0; i < array.length-1-j; i++) {
if(array[i]>array[i+1]){
let tem
tem=array[i]
array[i]=array[i+1]
array[i+1]=tem
}
}
}
document.write(array)
</script>
</body>
</html>
?
?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
display: flex;
width: 700px;
height: 300px;
border-left: 1px solid pink;
border-bottom: 1px solid pink;
margin: 50px auto;
justify-content: space-around;
align-items: flex-end;
text-align: center;
}
.box>div {
display: flex;
width: 50px;
background-color: pink;
flex-direction: column;
justify-content: space-between;
}
.box div span {
margin-top: -20px;
}
.box div h4 {
margin-bottom: -35px;
width: 70px;
margin-left: -10px;
}
</style>
</head>
<body>
<script>
let arr=[]
for (let i = 1; i<=4; i++) {
let data=+prompt(`请输入第${i}季度的数据:`)
arr.push(data)
}
document.write(`<div class="box">`)
for (let i = 0; i < arr.length; i++) {
document.write(`<div style="height: ${arr[i]}px;">
<span>${arr[i]}</span>
<h4>第${i+1}季度</h4>
</div> `) }
document.write(`</div>`)
</script>
</body>
</html>