亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

JavaScript選擇題解答(1-20)

Worktile / 3450人閱讀

摘要:最近做了個上的的測試題目地址,錯了一大堆,感覺的概念還有很多不是很清晰,這里記錄一下個人博客文章地址第一題解答這里考的是的用法。如果出現的數字不符合后面輸入的進制,則為,所以第二個值為。

最近做了個heroku上的JavaScript的測試(題目地址),錯了一大堆,感覺js的概念還有很多不是很清晰,這里記錄一下

個人博客文章地址

第一題

What is the result of this expression? (or multiple ones)

["1", "2", "3"].map(parseInt) 

A:["1", "2", "3"]

B:[1, 2, 3]

C:[0, 1, 2]

D:other

解答:這里考的是map、parseInt的用法。map會傳遞三個參數給其作為參數的函數,為(element, index, array),分別為當前的元素、當前元素在數組中的位置、整個數組:

> ["1", "2", "3"].map(function(){console.log(arguments)}) 
["1", 0, Array[3]]
["2", 1, Array[3]]
["3", 2, Array[3]]

而parseInt只接收兩個參數,為(element, radix),element代表需要被轉換為int的字符串,radix代表當前字符串里數字的進制數

所以相當于說,結果數組的元素實際分別為為:

parseInt("1", 0)
parseInt("2", 1)
parseInt("3", 2)

parseInt("1", 0)的值為1,MDN上可以看到parseInt函數的radix為0時的行為

If radix is undefined or 0 (or absent), JavaScript assumes the following:

If the input string begins with "0x" or "0X", radix is 16 (hexadecimal) and the remainder of the string is parsed.

If the input string begins with "0", radix is eight (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.

If the input string begins with any other value, the radix is 10 (decimal).

所以這里radix值實際為10,所以結果為1

而parseInt("2", 1)和parseInt("3", 2)則確實無法解析,會生成NaN

所以答案為[1,NaN,NaN],為D

第二題和第五題

What is the result of this expression? (or multiple ones)

[typeof null, null instanceof Object]

A: ["object", false]

B: [null, false]

C: ["object", true]

D: other

考察typeof運算符和instanceof運算符,上MDN上看一下typeof運算符,一些基礎類型的結果為:
Undefined "undefined"
Null "object"
Boolean "boolean"
Number "number"
String "string"
Any other object "object"
Array "object"

自從javascript創(chuàng)造出來,typeof null的值就是object了

而null instanceof 任何類型 都是false

所以答案為["object", false], 選A

第三題

What is the result of this expression? (or multiple ones)

[ [3,2,1].reduce(Math.pow), [].reduce(Math.pow)] ]

A: an error

B: [9, 0]

C: [9, NaN]

D: [9, undefined]

這題考的Math.pow和Array.prototype.reduce

Math.pow(base, exponent)接受兩個參數:基數、需要計算的次方

reduce傳遞給其作為參數的函數幾個值:
* previousValue:上一次計算的結果
* currentValue:當前元素的值
* index: 當前元素在數組中的位置
* array:整個數組

reduce本身接受兩個參數,callback和initialValue,分別是reduce的回調函數和計算初始值--也就是第一次reduce的callback被調用時的previousValue的值,默認為0

reduce在數組為空且沒有定義initialValue時,會拋出錯誤,如chrome下:TypeError: Reduce of empty array with no initial value

所以選A

第四題

What is the result of this expression? (or multiple ones)

var val = "smtg";
console.log("Value is " + (val === "smtg") ? "Something" : "Nothing");

A: Value is Something

B: Value is Nothing

C: NaN

D: other

這題考的javascript中的運算符優(yōu)先級,MDN傳送門,這里"+"運算符的優(yōu)先級要高于"?"所以運算符,實際上是 "Value is true"?"Something" : "Nothing",當字符串不為空時,轉換為bool為true,所以結果為"Something",選D

第六題

What is the result of this expression? (or multiple ones)

var name = "World!";
(function () {
    if (typeof name === "undefined") {
        var name = "Jack";
        console.log("Goodbye " + name);
    } else {
        console.log("Hello " + name);
    }
})();

A: Goodbye Jack

B: Hello Jack

C: Hello undefined

D: Hello World

這題考的是javascript作用域中的變量提升,javascript的作用于中使用var定義的變量都會被提升到所有代碼的最前面,于是乎這段代碼就成了:

var name = "World!";
(function () {
    var name;//現在還是undefined
    if (typeof name === "undefined") {
        name = "Jack";
        console.log("Goodbye " + name);
    } else {
        console.log("Hello " + name);
    }
})();

這樣就很好理解了,typeof name === "undefined"的結果為true,所以最后會輸出"Goodbye Jack",選A

第七題

What is the result of this expression? (or multiple ones)

var END = Math.pow(2, 53);
var START = END - 100;
var count = 0;
for (var i = START; i <= END; i++) {
    count++;
}
console.log(count);

A: 0

B: 100

C: 101

D: other

