這裡蒐集平常看到的小題目,無論做對或做錯,都記錄下來。
No.1
What will this code log to the consol?
1 | const foo = [1, 2, 3]; |
使用解構賦值 (Destructuring assignment) ,可以把陣列或物件中的資料解開擷取成為獨立變數。
解構賦值使用類似語法;不過在指定敘述式的左側,要宣告從來源變數接收解開值之變數。
1 | const x = [1, 2, 3, 4, 5]; |
No.2
What does this code print to the consol?
1 | const x =[1, 2]; |
使用 ES6 的解構賦值 (Destructuring assignment),可以把陣列或物件中的資料解開擷取。
No.3
What value is assigned to total after this code executes?
1 | const sum = function(num1, num2 =2, num3 = 3){ |
一樣使用 ES6 的解構賦值 (Destructuring assignment)
No.4
What line of code causes this code segment to throw an error?
1 | const lion = 1; |
++lion;
,因為是以 const 賦值,如果值為原始型別,就無法再度賦值。如果是物件型別則可修改物件裡的內容。
所以答案是:++lion;
,because lion cannot be reassigned a value.
No.5
What is the output that is printed when the div containing the text “click Here” is clicked?
1 | <div id="A"> |
1 | document.querySelectorAll("div").forEach(e => { |
答案是 “C” “B” “A”,冒泡(bubbling)
可參考這篇:冒泡和捕获
No.6
把缺的 code 補上
1 | // Missing Line |
別想太多,就是 vowels 變數沒定義與賦值:let vowels = "aeiou"
No.7
Which snippet could you add to this code to print “YOU GOT THIS” to the console?
1 | let cipherText = [..."YZOGUT QGMORTZ MTRHTILS"]; |
答案:
1 | for(let [index, value] of cipherText.entries()){ |
原來可以使用 for..of 這樣取到 index 和 value !
No.8
Which code would you yse to access the Irish flag?
1 | var flagsJSON = '{"countries" : [' + |
答案:flagDatabase.countries[0].flag
No.9
What statement can be used to select the element from the DOM containing the “The LinkedIn Learning library has great Javascript courses” from this markup”?
1 | <h1 class="content">LinkedIn Learning</h1> |
答案:document.querySelector("span.content");
No.10
What is wrong with this code?
1 | const foo = { |
一切正常,不會出錯。
No.11
Which line could you add to this code to print “jaguar” to the console?
1 | let animals = ["jaguar", "eagle"]; |
答案:animals.reverse()
Array.prototype.pop() 會刪除陣列的最後一個元素,並回傳刪除元素,先使用 reverse() 將陣列反轉,在取出最後一個元素。
No.12
What is the output of the code?
1 | let rainForests = ["Amazon", "Borneo", "Cerrado", "Congo"]; |
答案: ['Cerrado', 'Congo']
splice(0,2):從 index 0 切兩個掉。