• JavaScript基础测试


    一、选择题

    1.var num = 10;下面那个是将num转换成布尔型的语法()

    A. !!num B. ||num C. bollean(num) D. number(num)

    2.var a=prompt("请输入数字“) ; 变量a的类型是( )

    A. 数字类型 B. 字符串类型 C. Bollean类型 D. 以上都不对

    3.var a = 123 ; var b = “123”; alert(a + b); 结果正确的是()

    A. 246 B. 123123 C.NaN D. 以上都不对

    4.var a = “123abc” ; var c = Number(a); alert©;正确的是()

    A. NaN B. 报错 C.123 D.以上都不对

    答案:ABBA
    1.A:true ;BCD报错 —> Boolean(bun);Number(num);
    2.typeof (a) —> ‘string’ ;
    3.123123 ;+ 进行拼接,- * 、 % 才会进行隐式转换;
    4.NaN;Number将对象的值转换为数字,若有非数字部分则返回NaN;

    二、简答题

    1.推断输出结果

    for(var i = 1; i < 10; i++){
        if(i % 2 == 0){
            i = i*2;
        }
    }
    console.log(i);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    // 答案:13
    i
    1
    2 --- 2*2=4 --- 4++=5
    5
    6 --- 6*2=12 --- 12++=13
    13 > 10,退出循环输出:13
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2.推断输出结果

    var num = 10;
    function fn(){
        num = 20;
        alert(num); // 不执行
    }
    alert(num); // 10
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3.推断输出结果

    function Foo(){
        var i = 0;
        console.log(i++);
    }
    var f1 = Foo(); // 0
    var f2 = Foo(); // 0
    
    f1;     // 报错:f1 is not a function
    f1();   // f1 is not a function
    f2();   // f2 is not a function
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    三、实操题

    1.用for循环写出九九乘法表

    for(var i = 1; i <= 9; i++){
        for(j = 1; j <= i; j++){
            document.write(j + "*" + i + "=" + i*j + " ");    
        }
        document.write("
    "
    ) }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    2.用函数写出数组冒泡排序

    var arr = [8,6,7,5,9,1,3,4,2];
    function arrSort(){
        for(var i=0; i<arr.length-1; i++){
            for(var j=0; j<arr.length-1-i; j++){
                if(arr[j]>arr[j+1]){
                    temp=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=temp;            
                }        
            }    
        }
    }
    //调用函数
    arrSort(arr);
    document.write(arr); // 1,2,3,4,5,6,7,8,9
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    四、域解析测试题

    第一题:

    f();
    function f(){
        console.log("1");
    }
    f();
    function f(){
        console.log("2");
    }
    f();
    function f(){
        console.log("3");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    // 浏览器内部执行的伪代码:
    function f(){
        console.log("1");
    }
    function f(){
        console.log("2");
    }
    function f(){
        console.log("3");
    }
    //域解析使得函数提升到代码前部,但前两个函数被第三函数所覆盖;
    //所以连续三次调用第三个函数,输出:333
    f();
    f();
    f();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    第二题:

    console.log(a);
    var a = 123;
    console.log(a);
    function f1(){
        console.log(a);
        var a = 456;
        console.log(a);
    }
    f1();
    console.log(a);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    // 浏览器内部执行的伪代码:
    var a;//域解析变量声明提前,不赋值
    console.log(a);//undefined
    a = 123;
    console.log(a);//123
    function f1(){
        var a;//域解析局部变量声明提前,不赋值
        console.log(a);//undefined
        a = 456;
        console.log(a);//456
    }
    f1();
    console.log(a);//输出全局变量a:123
    
    // 答案:undefined 123 undefined 456 123
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    第三题:

    console.log(a);
    var a = 123;
    console.log(a);
    var a = 456;
    console.log(a);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    // 浏览器内部执行的伪代码:
    var a;
    console.log(a);//undefined
    a = 123;
    console.log(a);//123
    a = 456;
    console.log(a);//456
    // 总结:变量和变量同名,解析之后只存在一个当前变量的声明。
    // 答案:undefined 123 456
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    第四题:

    console.log(a);
    var a = 123;
    console.log(a);
    function a(){}
    console.log(a);
    function a(){}
    console.log(a);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    // 浏览器内部执行的伪代码:
    function a(){};
    console.log(a); // 函数体f
    var a = 123;
    console.log(a); // 123
    console.log(a); // 123
    console.log(a); // 123
    // 总结:函数和变量同名,函数声明提升,忽略变量声明。
    // 答案:函数体f 123 123 123
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    第五题:

    console.log(a);
    var a = 12;
    function fn(){
        console.log(a);
        var a = 13;
    }
    fn();
    console.log(a);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    // 浏览器内部执行的伪代码:
    var a;
    console.log(a);//undwfined
    a = 12;
    function fn(){
        var a;
        console.log(a);//undefined
        a = 13;
    }
    fn();
    console.log(a); //全局变量a:12
    
    // 答案:undefined undefined 12
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    第六题:

    console.log(a);
    var a = 12;
    function fn(){
        console.log(a);
        a = 13;
    }
    fn();
    console.log(a);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    // 浏览器内部执行的伪代码:
    var a;
    console.log(a);//undefined
    a = 12;
    function fn(){
        console.log(a);//全局a:12
        a = 13;//修改全局变量a
    }
    fn();
    console.log(a);//a被修改为13
    
    //答案:undefined 12 13
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    第七题:

    var foo = 1;
    function bar(){
        if(!foo){
            var foo = 10;
        }
        console.log(foo);
    }
    bar();//10
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    // 浏览器内部执行的伪代码:
    var foo = 1;
    function bar(){
    	//无论if条件是否成立,都要进行变量提升
        var foo;//!undefined = true
        if(!foo){
            foo = 10;
        }
        console.log(foo); // 10
    }
    bar();
    
    // 答案:10
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    第八题:

    var n = 13;
    function fn(n){
        alert(n);
        var n = 14;
        alert(n);
    }
    fn(n);
    console.log(n);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    // 浏览器内部执行的伪代码:
    var n = 13;
    function fn(n){//形参也是变量-----局部变量
        alert(n);//13
        var n = 14;
        alert(n);//局部变量,输出:14
    }
    fn(n);//实参也是变量
    console.log(n);//全局变量,输出:13
    
    // 答案:13 14 13
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    第九题:

    console.log(a, b, c);
    var a = 10, b = 20, c = 30;
    function f(a){
        console.log(a, b, c);
        var b = a = c = 100;
        console.log(a, b, c);
    }
    f(10,20);
    console.log(a, b, c);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    // 浏览器内部执行的伪代码:
    var a;
    var b;
    var c;
    console.log(a, b, c);//变量提升,定义不赋值:undefined undefined undefined
    a = 10;
    b = 20; 
    c = 30;
    function f(a){//a为形参,20没有形参接收则省略
        var b;//变量提升,声明不赋值,b为局部变量
        console.log(a, b, c);//10 undefined 30
        b = a = c = 100;
        //b在函数内定义,为局部变量,a为形参也是局部变量,c未在函数定义,为全局变量
        console.log(a, b, c);//100 100 100
    }
    f(10,20);
    console.log(a, b, c);//10 20 100
    //这里a,b为全局变量,函数内部不影响全局变量a,b的值;
    //c只在外部被定义,但是在函数内部有改变其值,所以C已在函数内被更改
    
    // 答案:
    // undefined undefined undefined 
    // 10 undefined 30
    // 100 100 100
    // 10 20 100
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    第十题:

    console.log(num, str);
    var num = 18;
    var str = "lily";
    function fn2(){
        console.log(str,num);
        num = 19;
        str = "candy";
        var num = 14;
        console.log(str, num);
    }
    fn2();
    console.log(str, num);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    // 浏览器内部执行的伪代码:
    var num;
    var str;
    console.log(num,str);//undefinrd undefined
    num = 18;
    str = "lily";
    function fn2(){
        var num;
        cnsole.log(str, num);//lily undefined
        num = 19;
        str = "candy";
        num = 14;
        console.log(str, num);//candy 14
    }
    fn2();
    console.log(str,num);//candy 18
    
    // 答案:
    // undefinrd undefined
    // lily undefined
    // candy 14
    // candy 18
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 相关阅读:
    网络:IPv6
    「数据结构详解·六」哈希表
    07.webpack5搭建Vue环境
    计算机基础 - 原码,反码,补码
    基于极限学习机的轴承故障分类(西储大学数据)
    ESP32C3 LuatOS RC522②写入字符串
    AI 驱动的医疗变革:迈向未来医疗新生态
    SSG、SSR、CSR的区别
    Unity实现Camera和Audio数据的低延迟RTMP推送技术探讨
    怎么用蜂邮EDM和Outlook批量发送邮件带附件
  • 原文地址:https://blog.csdn.net/qq_51667621/article/details/127704615