技能测试:变量

此技能测试旨在帮助您评估是否理解了我们关于 存储所需信息 — 变量 的文章。

注意: 如需帮助,请阅读我们的“技能测试”使用指南。您也可以通过我们的沟通渠道与我们联系。

互动挑战

首先,我们为您提供了一个由我们的 学习伙伴 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 学习伙伴:这是一项交互式挑战,提供了关于 letconst 的多个测试。