JavaScript Basics – Coding-Only Practice Test
Instructions:
• Write the JavaScript code for each problem.
• Do not use built-in shortcuts unless told.
• Make sure your syntax is correct.
• Test your code if possible.
Variables & Output
Create a variable called name and assign your name to it.
Print this message:
Hello, my name is ___
Data Types
Create 3 variables:
• age (number)
• isStudent (boolean)
• favoriteColor (string)
Print all three to the console.
Arithmetic Operators
Create two variables:
num1 = 10
num2 = 5
Print:
• Sum
• Difference
• Product
• Quotient
If Statement
Write a program that:
• Stores a number in a variable score
• If score is 70 or higher → print "Pass"
• Otherwise → print "Fail"
If / Else If / Else
Write a program that:
• Stores a number 1–7 in day
• Prints:
o "Weekend" if day is 6 or 7
o "Weekday" otherwise
Switch Statement
Rewrite this using switch:
If fruit equals:
• "apple" → print "Red"
• "banana" → print "Yellow"
• Anything else → print "Unknown fruit"
For Loop
Write a for loop that prints numbers from 1 to 10.
While Loop
Write a while loop that prints numbers from 5 down to 1.
Do While Loop
Write a do...while loop that prints numbers from 1 to 5.
Functions (Basic)
Create a function called greet that:
• Takes one parameter name
• Returns "Hello " + name
Call the function with your name.
Functions with Return Value
Create a function called addNumbers that:
• Takes two parameters
• Returns their sum
Call the function and print the result.
Comparison Practice
Write code that:
• Creates two variables: x = 5 and y = "5"
• Prints the result of:
o x == y
o x === y
Challenge Question
Write a program that:
• Loops from 1 to 20
• Prints:
o "Even" if the number is even
o "Odd" if the number is odd
Bonus (Harder)
Write a function called isAdult that:
Takes age as a parameter
Returns true if age is 18 or older
Returns false otherwise