源码

第1题

1
2
3
4
5
6
//1. 1+2=3
function getSum(num1, num2) {
var sum = num1 + num2;
console.log(num1 + "+" + num2 + "=" + (num1 + num2));
}
getSum(1, 2);

第1题

第2题

错误代码

1
2
3
4
5
//2.源代码运行报错
getProduct(2, 4);
var getProduct = function (num1, num2) {
console.log(num1 + "*" + num2 + "=" + num1 * num2);
}

第2题错误代码

修改如下

1
2
3
4
5
//2.修改方案如下,将getProduct()函数在声明之后调用
var getProduct = function (num1, num2) {
console.log(num1 + "*" + num2 + "=" + num1 * num2);
}
getProduct(2, 4);

第2题正确代码

第3题

1
2
3
4
5
6
7
8
9
10
//3.
var a = 100

function test() {
console.log(a);
a = 10;
console.log(a);
}
test();
console.log(a);

第3题

第4题

1
2
3
4
5
6
7
8
9
10
//4. undefined 20
var a = 10;
fun1();

function fun1() {
var b = 20;
console.log(a);
console.log(b);
var a = 30;
}

第4题

第5题

1
2
3
4
5
6
7
8
//5. 不报错
function fun2(a, b) {
console.log(arguments[0]);
console.log(arguments[1]);
console.log(arguments[2]);
}

fun2(1, 2, 3);

第5题

第6题

1
2
3
4
5
6
7
8
9
10
//6.
var obj = {a: 1, b: 2, c: 3};

function fun3(a) {
for (var c in a) {
console.log(obj[c]);
}
}

fun3(obj)

第6题

第7题

1
2
3
//7. num变量未定义 报错
"use strict"
num = 100;

第7题

第8题

1
2
3
4
5
6
7
//8.
"use strict"
myFunction();

function myFunction() {
y = 5; // y 未定义
}

第8题

第9题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

//9.
"use strict"

function test() {
try {
a = 20;
} catch (err) {
// var a = 10
console.log("报错咯😄");
console.log("219970502❤️陈浩然");
}
}

test()

第9题