Skip to content
UNWIND Code Lab Co-funded by the European Union
Functions and Loops
Log In

Functions and Loops

  • 6 min reading
  • 6 min activity
0 of 6 tasks doneup to 125 XP
  1. Quick recall10 XPOptional
  2. Calculator30 XPTo do
  3. Parts of a for loop15 XPTo do
  4. Which loop tool?15 XPTo do
  5. Even numbers30 XPTo do
  6. Countdown25 XPOptional

Mission 4, step 6. Two big ideas: write code once and use it many times (functions), and repeat work (loops). Your website needs both next.

In this lesson
  • Write and call functions with parameters and return.
  • Repeat code with for, while and do...while.
  • Stop a loop with break or skip a round with continue.
Remember

In lesson 4.5 you met the short ternary form. One quick question.

Quick check

Quick recall

10 XP

Functions

A function is a named piece of code for one job. You define it once and call it as often as you like. Its inputs are parameters. return sends a result back to the place of the call and ends the function: code after it does not run.

function greet(name) {
  console.log(`Hello, ${name}!`);
}
greet("Ayla");    // Hello, Ayla!
greet("Maria");   // Hello, Maria!

function add(a, b) {
  return a + b;
  console.log("You never see this");
}
let value = add(10, 20);   // 30
console.log(value * 2);    // 60

const multiply = function (a, b) {   // a function in a constant
  return a * b;
};

On the slides for return, while, break and continue, a closing } hides inside a comment, so the code stops with an error. Put } on its own line.

Worked example: a calculator

Worked example
  1. Three inputs

    calculator(a, b, operation) takes two numbers and a sign like "+".

  2. Choose with switch

    One case for each sign.

  3. Protect

    Before dividing, if (b === 0) returns a message at once.

  4. Send back

    return result; at the end; default returns "Unknown operation".

Practice

Calculator

Required 30 XP

The daily application from the course slides: a function with a switch inside. Fill the two gaps so that the function sends its results back.

Loops

A loop repeats work. for is for a known number of rounds: start, condition, step.

let fruits = ["Apple", "Pear", "Banana"];
for (let i = 0; i < fruits.length; i++) {
  console.log(fruits[i]);
}
// Apple, Pear, Banana

First i is 0. Before each round the loop checks i < fruits.length; after it, i++ adds 1. On the slide, some output lines are not comments, so copied code does not run; here they are comments.

Sorting

Parts of a for loop

Required 15 XP

while repeats as long as a condition is true. Change something inside, or it never ends (here the practice space stops it). do...while runs first and checks later: at least once.

let counter = 0;
while (counter < 3) {
  console.log("While runs: " + counter);
  counter++;   // without this line the loop never ends
}

let number = 100;
do {
  console.log("I run once, then I check.");
} while (number < 5);

break leaves the loop at once. continue skips the rest of this round.

for (let i = 0; i < 5; i++) {
  if (i === 2) {
    continue;   // skip 2
  }
  if (i === 4) {
    break;      // stop at 4
  }
  console.log(i);
}
// 0, 1, 3
Matching

Which loop tool?

Required 15 XP

Your turn: even numbers

The last daily application of the slides: go through 1 to 20 and print only the even numbers. Remember % from lesson 4.3. With this exercise you complete the Logic Builder badge.

Practice

Even numbers

Required 30 XP

The last daily application of the course slides: check the numbers 1 to 20 and print only the even ones.

A few more minutes? The optional bonus uses while, continue and do...while, and gives a star for Mission 4.

Practice

Countdown

25 XP

Bonus, if you have time: a countdown with while and continue, and a do...while loop, as on slides 74 to 77 of the course.

Function
A named piece of code that you can call many times.
Parameter
An input of a function, in its brackets.
return
Sends a value back and ends the function.
Loop
Code that repeats: for, while, do...while.

In short

  • Define a function once, call it many times; return sends the result back.
  • for for a known number of rounds, while for "as long as", do...while for "at least once".
  • break stops a loop, continue skips one round.
Next time

Project step 4: your website greets visitors, lists your goals and shows more about you at the tap of a button.

Sources

  1. MDN Web Docs (2026). Functions: reusable blocks of code and return.
  2. MDN Web Docs (2026). Looping code.
  3. MDN Web Docs (2026). do...while and continue.

End of the lesson

This lesson: 0 of 135 XP

Code Cards in this lesson: 7

  • function
  • parameter
  • return
  • for
  • while
  • break
  • continue