MDN self-hosted library · e01.org

Test your skills: Variables

Learnwebdevelopment/Core/Scripting/Math

no runnable cells learn-module-assessment upstream source

Learn_web_development/Core/Scripting/Math

The aim of this skill test is to help you assess whether you've understood our Storing the information you need — Variables article.

[!NOTE] To get help, read our Test your skills usage guide. You can also reach out to us using one of our communication channels.

Interactive challenge

First of all, we are giving you a fun, interactive variables challenge created by our learning partner, Scrimba.

Watch the embedded scrim, and complete the task on the timeline (the little ghost icon) by following the instructions and editing the code. When you are done, you can resume watching the scrim to check how the teacher's solution matches up with yours.

Variables 1

To complete this task, add a new line to correct the value stored in the existing myName variable to your own name.

```html hidden live-sample___variables-1 live-sample___variables-2 live-sample___variables-1-finish live-sample___variables-2-finish


```css hidden live-sample___variables-1 live-sample___variables-2 live-sample___variables-1-finish live-sample___variables-2-finish
* {
  box-sizing: border-box;
}

p {
  color: purple;
  margin: 0.5em 0;
}

The starting point of the task looks like this:

100%

Here's the underlying code for this starting point:

```js live-sample___variables-1 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);


The updated output should look like this:

100%

<details>
<summary>Click here to show the solution</summary>

Your finished JavaScript should look something like this:

```js
// ...
// Don't edit the code above here!

myName = "Chris";

// Don't edit the code below here!
// ...

```js hidden live-sample___variables-1-finish let myName = "Paul";

myName = "Chris";

const section = document.querySelector("section"); const para = document.createElement("p"); para.textContent = myName; section.appendChild(para);


</details>

## Variables 2

The final task for now — in this case you are provided with some existing code, which has two errors present in it. The results panel should be outputting the name `Chris`, and a statement about how old Chris will be in 20 years' time. We want you to fix the problem and correct the output.

The starting point of the task looks like this (nothing is shown yet):

100%

Here's the underlying code for this starting point:

```js live-sample___variables-2
// 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);

The updated output should look like this:

100%

Click here to show the solution Your finished JavaScript should look something like this:
// 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!
// ...
```js hidden live-sample___variables-2-finish let myName = "Default"; myName = "Chris"; let myAge = 42; 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); ```

See also

Check out Practice time - Part 3: let and const MDN learning partner from Scrimba: An interactive challenge providing multiple tests concerning let and const.

Learn_web_development/Core/Scripting/Math