Objects and functions
JavaScript · Unit 7: Objects
- Full plan
- JavaScript
- 10 exercises
A function can take an object: a single parameter that brings several named pieces of data. Inside, you read them with a dot.
function card(p) {
return `${p.name}, ${p.age}`;
}
const ava = {
name: "Ava",
age: 17,
};
console.log(card(ava));Prints
Ava, 17
The rest of the explanation is in the lesson, which is part of the full plan.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Predict the output
What does this code print?
2. Complete the code
Complete the function that works out what you pay for one item on your grocery list.
3. Multiple choice
A function takes an object instead of five separate parameters. What do you gain?
4. Predict the output
What does this code print?
5. Find the bug
This program should print "Leo". Which line has the error?
6. Complete the code
Complete it so the function returns the tip (10%) and the total to pay.
7. Predict the output
What does this code print?
8. Put the lines in order
Order the function that gets the marks of two tests,
t1andt2, and returns the average and whether they passed (6 or more).9. Find the case that fails
toPay(p)should return a string like"You pay 80", with the price minus the discount. If the item has nodiscount, you pay the full price. Which call makes it fail?10. Predict the output
Two players finish the match. What does it print?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.