字符串、数组函数、分支和循环
作业标题:0709作业
作业内容:
- 实例演示常用的字符串和数组函数(自选5个即可)。
2.实例演示分支与循环,将课堂上列支的所有场景全部演示一遍。
字符串和数组函数
<!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>字符串方法</title>
</head>
<body>
<script>
//concat():字符串拼装
str = "html" + ", css" + ", js";
console.log(str);
str = "html".concat(", css", ", js");
console.log(str);
//slice():从子串
str = "Hello PHP.CN";
//slice():从子串
str = "Hello PHP.CN";
str.slice(0, 5);
//Hello
//Hello
//substr()
res = str.substr(0, 5);
console.log(res);
res = str.substr(-6, 3);
console.log(res);
let arr = [];
//push:在尾部添加
console.log(arr.push("a", "c", "d"));
console.log(arr);
//pop()从尾部删除
console.log(arr.pop());
console.log(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>分支、循环</title>
<!-- if elseif else,switch,三元运行? :,while, do while, for, for in对象 for of数组 -->
</head>
<body>
<script>
//单分支
let score = 39;
if (score >= 60) {
console.log("及格");
}
//双分支
score = 59;
if (score >= 60) {
console.log("及格");
} else {
console.log("不及格");
}
//多分支
score = 100;
if (score >= 60 && score <= 70) {
console.log("及格");
} else if (score >= 70 && score <= 90) {
console.log("不错");
} else if (score > 100 || score < 0) {
console.log("非法字符");
} else if (score >= 90 && score <= 100) {
console.log("优秀");
} else {
console.log("不及格");
}
//简化多分支
score = 50;
switch (true) {
case score >= 60 && score <= 70:
console.log("及格");
break;
case score >= 70 && score <= 90:
console.log("不错");
break;
case score > 100 || score < 0:
console.log("非法字符");
break;
case score >= 90 && score <= 100:
console.log("优秀");
break;
default:
console.log("不及格");
}
//while循环
arr = [1, 2, 3, 4, 5];
let i = 0;
while (i < arr.length) {
console.log(arr[i]);
i++;
}
//do while循环
arr1 = [1, 2, 3, 4, 5];
i = 0;
do {
console.log(arr1[i]);
i++;
} while (i < arr1.length);
//for循环
arr2 = [1, 2, 3, 4, 5];
for (let a = 0; a < arr2.length; a++) {
console.log(arr1[a]);
}
//循环对象
const user = {
id: 1,
name: "admin",
email: "admin@php.cn",
};
for (let key in user) {
console.log(user[key]);
}
//循环数组
let arr3 = [1, 2, 3, 4, 5, 6, 7, 8];
for (let value of arr3) {
console.log(value);
}
</script>
</body>
</html>