<!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>
</body>
<script>
// 定义一个数组,筛选出能被2整除的数,构成一个新数组
var arr = new Array(8);
var newArr = [];
// 给arr数组初始化
for (var i = 0, j = 0; i < arr.length; i++) {
arr[i] = parseInt(prompt("请输入第" + (i + 1) + "个整数"));
// 筛选出能被2整除的数,给到newArr新数组
if (arr[i] % 2 == 0) {
newArr[j] = arr[i];
j++;
}
}
// 打印输出arr数组的元素到页面
for (var i = 0; i < arr.length; i++) {
document.write(arr[i] + "\t\t");
}
document.write("<br>");
// 打印newArr新数组的元素到页面
for (var i = 0; i < newArr.length; i++) {
document.write(newArr[i] + "\t\t");
}
</script>
</html>
js 定义一个数组,筛选出能被2整除的数,构成一个新数组
最新推荐文章于 2024-04-29 17:21:19 发布