feat(web.test7): 新增 web 前端第 7 次实验
This commit is contained in:
129
Web/test7/index.html
Normal file
129
Web/test7/index.html
Normal file
@@ -0,0 +1,129 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Document</title>
|
||||
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<script>
|
||||
// 1
|
||||
console.log("=============== 1 ===============");
|
||||
let str1 = "1203def456";
|
||||
let num1 = parseInt(str1);
|
||||
console.log("num1: ", num1);
|
||||
console.log("num1 type: ", typeof num1);
|
||||
let str2 = num1.toString();
|
||||
console.log("str2: ", str2);
|
||||
console.log(`str: ${str2.match(0)}, index: ${str2.match(0).index}`);
|
||||
|
||||
// 2
|
||||
console.log("=============== 2 ===============");
|
||||
let scaleFactor = 2;
|
||||
const calculateArea = (width, height) => {
|
||||
let area = width * height;
|
||||
return area;
|
||||
}
|
||||
console.log("calculateArea(2 * scaleFactor, 5 * scaleFactor): ", calculateArea(2 * scaleFactor, 5 * scaleFactor));
|
||||
|
||||
// 3
|
||||
console.log("=============== 3 ===============");
|
||||
let sum = 0;
|
||||
for (let i = 1; i <= 5; i++) {
|
||||
sum += i;
|
||||
}
|
||||
console.log("1-5 sum: ", sum);
|
||||
|
||||
// 4
|
||||
console.log("=============== 4 ===============");
|
||||
const result = parseInt(prompt("请输入一个整数")) % 2 == 0 ? "该数是偶数" : "该数是奇数";
|
||||
console.log("判断奇偶: ", result);
|
||||
|
||||
// 5
|
||||
console.log("=============== 5 ===============");
|
||||
let arr1 = [1, 2, 3];
|
||||
arr1.push(4);
|
||||
console.log("arr1: ", arr1);
|
||||
let arr2 = ['apple', 'banana', 'orange'];
|
||||
arr2.pop();
|
||||
console.log("arr2: ", arr2);
|
||||
let arr3 = [1, 2, 3];
|
||||
arr3.shift();
|
||||
console.log("arr3: ", arr3);
|
||||
let arr4 = [1, 2, 3];
|
||||
arr4.unshift(0);
|
||||
console.log("arr4: ", arr4);
|
||||
let arr5 = [1, 2, 3, 4];
|
||||
let strJoin = arr5.join("-");
|
||||
let strSplit = strJoin.split("-");
|
||||
console.log(`arr5: ${arr4}, strJoin: ${strJoin}, strSplit: ${strSplit}`);
|
||||
let arr6 = ['A', 'B', 'C'];
|
||||
let arr7 = [true, false];
|
||||
console.log(`arr6: ${arr6}, arr7: ${arr7}, arr6.concat(arr7): ${arr6.concat(arr7)}`);
|
||||
|
||||
// 6
|
||||
console.log("=============== 6 ===============");
|
||||
let fruits = ['apple', 'banana', 'cherry'];
|
||||
for (let i = 0; i < fruits.length; i++) {
|
||||
console.log(`${i + 1}: ${fruits[i]}`);
|
||||
|
||||
}
|
||||
|
||||
// 7
|
||||
console.log("=============== 7 ===============");
|
||||
let person = {
|
||||
name: "John",
|
||||
age: 30,
|
||||
city: "New York"
|
||||
}
|
||||
for (let key in person) {
|
||||
console.log(`${key}: ${person[key]}`);
|
||||
}
|
||||
|
||||
// 8
|
||||
console.log("=============== 8 ===============");
|
||||
let currentDate = new Date();
|
||||
let year = currentDate.getFullYear();
|
||||
let month = currentDate.getMonth() + 1;
|
||||
let day = currentDate.getDay();
|
||||
let hh = currentDate.getHours();
|
||||
let mm = currentDate.getMinutes();
|
||||
let ss = currentDate.getSeconds();
|
||||
month = month < 10 ? "0" + month : month;
|
||||
hh = hh < 10 ? "0" + hh : hh;
|
||||
mm = mm < 10 ? "0" + mm : mm;
|
||||
ss = ss < 10 ? "0" + ss : ss;
|
||||
console.log(`${year}年${month}月${day} ${hh}:${mm}:${ss}`);
|
||||
|
||||
// 附加题 1
|
||||
console.log("=============== 附加题 1 ===============");
|
||||
let objArr = [
|
||||
{ name: "张三", age: 20 },
|
||||
{ name: "李四", age: 25 },
|
||||
{ name: "王五", age: 18 },
|
||||
]
|
||||
const matchObj = objArr.forEach((obj, index, objArr) => {
|
||||
if (obj.age > 20)
|
||||
console.log(`用户名: ${obj.name}\n用户年龄: ${obj.age}\n元素下标: ${index}`);
|
||||
})
|
||||
|
||||
// 附加题 2
|
||||
console.log("=============== 附加题 2 ===============");
|
||||
const constArr = [1, 2, 3, 4, 5];
|
||||
const newArr = constArr.map((value) => {
|
||||
return value ** 2;
|
||||
})
|
||||
console.log("newArr: ", newArr);
|
||||
|
||||
// 附加题 3
|
||||
console.log("=============== 附加题 3 ===============");
|
||||
let arrNew = new Array('hello', 'world', 'javascript', 'test', 'apple');
|
||||
let arrLengthT5 = arrNew.filter((value) => value.length > 5)
|
||||
console.log("长度大于 5: ", arrLengthT5);
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Reference in New Issue
Block a user