javascript-puzzlers
萌神7号
学习
展开
-
14.数组
14.数组var a = [0];if ([0]) { console.log(a == true);} else { console.log("wut");}//false解析:[0]的boolean值是true,巩固:a[0] 的boolean是 false原创 2020-09-09 13:57:02 · 211 阅读 · 0 评论 -
13.isArray()
13.Array.isArray( Array.prototype )Array.isArray(Array.prototype) //true解析:Array.prototype是一个数组,数组的原型是数组,对象的原型是对象,函数的原型是函数Array.isArray(obj)obj 必需,要判断的对象。isArray() 方法用于判断一个对象是否为数组。如果对象是数组返回 true,否则返回 false。...原创 2020-09-09 13:53:46 · 261 阅读 · 0 评论 -
一道链式函数的题目
实现(5).add(3).minus(2),使其输入结果为6function check(n){ n = Number(n) return isNaN(n)?0:n;}Number.prototype.add = function (n) { // 注:箭头函数会将this指向window n = check(n); return this + n}Number.prototype.minus = function (n){ n = check(n); retu原创 2020-09-06 08:33:41 · 79 阅读 · 0 评论 -
12.parseInt()
12.parseInt()parseInt(3, 8) //3parseInt(3, 2) //NaN 2进制不可能有3parseInt(3, 0) //3,表示数本身parseInt(string, radix)//string 必需。要被解析的字符串。//radix 可选。表示要解析的数字的基数。该值介于 2 ~ 36 之间。//如果省略该参数或其值为 0,则数字将以10为基础来解析。如果它以“0x”或“0X”开头,将以16为基数。//如果该参数小于 2 或者大于 36,则 parseI原创 2020-09-02 08:49:04 · 1686 阅读 · 0 评论 -
11.取余数运算
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, fal原创 2020-09-02 08:42:51 · 802 阅读 · 0 评论 -
10.续第9题
10.续第9题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原创 2020-08-23 14:26:39 · 114 阅读 · 0 评论 -
9.Object不等于字符
9.Object不等于字符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; de原创 2020-08-23 14:23:27 · 200 阅读 · 0 评论 -
8.JS小数精度问题
8.JS小数精度问题var two = 0.2var one = 0.1var eight = 0.8var six = 0.6[two - one == one, eight - six == two]0.2-0.1 //0.10.8-0.6 //0.20000000000000007//答案是[true,false]//要想后者为true(0.8-0.6).toFixed(1)//0.2JS不能很精确地表示小数。当两个浮点数相加或者相减,将有可能会导致精度丢失问题。原创 2020-08-23 14:19:43 · 2145 阅读 · 0 评论 -
7.稀疏数组问题
7.稀疏数组问题var ary = [0,1,2];ary[10] = 10;ary.filter(function(x) { return x === undefined;});因为这个数组从3-9未定义,所以很容易想到上面数组返回7个undefined的数组。但是由于filter函数作用,首先判断数组当前index是否被设置(ary[index] = value),如果没有的话则会跳过这个index,继续向下执行。所以最终返回是空数组,即:[]filter()函数 创建一个新的数组,新数组中原创 2020-08-22 11:41:11 · 351 阅读 · 0 评论 -
6.JS所表示的最大数
6.JS所表示的最大数var END = Math.pow(2, 53);var START = END - 100;var count = 0;for (var i = START; i <= END; i++) { count++;}console.log(count);咋一看上面代码输入时101,可是Js所能表示最大的数是2^53,在2^53+1之后的数均显示2**53console.log(Math.pow(2,53)); //9007199254740992co原创 2020-08-22 10:57:35 · 582 阅读 · 0 评论 -
5.变量声明提升的作用
5.变量声明提升的作用//题目var name = 'World!';(function () { if (typeof name === 'undefined') { var name = 'Jack'; console.log('Goodbye ' + name); } else { console.log('Hello ' + name); }})(); //Goodbye Jack由于js在执行代码时,会把fun原创 2020-08-21 18:27:51 · 180 阅读 · 0 评论 -
4.运算符的优先级
4.运算符的优先级var val = 'smtg';console.log('Value is ' + (val === 'smtg') ? 'Something' : 'Nothing'); //Something这个题最容易犯的错误是‘Value is Something’判断优先级最高的是括号内部的var val = 'smtg';console.log('Value is ' + (val === 'smtg') ? 'Something' : 'Nothing'); //Som原创 2020-08-21 18:07:13 · 696 阅读 · 0 评论 -
3.[ [3,2,1].reduce(Math.pow), [].reduce(Math.pow) ]的结果
3.[ [3,2,1].reduce(Math.pow), [].reduce(Math.pow) ]的结果(1)Math.pow(x,y) 方法:得到 x 的 y 次幂的值Math.pow( x,y ) //x 的 y 次幂。//x 必需。底数,必须是数字。//y 必需。幂数,必须是数字。//特别地,如果是虚数或负数,则该方法将返回 NaN//如果由于指数过大而引起浮点溢出,则该方法将返回 Infinity。Math.pow(0,0)//0^0 = 1Math.pow(0,1) //0^1原创 2020-08-21 17:54:24 · 969 阅读 · 0 评论 -
2.[typeof null, null instanceof Object]的结果
javascript-puzzlers的知识点小结2.[typeof null, null instanceof Object]的结果2.[typeof null, null instanceof Object]的结果(1)typeof A 返回结果是A的类型。主要是由6种(number、string、undefined、boolean、function、object)typeof undefined;//undefinedtypeof 'abc';//stringtypeof 123;//n原创 2020-08-21 17:18:20 · 4791 阅读 · 2 评论 -
1.[‘1’,‘2’,‘3’].map(parseInt)返回结果是什么
javascript-puzzlers的知识点小结1.['1','2','3'].map(parseInt)返回结果是什么?1.[‘1’,‘2’,‘3’].map(parseInt)返回结果是什么?(1)map方法(接收一个函数,遍历这个数组每个元素都调用这个函数,最终返回值得数组)let arr = [1, 2, 3];let newArr = arr.map(x => x*x);console.log(newArr); // [1,4,9]array.map(function(cur原创 2020-08-21 16:21:20 · 279 阅读 · 0 评论