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);
// 答案:13
i
1
2 --- 2*2=4 --- 4++=5
5
6 --- 6*2=12 --- 12++=13
13 > 10,退出循环输出:13

2.推断输出结果

var num = 10;
function fn(){
    num = 20;
    alert(num); // 不执行
}
alert(num); // 10

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.用for循环写出九九乘法表

for(var i = 1; i <= 9; i++){
    for(j = 1; j <= i; j++){
        document.write(j + "*" + i + "=" + i*j + "&nbsp");    
    }
    document.write("<br />")
}

在这里插入图片描述

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

四、域解析测试题

第一题:

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

第二题:

console.log(a);
var a = 123;
console.log(a);
function f1(){
    console.log(a);
    var a = 456;
    console.log(a);
}
f1();
console.log(a);
// 浏览器内部执行的伪代码:
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

第三题:

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

第四题:

console.log(a);
var a = 123;
console.log(a);
function a(){}
console.log(a);
function a(){}
console.log(a);
// 浏览器内部执行的伪代码:
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

第五题:

console.log(a);
var a = 12;
function fn(){
    console.log(a);
    var a = 13;
}
fn();
console.log(a);
// 浏览器内部执行的伪代码:
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

第六题:

console.log(a);
var a = 12;
function fn(){
    console.log(a);
    a = 13;
}
fn();
console.log(a);
// 浏览器内部执行的伪代码:
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

第七题:

var foo = 1;
function bar(){
    if(!foo){
        var foo = 10;
    }
    console.log(foo);
}
bar();//10
// 浏览器内部执行的伪代码:
var foo = 1;
function bar(){
	//无论if条件是否成立,都要进行变量提升
    var foo;//!undefined = true
    if(!foo){
        foo = 10;
    }
    console.log(foo); // 10
}
bar();

// 答案:10

第八题:

var n = 13;
function fn(n){
    alert(n);
    var n = 14;
    alert(n);
}
fn(n);
console.log(n);
// 浏览器内部执行的伪代码:
var n = 13;
function fn(n){//形参也是变量-----局部变量
    alert(n);//13
    var n = 14;
    alert(n);//局部变量,输出:14
}
fn(n);//实参也是变量
console.log(n);//全局变量,输出:13

// 答案:13 14 13

第九题:

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);
// 浏览器内部执行的伪代码:
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

第十题:

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);
// 浏览器内部执行的伪代码:
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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Laker 23

要秃啦,支持一下嘛~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值