读者可以先去做一下感受感受. 当初笔者的成绩是 21/44… 当初笔者做这套题的时候不仅怀疑智商, 连人生都开始怀疑了…. 不过, 对于基础知识的理解是深入编程的前提. 让我们继续各种变态面试题来考察自己吧!
第8题
var two = 0.2
var one = 0.1
var eight = 0.8
var six = 0.6
[two - one == one, eight - six == two]
IEEE 754标准中的浮点数并不能精确地表达小数 那什么时候精准, 什么时候不经准呢? 笔者也不知道…
答案 [true, false]
第9题
function showCase(value) {
switch(value) {
case 'A':
console.log('Case A');
break;
case 'B':
console.log('Case B');
break;
case undefined:
console.log('undefined');
break;
default:
console.log('Do not know!');
}
}
showCase(new String('A'));
switch 是严格比较, String 实例和 字符串不一样.
var s_prim = 'foo';
var s_obj = new String(s_prim);
console.log(typeof s_prim); // "string"
console.log(typeof s_obj); // "object"
console.log(s_prim === s_obj); // false
答案是 'Do not know!'
第10题
function showCase2(value) {
switch(value) {
case 'A':
console.log('Case A');
break;
case 'B':
console.log('Case B');
break;
case undefined:
console.log('undefined');
break;
default:
console.log('Do not know!');
}
}
showCase2(String('A'));
解释:
String(x) does not create an object but does return a string, i.e. typeof String(1) === "string"
还是刚才的知识点, 只不过 String 不仅是个构造函数 直接调用返回一个字符串哦.
答案 'Case A'
第11题
function isOdd(num) {
return num % 2 == 1;
}
function isEven(num) {
return num % 2 == 0;
}
function isSane(num) {
return isEven(num) || isOdd(num);
}
var values = [7, 4, '13', -9, Infinity];
values.map(isSane);
此题等价于
7 % 2 => 1
4 % 2 => 0
'13' % 2 => 1
-9 % % 2 => -1
Infinity % 2 => NaN
需要注意的是 余数的正负号随第一个操作数.
答案 [true, true, true, false, false]
第12题
parseInt(3, 8)
parseInt(3, 2)
parseInt(3, 0)
第一个题讲过了, 答案 3, NaN, 3
第13题
Array.isArray( Array.prototype )
一个鲜为人知的实事: Array.prototype => [];
答案: true
第14题
var a = [0];
if ([0]) {
console.log(a == true);
} else {
console.log("wut");
}
答案: false
第15题
[]==[]
== 是万恶之源, 当对象和对象进行比较的时候,比较的是地址
答案是 false
第16题
'5' + 3
'5' - 3
+ 用来表示两个数的和或者字符串拼接, -表示两数之差.
请看例子, 体会区别:
> '5' + 3
'53'
> 5 + '3'
'53'
> 5 - '3'
2
> '5' - 3
2
> '5' - '3'
2
也就是说 - 会尽可能的将两个操作数变成数字, 而 + 如果两边不都是数字, 那么就是字符串拼接.
答案是 '53', 2