⚡ 0 XP
Step 1 of 2
Make your own machine
A function is a reusable machine: inputs go in, an answer comes out.
function gst(amount, rate) { ... } — amount and rate are parameters (the inputs)return sends the answer back, and the function stops there- Call it with values:
gst(100, 18) gives 118
console.log only shows a value; return gives it back so other code can use it, e.g. const bill = gst(250, 5);.
Examplefunction area(length, width) {
return length * width;
}
const room = area(4, 3);
console.log(`Room area: ${room} sq m`);
🎯 Challenge: A GST calculator function +15 XP
Complete gst(amount, rate) so it returns the amount plus rate percent GST. For example gst(100, 18) returns 118 and gst(250, 5) returns 262.5.
Try twice to unlock the solution (0/2)
script.jsCtrl ⏎
Console
Output from console.log appears here.
Checklist
Press Run to check your code.
Lesson notes (text version)
1. Make your own machine
A function is a reusable machine: inputs go in, an answer comes out.
function gst(amount, rate) { ... } — amount and rate are parameters (the inputs)return sends the answer back, and the function stops there- Call it with values:
gst(100, 18) gives 118
console.log only shows a value; return gives it back so other code can use it, e.g. const bill = gst(250, 5);.
function area(length, width) {
return length * width;
}
const room = area(4, 3);
console.log(`Room area: ${room} sq m`);
Challenge: A GST calculator function
Complete gst(amount, rate) so it returns the amount plus rate percent GST. For example gst(100, 18) returns 118 and gst(250, 5) returns 262.5.
2. Arrow functions
An arrow function is a shorter way to write a function and store it in a const:
const double = (n) => n * 2; — the value after => is returned automatically- With more inputs:
const add = (a, b) => a + b; - With many lines, use braces and
return: const f = (x) => { const y = x + 1; return y * 2; };
You already used arrows inside filter and map!
const square = (n) => n * n;
const fullName = (first, last) => `${first} ${last}`;
console.log(square(9), fullName("Sachin", "Tendulkar"));
Challenge: Three tiny helpers
Write three arrow functions:
toPaise(rupees) returns the amount in paise (1 rupee = 100 paise)isEven(n) returns true when n is even (use %)greet(name, city) returns Namaste Meera from Kochi! for greet("Meera", "Kochi")