Visual blocks to JavaScript: say, ask and variables

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Visual blocks to JavaScript: say, ask and variables

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which JavaScript is the typed version of a 'say Hello' block?
A prompt("Hello");
B console.log("Hello");
2. Which block becomes prompt() when you rewrite it as JavaScript?
A ask 'What is your name?' and wait
B say 'What is your name?'
3. Which line is the typed version of the block 'set score to 0'?
A let score = 0;
B score == 0;
4. A script starts with 'ask What is your name?'. How do you keep the answer, like the 'answer' block does?
A let name = prompt("What is your name?");
B prompt("What is your name?") = name;
5. The block 'change score to 10' (the variable already exists) becomes which line?
A let score = 10; written a second time
B score = 10;

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1console.log("Hello!"); Alet score = 0;
2let name = prompt("Your name?"); BThe typed version of an ask block: it asks the user a question and stores the answer in a variable.
3A set score to 0 block becomes a variable created with let and given a starting value. Cask is a block name, not a JavaScript function. The typed version of an ask block is prompt().
4A change score by 1 block becomes an assignment that updates the existing variable, with no new let. Dsay is the name of a block, not a JavaScript function. The typed version of a say block is console.log().
5say("Hello!"); Escore = score + 1;
6let age = ask("How old are you?"); FThe typed version of a say block: it shows a message in the console.

Definitions

console.log, prompt, let

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
___________("Hello!");
2.
let name = ______("What is your name?");
3.
___ score = 0;
4.
let score = 0; score _ 5;
5.
let name = prompt("Name?"); console.log(____);

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A let name = say("Name?");
B console.log("Hi " + name);
C console.log("Welcome!");
Question 2
A set score = 0;
B score = score + 5;
C console.log(score);
Question 3
A let player = "Sam";
B console.log("Hi, " + "player");
C console.log("Let's play!");
Question 4
A let lives = 3;
B let lives = lives - 1;
C console.log(lives);
Question 5
A let food = prompt("Food?");
B let fan = "I like " + food;
C console.log(Fan);

Answer Key

Visual blocks to JavaScript: say, ask and variables
Section A: Quick Questions
Q1
B — console.log("Hello");
Q2
A — ask 'What is your name?' and wait
Q3
A — let score = 0;
Q4
A — let name = prompt("What is your name?");
Q5
B — score = 10;
Section B: Match the Pairs
Q1
console.log("Hello!"); → The typed version of a say block: it shows a message in the console.
Q2
let name = prompt("Your name?"); → The typed version of an ask block: it asks the user a question and stores the answer in a variable.
Q3
A set score to 0 block becomes a variable created with let and given a starting value. → let score = 0;
Q4
A change score by 1 block becomes an assignment that updates the existing variable, with no new let. → score = score + 1;
Q5
say("Hello!"); → say is the name of a block, not a JavaScript function. The typed version of a say block is console.log().
Q6
let age = ask("How old are you?"); → ask is a block name, not a JavaScript function. The typed version of an ask block is prompt().
Section C: Definitions
Q1
console.log, prompt, let — Blocks have typed JavaScript equivalents: a say block becomes console.log, an ask block becomes prompt, and a set variable block becomes let (or an assignment with =).
Section D: Fill in the Blanks
Q1
console.log("Hello!");
Q2
let name = prompt("What is your name?");
Q3
let score = 0;
Q4
let score = 0; score = 5;
Q5
let name = prompt("Name?"); console.log(name);
Section E: Spot the Error
Q1
A — say is a block name, not a JavaScript function. An ask block becomes prompt, so the line should be let name = prompt("Name?");
Q2
A — set is the word on the block, not JavaScript. A set variable block becomes let, so the line should be let score = 0;
Q3
B — Quotes around player make it plain text, so the program prints the word player. Without quotes it uses the variable: console.log("Hi, " + player);
Q4
B — A change variable block is an assignment without let. Using let again redeclares lives and causes an error. It should be lives = lives - 1;
Q5
C — JavaScript is case sensitive. The variable is called fan, not Fan, so Fan is not defined. It should be console.log(fan);