← Back to Sprint Overview

Code-Alongs: JavaScript Fundamentals

Welcome to the Code-Alongs section! Here you'll find interactive exercises to practice your JavaScript skills. You can write and run code directly in your browser.

Exercise 1: Working with Variables and Console

In this exercise, you'll practice declaring variables and using console.log() to output information.

JavaScript Code Editor
// Output will appear here after you run the code

Tip:

Remember to use let or const to declare variables. When printing multiple values, you can use template literals with backticks (`) to create more readable output.

Show Solution
// Declare variables to store your name, age, and favorite programming language
const name = "Alex Johnson";
let age = 25;
const favLanguage = "JavaScript";

// Using regular string concatenation
console.log("My name is " + name + ", I am " + age + " years old, and my favorite programming language is " + favLanguage + ".");

// Using template literals (preferred method)
console.log(`My name is ${name}, I am ${age} years old, and my favorite programming language is ${favLanguage}.`);

// Next year, my age will increase
age = age + 1;
console.log(`Next year, I will be ${age} years old.`);

Exercise 2: Working with Conditionals

Practice using if/else statements to make decisions in your code.

JavaScript Code Editor
// Output will appear here after you run the code

Tip:

Remember to use the if/else structure for conditional logic. You can test your code with different age values.

Show Solution
// Create a voting eligibility checker
const voterAge = 17;  // Change this value to test different ages

if (voterAge >= 18) {
    console.log("You are " + voterAge + " years old.");
    console.log("You are eligible to vote!");
} else {
    console.log("You are " + voterAge + " years old.");
    console.log("Sorry, you must be 18 or older to vote.");
    console.log("You can vote in " + (18 - voterAge) + " years.");
}

// Testing with another age example
const anotherAge = 21;
const canVote = anotherAge >= 18;

if (canVote) {
    console.log(`At ${anotherAge} years old, you can vote!`);
} else {
    console.log(`At ${anotherAge} years old, you cannot vote yet.`);
}

Exercise 3: Working with Functions

Practice creating and calling functions to organize your code.

JavaScript Code Editor
// Output will appear here after you run the code

Tip:

A function should take width and height parameters, multiply them, and return the result. Don't forget to call your function with different values to test it.

Show Solution
// Function to calculate the area of a rectangle
function calculateRectangleArea(width, height) {
    const area = width * height;
    return area;
}

// Calculate area of first rectangle
const width1 = 5;
const height1 = 10;
const area1 = calculateRectangleArea(width1, height1);
console.log(`A rectangle with width ${width1} and height ${height1} has an area of ${area1} square units.`);

// Calculate area of second rectangle
const width2 = 7;
const height2 = 3;
const area2 = calculateRectangleArea(width2, height2);
console.log(`A rectangle with width ${width2} and height ${height2} has an area of ${area2} square units.`);

// Using the function with direct values
console.log(`A rectangle with width 4 and height 4 has an area of ${calculateRectangleArea(4, 4)} square units.`);

// Optional: Using arrow function
const calcArea = (w, h) => w * h;
console.log(`Using arrow function: Area = ${calcArea(6, 8)} square units.`);

Exercise 4: Working with Loops

Practice using loops to execute code multiple times.

JavaScript Code Editor
// Output will appear here after you run the code

Tip:

You can use a for loop with a counter variable that increments/decrements on each iteration. Remember to use console.log() to see the output.

Show Solution
// Counting up from 1 to 10
console.log("Counting up:");
for (let i = 1; i <= 10; i++) {
    console.log(i);
}

// Counting down from 10 to 1
console.log("\nCounting down:");
for (let i = 10; i >= 1; i--) {
    console.log(i);
}

// Bonus: Using a while loop
console.log("\nUsing a while loop to count up:");
let counter = 1;
while (counter <= 10) {
    console.log(counter);
    counter++;
}

// Bonus: Skip even numbers
console.log("\nOnly odd numbers:");
for (let i = 1; i <= 10; i++) {
    if (i % 2 === 0) {
        continue; // Skip even numbers
    }
    console.log(i);
}