【面试题】JavaScript
第一题 合并
1 const a = { 2 name: "zhangsan", 3 age: 22 4 } 5 6 const b = { 7 name: "lisi", 8 age: 55 9 }
a,b对象,并且不改变a,b,把合并后的对象转为字符串打印到控制台
答案:
var c = {} c.a = JSON.parse(JSON.stringify(a)); c.b = JSON.parse(JSON.stringify(b)); var string = ""; function fn(object) { for (const key in object) { if (object.hasOwnProperty(key)) { const element = object[key]; if(typeof element == "object"){ fn(element); }else { string += "," + key + ":" + element } } } } fn(c); console.log(string.slice(1))
第二题 把setTimeout(()=>{return 3+4;})封装在Promise中并获得其值
1 new Promise((resolve, reject) => { 2 setTimeout(() => { 3 resolve(3 + 4); 4 }) 5 }).then((data) => { 6 console.log(data) 7 })
第三题 随机生成一个长度为 10 的整数类型的数组,例如 [2, 10, 3, 4, 5, 11, 10, 11, 20],将其排列成一个新数组,要求新数组形式如下,例如 [[2, 3, 4, 5], [10, 11], [20]]。
1 /** 2 * 一个产生10个长度随机整数数组的函数 3 */ 4 function getRandomInt() { 5 let arr = []; 6 for (let index = 0; index < 10; index++) { 7 arr.push(Math.round(Math.random() * 50)); 8 } 9 return arr; 10 } 11 12 let arr = getRandomInt(); 13 /** 14 * 数组去重 排序 15 */ 16 arr = Array.from(new Set(arr)).sort((a, b) => { 17 return a - b; 18 }); 19 20 /** 21 * 0-9一组 22 * 10-19一组 类推 23 */ 24 var result = []; 25 arr.forEach(function (val) { 26 let index = parseInt(val / 10); 27 if (!result[index]) { 28 result[index] = []; 29 } 30 result[index].push(val); 31 }) 32 console.log(result);
第四题 反转链表,每 k 个节点反转一次,不足 k 就保持原有顺序
1 // 创建节点类 2 class Node { 3 constructor(data) { 4 this.data = data; 5 this.next = null 6 } 7 } 8 // 创建链表类 9 class LinkList { 10 constructor() { 11 this.head = null; 12 this.tail = null; 13 this.length = 0; 14 } 15 // 追加节点 16 append(data) { 17 // 创建节点 18 let newNode = new Node(data); 19 if (this.head === null) { 20 this.head = newNode; 21 this.tail = newNode; 22 } else { 23 this.tail.next = newNode; 24 this.tail = newNode; 25 } 26 this.length++; 27 } 28 } 29 // 创建链表 30 function createLinkList(...arr) { 31 let list = new LinkList() 32 arr.forEach((item) => { 33 list.append(item) 34 }) 35 return list; 36 } 37 38 // 翻转head到tail之间的部分,不包括head和tail 39 // 返回原链表的第一个元素,也就是翻转后的最后一个元素 40 function reverseList(head, tail) { 41 if (head === null || head.next === null) return head; 42 // 从要反转的第一个节点开始 43 let current = head.next, 44 first = head.next; 45 let pre = head; // 这里就是翻转不包括head的原因 46 while (current !== tail) { 47 // 这里就是翻转不包括tail的原因 48 // 反转相邻两个节点的顺序 49 const next = current.next; 50 current.next = pre; 51 // pre和current依次后移一位 52 pre = current; 53 current = next; 54 } 55 // 拼接 56 head.next = pre; 57 first.next = current; 58 59 return first; 60 } 61 62 function reverseKGroup(list, k) { 63 if (list.head === null || k === 1) { 64 return head; 65 } 66 67 let cnt = 0; 68 const dummy = { 69 next: list.head 70 }; 71 // 在目标链表前设置一个虚拟节点 72 let start = dummy; 73 let end = list.head; 74 while (end !== null) { 75 cnt++; 76 // 不足k的链表不反转 77 if (cnt % k !== 0) { 78 end = end.next; 79 } else { 80 start = reverseList(start, end.next); 81 end = start.next; 82 } 83 } 84 return dummy.next; 85 }; 86 reverseKGroup(createLinkList(1,2,3,4,5,6,7,8,9,10,11),3)

浙公网安备 33010602011771号