JavaScript, lesson 6
A function is a named piece of code you can run whenever you need it. Functions let you write a calculation once, give it a clear name, and reuse it with different inputs.
name is a parameter: a variable that receives a value when the function is called."Mia" is the argument: the actual value passed in.return sends a value back to the caller and ends the function immediately.A function without a return (or with a bare return;) gives back undefined. Printing
inside a function and returning from it are different things: a function that returns a value
lets the caller decide what to do with it, which makes it far easier to reuse and test.
2 exercises to try what you just read.
JavaScript has a shorter syntax called an arrow function. You usually store it in a
const:
Both styles are common. Function declarations are hoisted, which means you can call them
on a line above where they are written. Arrow functions stored in a const can only be used
after that line has run.
If a caller leaves out an argument, the parameter is undefined. You can supply a fallback
value instead:
The second result shows floating-point rounding again; format it with toFixed(2) when you
print money.
Variables declared with let or const live only inside the block (the { }) where they
are declared. A function's parameters and local variables are invisible outside it:
Inner code can read outer variables, like rate here, but not the other way round. Keeping
variables local means two functions can both use a variable called total without stepping on
each other. Try to make functions depend only on their parameters; they are then easy to
understand in isolation.
Good functions do one job and have a name that says what that job is. A program that reads
input, computes something and prints a report often splits naturally into parse...,
compute... and format... functions. You will see this shape again in the final project.
Write two functions:
toFahrenheit(c) returns c * 9 / 5 + 32.toCelsius(f) returns (f - 32) * 5 / 9.The input is one line such as 100 C or 212 F: a number, a space, and the unit. Convert it
to the other unit and print the result with one decimal place and the new unit letter.
Sample input:
Sample output:
The program around your functions is already written; you only fill in the two functions.
Write a function pizzasNeeded(people, slicesEach = 3) that returns how many whole pizzas
to order. Every pizza has 8 slices, and you always round up (use Math.ceil), because
nobody orders half a pizza.
The input has one or two whole numbers on a line: the number of people and, optionally, the
slices each person eats. Print Order N pizzas (or Order 1 pizza when exactly one is
needed, and Order 0 pizzas for zero people).
Sample input:
Sample output:
Ten people eating 3 slices each need 30 slices, which is 3.75 pizzas, so you order 4. Write the whole program: the function, reading the input, and printing the line.
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("Mia")); // Hello, Mia!
console.log(greet("Omar")); // Hello, Omar!const square = (n) => n * n; // single expression: returned automatically
const area = (w, h) => {
const result = w * h;
return result; // with braces you need return
};
console.log(square(4), area(3, 5)); // 16 15function price(amount, taxRate = 0.2) {
return amount * (1 + taxRate);
}
console.log(price(100)); // 120
console.log(price(100, 0.1)); // 110.00000000000001const rate = 3; // visible everywhere in this file
function triple(n) {
const result = n * rate; // result exists only inside triple
return result;
}
console.log(triple(5)); // 15
// console.log(result); // ReferenceError: result is not defined100 C212.0 F10Order 4 pizzas