Functions and Loops
- 6 min reading
- 6 min activity
0 of 6 tasks doneup to 125 XP
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.
- Write and call functions with parameters and
return. - Repeat code with
for,whileanddo...while. - Stop a loop with
breakor skip a round withcontinue.
In lesson 4.5 you met the short ternary form. One quick question.
Quick recall
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
- Three inputs
calculator(a, b, operation)takes two numbers and a sign like"+". - Choose with switch
One
casefor each sign. - Protect
Before dividing,
if (b === 0)returns a message at once. - Send back
return result;at the end;defaultreturns "Unknown operation".
Calculator
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.
Parts of a for loop
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
Which loop tool?
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.
Even numbers
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.
Countdown
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;
returnsends the result back. forfor a known number of rounds,whilefor "as long as",do...whilefor "at least once".breakstops a loop,continueskips one round.
Project step 4: your website greets visitors, lists your goals and shows more about you at the tap of a button.
Sources
- MDN Web Docs (2026). Functions: reusable blocks of code and return.
- MDN Web Docs (2026). Looping code.
- MDN Web Docs (2026). do...while and continue.
End of the lesson
This lesson: 0 of 135 XP
Code Cards in this lesson: 7
functionparameterreturnforwhilebreakcontinue