码迷,mamicode.com
首页 > Web开发 > 详细

27道js题

时间:2017-04-28 23:32:04      阅读:336      评论:0      收藏:0      [点我收藏+]

标签:javascrip   相等   math   reduce   lte   数字   .com   ror   窗口   


开始
原文网址:http://javascript-puzzlers.herokuapp.com/
本文是题目来自以上网址,解析是自己写的

1. ["1", "2", "3"].map(parseInt)
A ["1","2","3"] B [1,2,3] C [0,1,3] D other
选D ohter 也就是 [1,NaN,NaN]:parseInt("字符串",redix),redix表示要解析的数字的基数。该值介于 2 ~ 36 之间,也就是进制数;map函数传入3个参数(item,index,array) ,所以[parseInt("1",0), parseInt("1",1) ,parseInt("1",2) ] 当redix为0时,默认十进制,如果它以 “0x” 或 “0X” 开头,将以 16 为基数。

2. [typeof null, null instanceof Object]
A ["object",false] B [null,false] C ["object",true] D other
选A null 类型是一个对象,typeof 是显示null的类型,但null与object本质不是一个数据类型 null instanceof Object为false
3. [ [3,2,1].reduce(Math.pow), [].reduce(Math.pow) ]
A an error B[9,0] C[9,NaN] D[9,undefined]
选 A [9,报错] [2,3].reduce(Math.pow)=2^3=8


4. var val = ‘smtg‘;
console.log(‘Value is ‘ + (val === ‘smtg‘) ? ‘Something‘ : ‘Nothing‘);
答: Something
解析:‘Value is ‘ + (val === ‘smtg‘)为string为true, 所以打印Something


5.
var name = ‘World!‘;
(function () {
if (typeof name === ‘undefined‘) {
var name = ‘Jack‘;
console.log(‘Goodbye ‘ + name);
} else {
console.log(‘Hello ‘ + name);
}
})();
答:Goodbye Jack 解析:if中也存在变量申明提前

6.
var END = Math.pow(2, 53);
var START = END - 100;
var count = 0;
for (var i = START; i <= END; i++) {
count++;
}
console.log(count);
答:count 会进入一个无限循环 2^53过大,START不管怎么加一都等于END

7.
var ary = [0,1,2];
ary[10] = 10;
ary.filter(function(x) { return x === undefined;});
答: [] 解析:filter会直接跳过数组不存在值的部分,所以值符合要求

8.
var two = 0.2
var one = 0.1
var eight = 0.8
var six = 0.6
以下结果为?
[two - one == one, eight - six == two]
答:[true,false] two - one=0.1 0.8-0.6=0.20000000000000007 0.1+0.2=0.30000000000000004

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‘));
答: ‘Do not know!‘ switch使用的是===, new String(‘A‘)是新对象
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‘));
答: 没啥好说的 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);//?返回数组是?
答 [true,,true,true,false,false] -9%2为-1,‘13‘%2会自动转为Number,Infinity%2 为NaN
12.
parseInt(3, 8)
parseInt(3, 2)
parseInt(3, 0)
答:[3,NaN,3] 上面讲过


13. Array.isArray( Array.prototype )
答: true 数组原型也是数组


14.
var a = [0];
if ([0]) {
console.log(a == true);
} else {
console.log("wut");
}
答: false 解析:在if([0])中作为布尔值为true,[0]==true中作为0是false


15. []==[]
答: false 不好解析 知乎解析:https://www.zhihu.com/question/29615998


16.
‘5‘ + 3
‘5‘ - 3
答: ‘53‘,2 ‘+‘使3转为字符串,但‘-‘使字符串转为数字 exp:-‘223‘ 为-223


17. 1 + - + + + - + 1
答: 2 貌似没啥玄机的 1 + - + + + - + - 1为0就看最后的符号是加还是减


18.
var ary = Array(3);
ary[0]=2
ary.map(function(elem) { return ‘1‘; });//?返回的数组是?
答:[1, , ] 解析:map函数会跳过没有值的部分


19.
function sidEffecting(ary) {
ary[0] = ary[2];
}
function bar(a,b,c) {
c = 10
sidEffecting(arguments);
return a + b + c;
}
bar(1,1,1)
答:21 解析:在sidEffecting(arguments);输入console.log(arguments);你会发现打印出{ ‘0‘: 10, ‘1‘: 1, ‘2‘: 10 },
arguments对象是参数的集合,类似数组但又不是数组,在bar函数中可以初步认为变化3次,arguments输入时为{‘0‘:1,‘1‘:1,‘2‘:10}经过sidEffecting函数{ ‘0‘: 10, ‘1‘: 1, ‘2‘: 10 }所以a=10,b=1,c=10。


20. var a = 111111111111111110000,
b=1111;
a + b;
答 111111111111111110000 解析:js中的精度问题影响了小数和大数,但是如果b = 111111111111111111111;那么结果a+b为222222222222222230000


21. var x = [].reverse;
x();
答:window 查的解析: [] .reverse将返回this,如果没有显式接收器对象调用,它将默认为默认的此AKA窗口

 

22. Number.MIN_VALUE > 0
答: true 解析 Number的最小值大于0


23. [1 < 2 < 3, 3 < 2 < 1]
答: [true,true] true为1,false为0


24.   2 == [[[2]]]
答:true  解析:两边都被转为字符串


25.
3.toString()
3..toString()
3...toString()
答: error,"3",error


26.
(function(){
var x = y = 1;
})();
console.log(y);
console.log(x);

答: 1,error y是全局的要注意


27.
var a = /123/,
b = /123/;
a == b
a === b
答:false,false 解析:匹配对象不相等

27道js题

标签:javascrip   相等   math   reduce   lte   数字   .com   ror   窗口   

原文地址:http://www.cnblogs.com/waterserver/p/6783459.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!