1. Find the largest value in the array, arr = [7,4,8,1,5]
console.log(Math.max(7,4,8,1,5)); console.log(Math.max.apply(1,arr));
2. Encapsulate a function to generate n-bit random verification code
function randomCode(num) { var str = '0-9a-zA-Z'; var arr = ''; for (var i = 0; i < num; i++) { var n = Math.floor(Math.random() * str.length); arr += str[n]; } return arr; }
3. Please explain the DOM event flow? How to prevent event bubbling and event default behavior
Event flow: when an event occurs, the order in which events are transmitted between parent and child nodes is called event flow
Prevent event bubbling
event.stopPropagation ? event.stopPropagation() : event.cancelBubble = true;
Block event default behavior
event.preventDefault ? event.preventDefault() : event.returnValue = false;
4. Please explain the variable promotion?
The execution of js code is divided into at least two steps:
- Pre parsing: find var and function, bring the variable declared by var to the front, find the function declared by function, and store the whole function in memory
- Line by line parsing: from top to bottom
5. What are the string methods and their respective functions?
Length: length
Find: charAt, charCodeAt, indexOf, lastIndexOf
Interception: substring, slice, substr
Splitting: split
Replace: replace
To uppercase: toUpperCase
To lowercase: toLowerCase
Remove spaces: trim
Chinese sort: localeCompare
6. What are the array methods and their respective functions?
Stack: push, pop, unshift, shift
Universal: splice
Find: indexOf, lastIndexOf
Splicing: concat
Intercept: slice
Splice into string: join
Flip: reverse
Sorting: sort
Iteration: every, some, filter, map, forEach
7. How to create JavaScript objects?
- Literal creation
- new keyword creation
- Factory mode
- Constructor
- Prototype creation
- Blend creation
8. How many inheritance methods of JavaScript objects?
- Object oriented inheritance
- Prototype chain inheritance
- Object impersonation inheritance
- Combinatorial inheritance
- Parasitic combinatorial inheritance
9. What happened during new?
- Create an empty object
- Point this to an empty object
- Assign the prototype of the function to the prototype of the object__ proto__
- Point to the code in the function and add properties and methods
- Implicit return
10. Write the bubble sorting principle and implementation code in array sorting
Principle: compare two adjacent numbers. If the front one is larger than the back one, change the position
var arr = [99, 1, 46, 37, 33, 22, 11, 5]; for (var i = 0; i < arr.length; i++) { for (var j = 0; j < arr.length - i; j++) { if (arr[j] > arr[j + 1]) { var temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } console.log(arr);
11. Write out the selection sorting principle and implementation code in array sorting
Principle: compare each item of the array with each item after the current item. If the current item is larger than the latter item, swap positions
var arr = [99, 1, 46, 37, 33, 22, 11, 5]; for (var i = 0; i < arr.length; i++) { for (var j = i + 1; j < arr.length; j++) { if (arr[i] > arr[j]) { var temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } console.log(arr);
- Array de duplication
Method 1: the empty array determines whether each item in the arr exists in the empty array and whether it is appended to the empty array
var arr = [1, 2, 2, 3, 4, 1, 5, 2, 6, 8]; var brr = []; for (var i = 0; i < arr.length; i++) { if (brr.indexOf(arr[i]) == -1) { brr.push(arr[i]); } } console.log(brr);
Method 2: judge each item in the arr and each item after the current item. If they are equal, delete the following items
var arr = [1, 2, 2, 3, 4, 1, 5, 2, 6, 8]; for (var i = 0; i < arr.length; i++) { for (var j = j + 1; j < arr.length; j++) { if (arr[i] == arr[j]) { arr.splice(j, 1); j--; } } } console.log(arr);
14. There is such a string“ http://item.taobao.com/item.html?a=1&b=2&c=&d=xxx&e ", it is required to convert to {" a ": 1," B ": 2," C ":", "d": XXX, "e": undefined}
/* a:1 b:2 c:'' d:xxx e:undefined */ var str = "http://item.taonao.com/item.html?a=1&b=2&c=&d=xxx&e"; var arr = str.split('?')[1].split('&'); console.log(arr); // Create a new collection var obj = {}; for (var i = 0; i < arr.length; i++) { var a = arr[i].split('='); // Set value Object [variable] = Value; obj[a[0]] = a[1]; console.log(obj); }