周考模拟题
题目一:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ab140e07048af6cf935035129b4e7dfd.png)
题目二:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4f0f4eecb0e950a3acfaf9db35fd0488.png)
效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f0030adc124b18ae726d16e64e269435.png)
代码
function test6() {
for (var i = 1; i <= 3; i++) {
document.write(' <div style="border: 1px solid black ;float: left;width:100px;height:120px;margin:5px;">')
document.write(' <div style="background-color: red;width:100px;border: 1px solid black ;">' + i + '</div>');
for (var j = 0; j < i; j++) {
document.write('<div style="background-color: yellow;width:10px;margin:0 auto;">' + j + '</div>');
}
document.write('</div>');
}
}
题目三:
效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/228468ced05c533beb3e190899d22a98.png)
代码
function copy(yuanArr) {
var newArr = {};
if (Array.isArray(yuanArr)) {
newArr = [];
}
for (var i in yuanArr) {
if (typeof yuanArr[i] === 'object') {
newArr[i] = copy(yuanArr[i]);
} else {
newArr[i] = yuanArr[i];
}
}
return newArr;
}
var n = [[2, 3], [2, 3, 4, 5, 6], [6, 7, 8]];
var fuzhuArr = copy(n);
var yiweiArr = fuzhuArr.flat();
var quchong = [];
for (var i = 0; i < yiweiArr.length; i++) {
if (quchong.indexOf(yiweiArr[i]) == -1) {
quchong.push(yiweiArr[i]);
}
}
document.write(quchong);
document.write("<br>");
document.write(yiweiArr);
题目四
效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/626a3ad694494cb496fdfb777eb0768f.png)
代码
function test2() {
var str = 'i am the good boy';
var arr = str.split(" ");
var jg = "";
var nArr = arr.reverse();
for (var i = 0; i < nArr.length; i++) {
jg += nArr[i].substring(0, 1).toUpperCase() + nArr[i].substring(1) + " ";
}
document.write(jg);
}
题目五
要求
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/164c9ae562a0b5270c8c621d5c6bd1e6.png)
代码
function test4() {
var arr = [
[
{ "state": "true", "num": 20 }, { "state": "true", "num": 100 },
{ "state": "false", "num": 200 }, { "state": "false", "num": 80 },
{ "state": "true", "num": 30 }, { "state": "false", "num": 40 },
{ "state": "true", "num": 50 }, { "state": "false", "num": 120 }
],
[
{ "state": "true", "num": 300 }, { "state": "true", "num": 100 },
{ "state": "true", "num": 200 }, { "state": "true", "num": 80 },
{ "state": "false", "num": 30 }, { "state": "false", "num": 40 },
{ "state": "true", "num": 50 }, { "state": "true", "num": 120 }
]
]
var sum = 0;
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr[i].length; j++) {
if (arr[i][j].state == 'true') {
sum += arr[i][j].num;
}
}
}
console.log(sum);
}
题目六
要求
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/35d68c93354fa2881128a2c970234f20.png)
代码
function test5() {
var arr = ["abc123", "wwjj22", "123456", "manmen", "123hkd", "w_abde3"];
var xin = copy(arr);
console.log(arr);
console.log(xin);
var reg = /^[a-zA-Z]{1}[a-zA-Z0-9_]{4,6}[0-9]{1}$/;
for (var i = 0; i < arr.length; i++) {
if (reg.test(arr[i])) {
document.writeln(arr[i]);
}
}
}
题目七(回文数)
function huiwenshu(num) {
for (var i = 1; i < num; i++) {
if (i < 10) {
document.writeln(i);
} else {
var newNum = String(i).split('').reverse().join('');
if (newNum == i) {
document.writeln(i);
}
}
}
}
var num = Number(prompt('请输入一个数字做为范围'));
huiwenshu(num);
题目八(操作对象属性)
function duixiang() {
var arr = [
{ id: "600", pId: null, name: "图形的变化" },
{ id: "630", pId: "600", name: "投影与视图" },
{ id: "631", pId: "630", name: "投影" },
{ id: "632", pId: "630", name: "三视图" }
];
var str = "";
var str1 = [];
for (var i = 0; i < arr.length; i++) {
str += arr[i].id + " ";
var sz = [];
sz.push(arr[i].id);
str1.push(sz);
}
document.writeln(str);
document.writeln("<br>");
console.log(str1);
}
题目九(字符串反转并首字母大写)
function fanzhuang() {
var str = 'i am a good boy';
var arr = str.split(" ").reverse();
var str = "";
for (var i = 0; i < arr.length; i++) {
if (i == 0) {
str += arr[i] + " ";
} else {
str += arr[i].substring(0, 1).toUpperCase() + arr[i].substring(1) + ' ';
}
}
document.writeln(str);
console.log(str);
}
题目十(字符串转对象数组并排序)
function duixiang2(str) {
var arr = str.split('&');
var newArr = [];
for (var i = 0; i < arr.length; i++) {
var obj = {};
obj.name = arr[i].split(',')[0];
obj.age = arr[i].split(',')[1];
newArr.push(obj);
}
newArr.sort(function (a, b) {
return b.age - a.age;
})
return newArr;
}
var str = 'zhaoliying,29&yangmi,30&liudehua,50';
console.log(duixiang2(str));