Important
These warm-up exercises are designed to ease you into the material. You don't need to complete all of them; instead, choose 1-2 sections that interest you and work on those. When you're ready for more challenging tasks, proceed to the Level 1 exercises.
There are two main ways of running Javascript: Via the Browser or through the terminal using node
- Create a
.js
file which logs:- A regular log:
Hello World
- A warning log:
Something is about to happen...
- An error log:
KABOOOOM
- A regular log:
- Run a script through the Browser
- Create a HTML file which include the javascript file
- Run the HTML file in the Browser
- Open the Developer Tools to see your logs
- Run the script via Node
- Check if node is installed by running
node -v
. If it doesn't understand the command you have to install Node - Open the Terminal in VS Code
- Run the javascript file using the command
node <path to your file>
.
For instancenode ./script.js
- Check if node is installed by running
- Declare three variables:
name
,age
, andisStudent
. Assign appropriate values to them. - Can you help me fix my code?
const name = "Alice"; const age = 16; const country = "USA"; const message = ""; const status = ""; if (age < 18) { status = "minor"; } else { status = "adult"; } message = `${name} is a ${status} from ${country}.`; console.log(message); // Alice is a minor from USA.
- Why is my code not working?
const canSee = true; if (canISee) { let room = "This room is not dark"; } else { room = "This room is pitch black"; } console.log(room);
- Define variables with different data types (string, number, boolean, undefined, null, object, array). They should all be something you may have for your team.
const team = "Team 31"; // add more variables here...
- Use
typeof
to check and print the type of each variable. - Guess the output, then run the code to see if you got it right.
const a = 10; const b = -10; const c = "100" const d = "no"; const e = true; const f = false; const g = null; console.log("#1", a + a, typeof (a + a)); // output is "#1 20 number" // guess the output of the following statements console.log("#2", a + b, typeof (a + b)); console.log("#3", a + c, typeof (a + c)); console.log("#4", a + d, typeof (a + d)); console.log("#5", a + e, typeof (a + e)); console.log("#6", a + f, typeof (a + f)); console.log("#7", a + g, typeof (a + g));
- Write a function that takes a user’s age as input and prints whether they are a child (0-12), a teenager (13-19), or an adult (20+).
- Create a function that asks for a number and prints whether it is positive, negative, or zero.
- Write a function that checks if a given year is a leap year (Hint: A leap year is divisible by 4).
- Print numbers from 1 to 10 using a for loop.
- Print a countdown from 10 to 1 using a for loop.
- Print even numbers between 1 and 20 using a for loop.
- Use a for...of loop to print each value in this array:
const names = ["john", "jane", "joe"];
- Create an array of five favorite foods and print each item using a for..of loop.
- Write a function that takes an array of numbers and returns the sum of all elements. What is the sum of
[5, 10, -98, 17.5, 365, -2.5]
? - Given an array
[10, 20, 30, 40, 50]
, write code to remove the last element and add 60 at the end. - Write a function that finds the largest number in an array
- Create an object representing a book with properties:
title
,author
, andyearPublished
. - Write a function that takes a book object and returns a string:
{title} by {author}, published in {yearPublished}
. - Add a method getAge to the book object that returns how old the book is based on the current year.
- Write a function that takes an array of book objects and returns an array of all book titles.
- Write a function
greet
that takes a name as a parameter and returns a greeting messageconsole.log(greet("Alice")); // "Hello, Alice!"
- Write a function
add
that takes two numbers and returns their sum.console.log(add(5, 3)); // 8
- Write a function
square
that returns the square of a number. Then, write another functionsumOfSquares
that takes two numbers and returns the sum of their squares.console.log(sumOfSquares(3, 4)); // 25 (3² + 4² = 9 + 16)