Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
βΆ Do the exercisesExercise 1 β Which keyword allows changes?
You need to create a variable for a CodΓ©mon's health that will decrease during battles. Which keyword should you use?
letconstvarHint : Ask yourself whether this value will still be the same after a few battles.
Exercise 2 β Complete the string
Add the missing quotes to create a valid string:
Hint : Think of a string as something you open and then close. Look carefully at both ends of the text.
Exercise 3 β Valid camelCase name?
Which variable name follows camelCase convention?
attackPowerAttackPowerattack_powerATTACKPOWERHint : Reread the name of the convention itself: the way it is written shows you how it works.
Exercise 4 β Match values to types
Match each value with its correct type:
100"100"true"true"Exercise 5 β Correct boolean syntax?
Which is the correct way to create a boolean variable?
let isActive = true;let isActive = "true";let isActive = True;let isActive = TRUE;Hint : Two things separate these options: whether the value is treated as text, and how it is capitalised. Only one option gets both right.
Exercise 6 β Use typeof operator
Complete the code to check the type of the variable:
Hint : One of these options is the operator made for this job; the others are traps, so check the spelling and the capital letters. Then ask yourself whether you want to inspect the variable itself or a piece of text.
Exercise 7 β undefined vs null
What value does an unassigned variable have?
let codemon;
console.log(codemon);undefinednull0""Hint : The variable was declared but never given anything. Does JavaScript leave it empty on its own, or does someone have to put that emptiness there?
Exercise 8 β Declare a constant
Create a constant for the game version that never changes:
Hint : A game version never moves once it is set: let that guide the first blank. For the second, ask yourself whether you are giving a value or comparing two of them.
Exercise 9 β Which is a number?
Which of these values is a number type?
3.14"3.14"'3.14'3,14Hint : Ask yourself which of these values JavaScript could do maths with.
Exercise 10 β Match typeof results
Match each value with what typeof returns:
typeof "Hello"typeof 99typeof falsetypeof undefined"string""number""boolean""undefined"Exercise 11 β Assign null value
Create a variable with null to represent an empty inventory slot:
Hint : The slot is empty on purpose, not by accident, and that difference matters here. Watch out too for options that only look right: capital letters and quotes change everything.
Exercise 12 β Invalid variable name?
Which variable name is NOT allowed in JavaScript?
player-scoreplayerScoreplayer_scoreplayerScore2Hint : Read each name the way JavaScript would: one of them contains a symbol that JavaScript already uses for something else.
Exercise 13 β Convert to camelCase
Convert these variable names to proper camelCase:
Hint : The comment on each line gives you the original name. Think back to how the naming chapter glued several words into a single name, and compare the options letter by letter.
Exercise 14 β Which quotes work?
Which of these creates a valid string in JavaScript?
"text", 'text', `` `text` ``"text"'text'Hint : Look back at how strings were written in the primitive types chapter: was there only one accepted way, or several?
Exercise 15 β Match keyword to usage
Match each keyword with when to use it:
letconstvarExercise 16 β Create number variables
Create two number variables with the correct syntax. wholeNumber should be an integer and decimal should be a decimal number:
Hint : One value has to be whole, the other has to have a decimal part. Then ask yourself which of the remaining options JavaScript would read as text instead of as a number.
Exercise 17 β What does typeof return?
What type of value does the typeof operator always return?
Hint : Think back to what appeared in the console when you used this operator: was the result something you could calculate with, or something you could read?
Exercise 18 β Complete boolean variables
Fill in the boolean values. Game was won and not lost. You should know which value to use where.
Hint : The sentence in the question tells you which state each variable is in. Then pick the writing that JavaScript understands as a real value and not as text.
Exercise 19 β How many primitive types?
How many primitive types have you learned so far?
Hint : Go back through the chapter on primitive types and count only the ones you actually practised writing yourself.
Exercise 20 β Match errors to problems
Match each code issue with its problem:
let name = Aquachu;let isActive = "true";let level = "50";const hp = 100; hp = 50;Exercise 21 β When to use null?
When should you use null instead of undefined?
Hint : One of these two values shows up on its own; the other only exists if you write it. Start from that difference.
Exercise 22 β Declare multiple variables
Complete this code to create three different types of variables:
Hint : Ask two questions on each line: will this value ever change later, and does JavaScript need a mark to know it is text?
Exercise 23 β What happens with const?
What happens if you try to change a const variable?
const maxPlayers = 4;
maxPlayers = 6;Hint : Ask yourself why you would pick this keyword rather than another one. If changing the value were allowed, would that choice still mean anything?
Exercise 24 β Good vs bad names
Match each bad variable name with a better alternative:
player_nameMaxHealthattack-powerCODEMON_TYPEplayerNamemaxHealthattackPowercodemonTypeExercise 25 β Review all concepts
Complete this code using everything you've learned:
Hint : Take the blanks one at a time. For each declaration, ask whether that value will ever change during the game. For each value, ask whether JavaScript should read it as text. For the last blank, look at what that line is meant to display.