這題考查javascript中的數字的概念:首先明確一點,javascript和其他語言不同,僅有一種數字,IEEE 754標準的64位浮點數,能夠表示的整數范圍是-2^53~2^53(包含邊界值),所以Math.pow(2, 53)即為javascript中所能表示的最大整數,在最大整數在繼續(xù)增大就會出現精度丟失的情況,END + 1 的值其實是等于END的,這也就造成了死循環(huán),所以選D

第八題

What is the result of this expression? (or multiple ones)

var ary = [0,1,2];
ary[10] = 10;
ary.filter(function(x) { return x === undefined;});

A: [undefined × 7]

B: [0, 1, 2, 10]

C: []

D: [undefined]

考查Array.prototype.filter方法的使用,MDN上有這么一句it is not invoked for indexes which have been deleted or which have never been assigned values,所以結果為空數組,選C

第九題

What is the result of this expression? (or multiple ones)

var two   = 0.2
var one   = 0.1
var eight = 0.8
var six   = 0.6
[two - one == one, eight - six == two]

A: [true, true]

B: [false, false]

C: [true, false]

D: other

浮點數計算時的精度丟失問題,其他語言也會出現...至于結果,反正我是蒙的...chrome中計算出來的結果:[0.1, 0.20000000000000007],也就是[true, false],選C

第十題

What is the result of this expression? (or multiple ones)

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"));

A: Case A

B: Case B

C: Do not know!

D: undefined

這題考的是使用new方法創(chuàng)建基礎類型,使用new方法創(chuàng)建的基礎類型,首先來看個栗子(chrome):

> typeof new String("skyinlayer");
"object"
typeof "skyinlayer";
"string"

這樣基本上就能看到結果了,但是為什么呢?MDN上的解釋是,字符串字面量和直接調用String()方法(不使用new調用構造函數)的結果是原始字符串。JS自動回轉化原始字符串到String對象。所以可以在原始字符串上使用用String對象的方法。而在上下文中,在原始字符串的方法被調用或者從其中獲取屬性時,JS會自動包裹原始字符串然后調用方法或者獲取屬性。

所以呢,JS本身有原始字符串和字符串對象之分,只不過在調用方法和獲取屬性時的時候會自動轉換,但typeof運算符運算時是不會轉換的。Number和Boolean同樣適用

所以這里結果為Do not know!,選C

第十一題

What is the result of this expression? (or multiple ones)

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!");
    }
}
showCase(String("A"));

A: Case A

B: Case B

C: Do not know!

D: undefined

和上題原理一樣,不過這里沒有使用new來生成字符串,所以生成的結果就是原始字符串,相當于showCase("A"),所以結果就是A了

第十二題

What is the result of this expression? (or multiple ones)

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);

A: [true, true, true, true, true]

B: [true, true, true, true, false]

C: [true, true, true, false, false]

D: [true, true, false, false, false]

還是JS的數字相關,不過這次考察的是取模,這題我也是瞎蒙的(果斷跪了)。

前兩個基本上沒什么疑問,必然是true

"13"在進行計算前則會進行隱式類型轉換(JS最惡心的部分之一),詳細參見$雨$的文章《Javascript類型轉換的規(guī)則》,這里的規(guī)則就是將字符串通過Number()方法轉換為數字,所以結果為13 % 2 ,也就是true

而JS中負數取模的結果是負數,這里-9%2的結果實際上是-1,所以為false

而Infinity對任意數取模都是NaN,所以是false

綜上,結果為[true, true, true, false, false],也就是C

第十三題

What is the result of this expression? (or multiple ones)

parseInt(3, 8)
parseInt(3, 2)
parseInt(3, 0)

A: 3, 3, 3

B: 3, 3, NaN

C: 3, NaN, NaN

D: other

還是parseInt的題,考的和第一題類似,第一個值為3沒什么好說的。如果出現的數字不符合后面輸入的進制,則為NaN,所以第二個值為NaN。而radix為0時的情況第一題下面有介紹,這里也是一樣為默認10,所以結果為3,所以答案為3, NaN, 3,選D

第十四題

What is the result of this expression? (or multiple ones)

Array.isArray( Array.prototype )

A: true

B: false

C: error

D: other

死知識,MDN傳送門,這是MDN官方給的例子...

第十五題

What is the result of this expression? (or multiple ones)

var a = [0];
if ([0]) { 
  console.log(a == true);
} else { 
  console.log("wut");
}

A: true

B: false

C: "wut"

D: other

同樣是一道隱式類型轉換的題,不過這次考慮的是"=="運算符,a本身是一個長度為1的數組,而當數組不為空時,其轉換成bool值為true。

而==左右的轉換,會使用如果一個操作值為布爾值,則在比較之前先將其轉換為數值的規(guī)則來轉換,Number([0]),也就是0,于是變成了0 == true,結果自然是false,所以最終結果為B

第十六題

What is the result of this expression? (or multiple ones)

[] == []

A: true

B: false

C: error

D: other

