Some common interview questions

Keywords: Javascript

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:

  1. 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
  2. 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?

  1. Literal creation
  2. new keyword creation
  3. Factory mode
  4. Constructor
  5. Prototype creation
  6. Blend creation

8. How many inheritance methods of JavaScript objects?

  1. Object oriented inheritance
  2. Prototype chain inheritance
  3. Object impersonation inheritance
  4. Combinatorial inheritance
  5. Parasitic combinatorial inheritance

9. What happened during new?

  1. Create an empty object
  2. Point this to an empty object
  3. Assign the prototype of the function to the prototype of the object__ proto__
  4. Point to the code in the function and add properties and methods
  5. 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);
  1. 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);
        }

Posted by shoz on Thu, 30 Sep 2021 15:27:54 -0700