Moving from Scratch and JavaScript to Python
All code in this test is JavaScript, which you already know. Read carefully, take your time, and show your thinking. It is okay if you do not finish the bonus question. When you are done, click Download my answers at the bottom and send the file to your teacher.
Choose the best answer. 1 point each.
A1. What does a variable do in a program?
A2. After this code runs, what is the value of score?
score
let score = 10; score = score + 5;
A3. In JavaScript, which symbol CHECKS if two values are equal?
A4. What is the position (index) of the FIRST item in an array?
A5. True or False: a while loop always repeats an exact number of times that you choose in advance.
while
A6. In Scratch, the “repeat 10” block is most like which JavaScript idea?
A7. What does return do inside a function?
return
A8. In JavaScript, what does "5" + 5 give you?
"5" + 5
Write EXACTLY what each program prints, in order. One line of output per console.log.
B1. (2 pts)
let x = 4; let y = x * 2; x = x + 1; console.log(x); console.log(y);
B2. (2 pts)
let age = 13; if (age >= 13) { console.log("teen"); } else { console.log("kid"); }
B3. (3 pts)
let total = 0; for (let i = 1; i <= 4; i++) { total = total + i; } console.log(total);
B4. (3 pts)
let count = 0; while (count < 3) { console.log("hi"); count = count + 1; } console.log(count);
B5. (3 pts)
let animals = ["cat", "dog", "bird"]; console.log(animals[1]); console.log(animals.length);
B6. (4 pts)
for (let i = 1; i <= 3; i++) { for (let j = 1; j <= 2; j++) { console.log(i + "-" + j); } }
B7. (3 pts)
function double(n) { return n * 2; } let result = double(3) + double(5); console.log(result);
Each program has ONE bug. Describe what is wrong and how you would fix it. 3 points each.
C1. This should print “Welcome!” only when the password is correct, but it prints it no matter what.
let password = "cat"; if (password = "dragon") { console.log("Welcome!"); }
C2. This game should end when you run out of lives, but it never stops running.
let lives = 3; while (lives > 0) { console.log("Playing..."); }
C3. This should print the LAST colour in the list, but it prints undefined.
let colors = ["red", "green", "blue"]; console.log(colors[3]);
You can write JavaScript or clear pseudocode (plain English steps). Small syntax mistakes are okay if the logic is right.
D1. Write a loop that prints the numbers 1 to 10, one per line. (4 pts)
D2. Write a function called greet that takes a name and RETURNS (not prints) the text “Hello, NAME!”. For example, greet("Sam") should return “Hello, Sam!”. (5 pts)
greet
greet("Sam")
BONUS. Write a function called isEven that takes a number and returns true if it is even and false if it is odd. Hint: n % 2 gives the remainder when n is divided by 2. (3 pts)
isEven
true
false
n % 2
Saves a text file you can email or hand in to your teacher.