Special Exercise 24

Table of contents

1. Multiple choice questions

    1. How many Bytes does the number in JavaScript occupy in computer memory?

    2. What will the following JS code output?

2. Programming questions

    1. Return the result of rounding down the number parameter in the form of a number


1. Multiple choice questions

1. How many Bytes does the number in JavaScript occupy in computer memory?

A、2 Byte

B、4Byte

C、8Byte

D、16Byte

Correct answer: C Your answer: A

Parse:

(1) Summary of some knowledge about numbers in Javascript

①In Javascript, due to the different variable contents, variables are divided into basic data type variables and reference data type variables .

  • Basic type variables use 8 bytes of memory to store values ​​of basic data types (numeric, boolean, null, and undefined)
  • Reference type variables only hold references to values ​​​​of reference types such as objects, arrays, and functions (that is, memory addresses)

②Numbers in JS are regardless of type, that is, there is no difference between byte/int/float/double, etc.

(2) Inside JavaScript, all numbers are stored as 64-bit floating point numbers , even integers


2. What will the following JS code output?
var a = 10; 
(function a() {
    a = 20;
    console.log(a); 
})()

A、10

B、20

C、undefined

D. Output the content of function a

Correct answer: D Your answer: B

Parse:

(1) There are variable promotion and function promotion in this question

①In the beginning, the variable a is promoted, and its value is undefined. Since the priority of function promotion is higher , a is first assigned as a function. is assigned a value of 10

(2) The function can access its own identifier (function name) in the function body

(3) Ordinary functions can modify variable names, but immediate execution functions cannot modify variable names

①Ordinary function:

function a(){
    a = 34;
    console.log(a)   // 打印结果是34
}
a() 

② Execute the function immediately:

(function a(){
    a = 34;
    console.log(a)  //打印结果是函数a
})()

(4) Detailed answers to the output results of different situations of the function

① Execute the function immediately [without parameters]: If there is a function name that is the same as the variable, then the function name cannot be modified in the function body

(function add(){
    add = 100;//此行代码作废,无效,因为此行代码的意思是要修改函数名,此处是立即执行函数,不允许被修改!
    console.log(add);
})();

② Execute the function immediately [without parameters]: If there is no function name same as the variable, then the variable value can be output normally

(function (){
    a = 100;// 此时的a成为全局变量,挂在到window上!
    console.log(a);//100
})();

 ③ Ordinary function [without parameters]: If there is a function name same as the variable, calling the function will report an error

var add = 1;
function add(){
    add = 2;
    console.log(add);//2
}
add();

 ④ Ordinary function [with parameters]: effective scope of variables

var a = 1;
function add(a=2){  
   console.log(a);//2  这地方的a在预解析变量声明提升处理完成后,最后一步会把同名的参数a的值2,赋值给变量a
   var a = 3;//该赋值仅在函数内有效
   console.log(a);//3  
}
add();
console.log(a)//1 

 ⑤ Ordinary functions [with function parameters]: functions that execute function parameters

<script>
    var a = 1;
    function add(a = 1, b = function () {
        a = 3;
        console.log(a);//3  修改的是参数体内的a!
    }) {
        b();
        console.log(a);//1 变量声明提升之后,被同名函数a覆盖,后又被同名参数a覆盖,最终值为1
        var a = 10; //该赋值仅在add()函数内有效
        console.log(a);//10    
    } 
    add();
    console.log(a)//1  最外层的a变量
</script>

2. Programming questions

1. Return the result of rounding down the number parameter in the form of a number

Parse:

(1) Math.floor() method, round down

<script>
    let number = 23.46
    function _floor(number){
        return Math.floor(number)
    }
    console.log(_floor(number));
</script>

(2) The Math.trunc() method removes the fractional part of the number and only keeps the integer part

<script>
    let number = 23.46
    function _floor(number){
        return Math.trunc(number)
    }
    console.log(_floor(number));
</script>

(3) split() method, string cutting

<script>
    let number = 23.46
    function _floor(number){
        let arr = number.toString().split('.')
        return +arr[0]
    }
    console.log(_floor(number));
</script>

(4) parseInt() method, convert to integer

<script>
    let number = 23.46
    function _floor(number){
        return parseInt(number)
    }
    console.log(_floor(number));
</script>

(5) ~~Twice bitwise inversion

<script>
    let number = 23.46
    function _floor(number){
        return ~~number
    }
    console.log(_floor(number));
</script>

(6) >> Right shift operation

<script>
    let number = 23.46
    function _floor(number){
        return number >> 0
    }
    console.log(_floor(number));
</script>

(7)

<script>
    let number = 23.99
    function _floor(number){
        return number-number%1
    }
    console.log(_floor(number));
</script>

Guess you like

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