這題考的是數組字面量創(chuàng)建數組的原理和==運算符,首先JS中數組的真實類型是Object這點很明顯typeof []的值為"object",而==運算符當左右都是對象時,則會比較其是否指向同一個對象。而每次調用字面量創(chuàng)建,都會創(chuàng)造新的對象,也就是會開辟新的內存區(qū)域。所以指針的值自然不一樣,結果為 false,選B

第十七題

What is the result of this expression? (or multiple ones)

"5" + 3  
"5" - 3  

A: 53, 2

B: 8, 2

C: error

D: other

又是一道隱式類型轉換的題

加法: 加法運算中,如果有一個操作值為字符串類型,則將另一個操作值轉換為字符串,最后連接起來

減法: 如果操作值之一不是數值,則被隱式調用Number()函數進行轉換

所以第一行結果為字符串運算,為"53"。第二行結果為2,選A

第十八題

What is the result of this expression? (or multiple ones)

1 + - + + + - + 1 

A: 2

B: 1

C: error

D: other

C語言中的經典...對于這種問題,原理什么的不懂,蒙吧,結果是2

第十九題

What is the result of this expression? (or multiple ones)

var ary = Array(3);
ary[0]=2
ary.map(function(elem) { return "1"; }); 

A: [2, 1, 1]

B: ["1", "1", "1"]

C: [2, "1", "1"]

D: other

又是考的Array.prototype.map的用法,map在使用的時候,只有數組中被初始化過元素才會被觸發(fā),其他都是undefined,所以結果為["1", undefined × 2],選D

第二十題

What is the result of this expression? (or multiple ones)

function sidEffecting(ary) { 
  ary[0] = ary[2];
}
function bar(a,b,c) { 
  c = 10
  sidEffecting(arguments);
  return a + b + c;
}
bar(1,1,1)

A: 3

B: 12

C: error

D: other

這題考的是JS的函數arguments的概念:

在調用函數時,函數內部的arguments維護著傳遞到這個函數的參數列表。它看起來是一個數組,但實際上它只是一個有l(wèi)ength屬性的Object,不從Array.prototype繼承。所以無法使用一些Array.prototype的方法。

arguments對象其內部屬性以及函數形參創(chuàng)建getter和setter方法,因此改變形參的值會影響到arguments對象的值,反過來也是一樣

具體例子可以參見Javascript秘密花園#arguments

所以,這里所有的更改都將生效,a和c的值都為10,a+b+c的值將為21,選D

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規(guī)行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://www.ezyhdfw.cn/yun/87481.html

相關文章

  • JavaScript擇題解答(21-37)

    摘要:上半部選擇題解答個人博客文章地址第二十一題又是一道考查數字的題,與第七題考察點相似。 上半部:JavaScript選擇題解答(1-20) 個人博客文章地址 第二十一題 What is the result of this expression? (or multiple ones) var a = 111111111111111110000, b = 1111; a +...

    李增田 評論0 收藏0
  • LeetCode 之 JavaScript 解答第229題 —— 求眾數 II(Majority E

    摘要:繼續(xù)使用摩投票算法,假設要投票,選取票數超過以上選為候選人,一個被分為三等份,也就是說最多有兩位當選人。排序解決先進行一次排序快排,然后遍歷數據,找出滿足條件的數據。 Time:2019/4/5Title: Majority Element 2Difficulty: mediumAuthor: 小鹿 問題:Majority Element 2 Given an integer ar...

    BearyChat 評論0 收藏0
  • LeetCode 之 JavaScript 解答第十五題 —— 三數之和(3Sum)

    摘要:如果三個數據相加等于了,就存儲該三個值且更新和指針。邊界條件判斷數組內元素是否都為整數或負數,直接返回。判斷和指針的大小關系。在原來數組上進行排序,不生成副本。 Time:2019/4/3Title:3SumDifficulty: mediumAuthor:小鹿 題目三:ADD Two Numbers Given an array?nums?of?n?integers, are the...

    Wildcard 評論0 收藏0
  • LeetCode 之 JavaScript 解答104題 —— 二叉樹的最大深度

    摘要:小鹿題目二叉樹的最大深度給定一個二叉樹,找出其最大深度。二叉樹的深度為根節(jié)點到最遠葉子節(jié)點的最長路徑上的節(jié)點數。求二叉樹的深度,必然要用到遞歸來解決。分別遞歸左右子樹。 Time:2019/4/22Title: Maximum Depth of Binary TreeDifficulty: MediumAuthor:小鹿 題目:Maximum Depth of Binary Tre...

    boredream 評論0 收藏0
  • LeetCode 之 JavaScript 解答第226題 —— 翻轉二叉樹(Invert Bina

    摘要:算法思路判斷樹是否為空同時也是終止條件。分別對左右子樹進行遞歸。代碼實現判斷當前樹是否為左右子樹結點交換分別對左右子樹進行遞歸返回樹的根節(jié)點歡迎一起加入到開源倉庫,可以向提交您其他語言的代碼。 Time:2019/4/21Title: Invert Binary TreeDifficulty: EasyAuthor: 小鹿 題目:Invert Binary Tree(反轉二叉樹) ...

    MingjunYang 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<