互動挑戰
首先,我們為您提供了一個由我們的 學習夥伴 Scrimba 建立的有趣的互動式變數挑戰。
觀看嵌入的 scrim,並按照說明編輯程式碼,在時間軸上(小幽靈圖示)完成任務。完成後,您可以繼續觀看 scrim,檢查老師的解決方案與您的解決方案有何不同。
任務 1
要完成此任務,請新增新的一行,將現有 myName 變數中儲存的值更正為您的名字。
js
let myName = "Paul";
// Don't edit the code above here!
// Add your code here
// Don't edit the code below here!
const section = document.querySelector("section");
const para = document.createElement("p");
para.textContent = myName;
section.appendChild(para);
點選此處顯示解決方案
你完成的 JavaScript 應該看起來像這樣
js
// ...
// Don't edit the code above here!
myName = "Chris";
// Don't edit the code below here!
// ...
任務 2
這是目前最後一個任務 — 在這種情況下,您將獲得一些現有程式碼,其中包含兩個錯誤。結果面板應輸出名字 Chris,以及關於 Chris 20 年後年齡的陳述。我們希望您修復問題並更正輸出。
js
// Fix the following code
const myName = "Default";
myName = "Chris";
let myAge = "42";
// Don't edit the code below here!
const section = document.querySelector("section");
const para1 = document.createElement("p");
const para2 = document.createElement("p");
para1.textContent = myName;
para2.textContent = `In 20 years, I will be ${myAge + 20}`;
section.appendChild(para1);
section.appendChild(para2);
點選此處顯示解決方案
你完成的 JavaScript 應該看起來像這樣
js
// Turn the const into a let, so the value can be changed
let myName = "Default";
myName = "Chris";
// myAge needs to have a number datatype
let myAge = 42;
// Don't edit the code below here!
// ...
另見
同時,請檢視 Scrimba 的 練習時間 - 第 3 部分:let 和 const MDN 學習夥伴:這是一項互動式挑戰,提供了關於 let 和 const 的多個測試。