Special Exercise 19

Table of contents

1. Multiple choice questions

    1. About JavaScript, among the following options, the wrong one is ()

    2. The following code will return:

    3. Execute the following code, the output result is ()

    4. For the code var a = 10.42; to take out the integer part of a, which of the following codes is correct?

2. Programming questions

    1. Reverse the parameter array and return


1. Multiple choice questions

1. About JavaScript, among the following options, the wrong one is ()

A. JavaScript is a dynamic type, weak type, prototype-based, literal scripting language

B. JavaScript is a multi-threaded language

C. JavaScript on the browser side includes ECMAScript, DOM objects and BOM objects

D. The JavaScript language can not only run in the browser environment, but also run on the server-side platform provided by node.js

Correct answer: B Your answer: A

Parse:

(1) The biggest feature of the JavaScript language is single-threaded . At a certain moment, only specific code can be executed.

(2) JavaScript is a language designed specifically for interacting with web pages and consists of three distinct parts:

  • ECMAScript, which provides core language features
  • Document Object Model (DOM), which provides methods and interfaces for accessing and manipulating web content
  • The Browser Object Model (BOM), which provides methods and interfaces for interacting with the browser

2. The following code will return:
Number(null);

A、Null

B、0

C、undefined

D、1

Correct answer: B Your answer: C

Parse:

(1) Number() can be used for conversion of any data type , null, empty, 0, Number() converts it to 0 by default

(2) The conversion rules are as follows:

①If it is a Boolean value, true and false will be converted to 1 or 0 respectively

②If it is a null value, return 0

③If it is undefined , return NaN

④ If it is a string , follow the following rules

  • If the string contains only numbers (including cases preceded by a positive or negative sign ), it will be converted to a decimal value, and the leading zeros will be ignored (for example, "011" will become 11)
  • If the string contains a valid floating-point format, convert it to the corresponding floating-point value , leading zeros are ignored
  • If the string contains hexadecimal format, convert it to a decimal integer of the same size; //number("01f")=31;
  • If the string is empty , convert it to 0 ; //number(" ")=0
  • If the string contains characters other than the above format, convert it to NaN //number("helloworld")=NaN

⑤ If it is a value , what is passed back

⑥ If it is an object , call  the valueOf()  method of the object, and then convert the returned value according to the previous rules. If the result of the conversion is NaN , call the object's toString()  method, and then convert the returned string value again according to the previous rules

(3) The following will be converted to 0

Number()
Number(0)
Number('')
Number('0')
Number(false)
Number(null)
Number([])
Number([0])

(4) Other conversion examples

console.log(Number(undefined));    //NaN
console.log(parseInt(""));        //NaN
console.log(parseInt(null));      //NaN
console.log(parseInt(undefined)); //NaN
console.log(null == 0); //false
console.log(undefined == 0); //false

(5) Note

        Don't confuse with the conversion of boolean(), in the conversion of boolean(), "", 0, -0, NaN, null, undefined will be converted to false, [] will be converted to true , but in number ( ) [ ] will be converted to 0


3. Execute the following code, the output result is ()
console.log(1);
let a = setTimeout(() => {console.log(2)}, 0);
console.log(3);
Promise.resolve(4).then(b => {
    console.log(b);
    clearTimeout(a);
});
console.log(5);

A、1 2 3 4 5

B、1 3 4 5

C、1 3 5 4

D、1 3 5 4 2

Correct answer: C

Parse:

(1) The then() method of the promise object is a microtask , while the setTimeout() timer function is a macrotask

(2) In terms of execution sequence processing, js will first execute all synchronous codes, then execute all microtasks in the microtask queue , and finally continue to execute macrotasks

(3) In this question, first execute the synchronization code and output 1 3 5, then execute the Promise.resolve().then() method, and output 4. Since the timer function is deleted in the then() method, it will not be output again 2. The final output is 1 3 5 4


4. For the code var a = 10.42; to take out the integer part of a, which of the following codes is correct?

A、parseInt(a);

B、Math.floor(a);

C、Math.ceil(a);

D、a.split('.')[0];

Correct answer: AB Your answer: ACD

Parse:

A: parseInt is converted to an integer, the default is decimal, and the result is 10

B: floor  is rounded down , the result is 10 

C: ceil  is rounded up , the result is 11

D: The operand of split must be a regular expression or a string , and the result is TypeError


2. Programming questions

1. Reverse the parameter array and return

Parse:

(1) reverse() reverse

<script>
    let arr = [12,23,34,45,56,67]
    function _reverse(arr){
        return arr.reverse()
    }
    console.log(_reverse(arr));
</script>

(2) Ordinary for loop, traverse the original array from back to front, and then push() to the new array

<script>
    let arr = [12,23,34,45,56,67]
    function _reverse(arr){
        let newArr = []
        for(let i = arr.length-1;i>=0;i--){
            newArr.push(arr[i])
        }
        return newArr
    }
    console.log(_reverse(arr));
</script>

(3) sort() compares the size. When the function is negative, a and b exchange positions, so when the return is less than 0 or -1, the reverse order can be realized

<script>
    let arr = [12,23,34,45,56,67]
    function _reverse(arr){
        return arr.sort((a,b)=>-1)
    }
    console.log(_reverse(arr));
</script>

(4) Double pointer for loop

<script>
    let arr = [12,23,34,45,56,67]
    function _reverse(arr){
        for(let i=0,n=arr.length-1;i<=n;i++,n--){
            [arr[i],arr[n]] = [arr[n],arr[i]]
        }
        return arr
    }
    console.log(_reverse(arr));
</script>

(5)while()

<script>
    let arr = [12,23,34,45,56,67]
    function _reverse(arr){
        let i = 0,n=arr.length - 1
        while(i++ <= n--){
            [arr[i],arr[n]] = [arr[n],arr[i]]
        }
        return arr
    }
    console.log(_reverse(arr));
</script>

Guess you like

Origin blog.csdn.net/qq_51478745/article/details/131534139