LEARN TO CODE | JAVASCRIPT | 01 LANGUAGE FOUNDATIONS

01 Language foundations: challenges

Write small independent programs, predict their outputs and prove your conditions and loops behave correctly at their boundaries.

START HERE | WORK IN VS CODE

Use the index.html and script.js setup from 01.1. Save your JavaScript in VS Code, refresh the browser and inspect its Console. Use a separate folder or replace script.js for each independent task, so repeated declarations do not collide.

Attempt each challenge before opening a solution. Record a prediction, the actual output and one sentence explaining any difference. Completion ticks last while this page is open.

Download the complete example programs

CHALLENGE 01 | CONNECT THE FILES

Create a VS Code folder containing index.html and script.js. Log a welcome message and 6 * 7.

Need a hint?

Use the script src and defer starter. Read the browser console, not the terminal.

Reveal a possible solution
console.log("Welcome to my project");
console.log(6 * 7);

The heading appears on the page; both logged values appear in that tab’s Console.

CHALLENGE 02 | TEXT OR VALUE?

Declare const clubName = "Robotics". Log the name’s value and the literal text clubName on separate lines.

Need a hint?

Quotes create literal text.

Reveal a possible solution
const clubName = "Robotics";
console.log(clubName);
console.log("clubName");

CHALLENGE 03 | CHOOSE LET OR CONST

Store a fixed 5-point bonus and a score starting at 10. Apply the bonus twice and log the final score.

Need a hint?

Only the score needs reassignment.

Reveal a possible solution
const bonus = 5;
let score = 10;
score += bonus;
score += bonus;
console.log(score);

CHALLENGE 04 | KEEP THE OLD TOTAL

Start with a balance of 20, add 8 and spend 5. Explain why balance = 8 would be wrong for adding 8.

Need a hint?

Read the right-hand side before assigning.

Reveal a possible solution
let balance = 20;
balance = balance + 8;
balance = balance - 5;
console.log(balance);

balance = 8 replaces the old balance; it does not preserve and increase it.

CHALLENGE 05 | REPAIR A TEXT CALCULATION

The known value "4" is ticket-count text. Each ticket costs 6 credits. Log the numeric total and its type.

Need a hint?

Convert the known text deliberately.

Reveal a possible solution
const countText = "4";
const count = Number(countText);
const total = count * 6;
console.log(total);
console.log(typeof total);

For real form input, also reject blank or invalid values. Number alone is not complete validation.

CHALLENGE 06 | WRITE A TEMPLATE MESSAGE

Use a name, 3 rounds and 4 points per round to produce “Alex earned 12 points in 3 rounds.”

Need a hint?

Use backticks and ${...}.

Reveal a possible solution
const name = "Alex";
const rounds = 3;
const pointsPerRound = 4;
console.log(`${name} earned ${rounds * pointsPerRound} points in ${rounds} rounds.`);

CHALLENGE 07 | TEST THE EXACT BOUNDARY

Print Target reached for scores of at least 10, otherwise Keep going. Test 9, 10 and 11 separately.

Need a hint?

At least includes equality.

Reveal a possible solution
const score = 10;
if (score >= 10) {
  console.log("Target reached");
} else {
  console.log("Keep going");
}

Expected for 9: Keep going. Expected for 10 and 11: Target reached. Change the constant between runs.

CHALLENGE 08 | CHECK TWO REQUIREMENTS

An activity requires age at least 12 AND permission. Test all combinations of age 11/12 and permission false/true.

Need a hint?

Two required conditions need &&.

Reveal a possible solution
const age = 12;
const hasPermission = true;
if (age >= 12 && hasPermission) {
  console.log("Approved");
} else {
  console.log("Not approved");
}

Only age 12 with permission true is approved among these four cases. A string "false" is not the boolean false.

CHALLENGE 09 | NUMBER THE ROUNDS

Use a for loop to log Round 1 through Round 5, with no Round 0 or Round 6.

Need a hint?

Write down the first value, last permitted value and update.

Reveal a possible solution
for (let round = 1; round <= 5; round++) {
  console.log(`Round ${round}`);
}

CHALLENGE 10 | ACCUMULATE INSTEAD OF REPLACING

Add the integers 1 to 5 using a loop. Log only the final total and explain where the initialisation belongs.

Need a hint?

Keep the total outside the repeated body.

Reveal a possible solution
let total = 0;
for (let number = 1; number <= 5; number++) {
  total = total + number;
}
console.log(total);

Initialise once before the loop. Resetting inside would erase earlier additions.

CHALLENGE 11 | COUNT DOWN SAFELY

Log 4, 3, 2, 1 and then Launch! using while. Predict what happens if the initial value is 0.

Need a hint?

Ensure the update moves towards a false condition.

Reveal a possible solution
let count = 4;
while (count > 0) {
  console.log(count);
  count--;
}
console.log("Launch!");

With count initially 0, the body runs zero times and only Launch! is logged. Do not remove the update and run an infinite loop.

CHALLENGE 12 | CAPSTONE: A ROUND REPORT

Build a four-round report. Each round adds 3 points. Round 3 adds a one-off bonus of 2 points. Log every running total, then report whether Alex reached a target of 14. Change the target to 15 and test again.

Need a hint?

Keep the total outside the loop. Put the bonus decision inside; put the final target decision after the loop.

Reveal a possible solution
const playerName = "Alex";
const rounds = 4;
const pointsPerRound = 3;
const bonusAtRound = 3;
const bonusPoints = 2;
const target = 14;
let total = 0;

for (let round = 1; round <= rounds; round++) {
  total += pointsPerRound;
  if (round === bonusAtRound) {
    total += bonusPoints;
  }
  console.log(`Round ${round}: ${total} points`);
}

if (total >= target) {
  console.log(`${playerName} reached the target with ${total} points.`);
} else {
  console.log(`${playerName} needs ${target - total} more points.`);
}

With target 15, the round totals stay 3, 6, 11 and 14, but the final message is Alex needs 1 more points. A stretch task is to improve singular/plural wording using another condition.

FINISH | DEMONSTRATE YOUR UNDERSTANDING

Explain the difference between assignment and comparison, why a total is initialised before a loop, and why "5" + 2 differs from 5 + 2. Show one test exactly on a decision boundary.

Next, organise programs into functions and explore arrays and objects. You now have the language foundations those structures will build on.

KEEP LEARNING

Previous page 02.1 Functions, parameters and return values

Download Section 01 example programs

Further reading: JavaScript in VS Code · Grammar and types · Template literals · Loops and iteration