Important
The Level 1 exercises will require you to apply your learnings from previous weeks. We recommend that you start with one exercise. If you find it easy, proceed to the Level 2 exercises. Otherwise, try another exercise here.
In this exercise you will create a basic system to manage student records. Let's get started:
- Create Student Objects
- Define a function
createStudent(name, age, grades)
that takes a student's name, age, and grades and return an object with these properties.
- Define a function
- Store Students in an Array
- Create an empty array called
students
. - Use the
createStudent
function to create at least three student objects and store them in the students array.
- Create an empty array called
- Calculate Average Grade
- Write a function
calculateAverageGrade(student)
that takes a student object and returns their average grade.
- Write a function
- Find the Best Student
- Write a function
findTopStudent(students)
that takes an array of students and returns the student with the highest average grade.
- Write a function
- Display Student Information
- Write a function
displayStudentInfo(student)
that logs a message like:"John, Age: 20, Average Grade: 85"
- Use this function to display details for all students in the students array.
- Write a function
- Add Grades Dynamically
- Modify the
createStudent
function to also include a methodaddGrade(newGrade)
inside the student object, allowing new grades to be added dynamically. Use this function to add a grade to a student after it is created.
- Modify the
Now help us answer these questions for the below students:
- What is the average grade of Frank?
- Who has the highest average grades?
- Who has the lowest average grades?
- Allan 20 years - Grades: [10, 10, 7, 4, 7]
- Betty 22 years - Grades: [12, 7, 7, 4, 2]
- Charlie 21 years - Grades: [10, 7, 4, 2, 0]
- David 23 years - Grades: [7, 4, 2, 0, -3]
- Eva 20 years - Grades: [12, 7, 10, 2, 12]
- Grace 22 years - Grades: [7, 10, 7, 10, 7]
- Henry 21 years - Grades: [4, 7, 7, 4, 4]
- Irene 23 years - Grades: [2, 7, 7, 4, 0]
- Describe in your own words to a team mate or mentor what this code does
- The code is hard to read due to poor naming. Can you improve it? (there might be more than just the names to improve)
function f(m) {
let a = [];
for (let i = 0; i < m.length; i++) {
if (m[i].rating >= 8) {
a.push(m[i].title);
}} return a; }
let d = [
{ title: "Interstellar", rating: 8.6 },
{ title: "The Room", rating: 3.7 },
{ title: "The Godfather", rating: 9.2 },
{ title: "Cars", rating: 7.1 }
];
console.log(f(d));
// Expected Output: ["Interstellar", "The Godfather"]
Your niece found this script on the internet and is asking how it works.
- Guess the output
- Now run the code, did you guess correctly?
const a = 10;
const b = -10;
const c = "100"
const d = "no";
const e = {
name: "John",
};
const f = [1, 2, 3];
const h = true;
// guess the output of the following statements
console.log("#1", a + a);
console.log("#2", a + b);
console.log("#3", a + c);
console.log("#4", a + d);
console.log("#5", a + e);
console.log("#6", a + e["name"]);
console.log("#7", a + e["age"]);
console.log("#8", a + f);
console.log("#9", a + f[1]);
console.log("#10", a + h);
Below is a piece of code that is supposed to process a list of students and their grades, and return the average grade for the class. The calculation should ignore any invalid grades (negative numbers or non-numeric grades). However, the code has some bugs. Your task is to identify and fix the issues.
function calculateAverageGrade(students) {
let total = 0;
let count = 0;
for (let i = 0; i < students.length; i++) {
let grade = students[i].grade;
if (grade > 0) {
total += grade;
count++;
} else {
console.log("Invalid grade for student: " + students[i].name);
}
}
return total / count;
}
let studentsList = [
{ name: "Alice", grade: 90 },
{ name: "Bob", grade: -10 },
{ name: "Charlie", grade: 85 },
{ name: "David", grade: 100 },
{ name: "Eva", grade: "A" }
];
console.log("Average grade:", calculateAverageGrade(studentsList));
// Expected Output: Average grade: 91.66
Tasks
- Identify the bug(s) in the code.
- Fix the code so that it correctly calculates the average grade for the students.
You are tasked with creating a recipe management system. This system will keep track of recipes, each containing:
- The name of the recipe.
- The time it takes to cook the recipe, in minutes.
- A list of ingredients required for the recipe.
Spaghetti
30 min
spaghetti, tomato sauce, garlic
Salad
10 min
lettuce, tomato, cucumber, olive oil
Pancakes
20 min
flour, milk, eggs, sugar
Tasks
- Create a list with the below recipes. Each recipes which each have a name, time, and ingredients.
- Write a method to find the fastest recipe based on the cooking time.
- Write another method to find all recipes that contain specific ingredients. The function should take an array of ingredients as an argument and return all recipes that contain any of those ingredients.
- Describe in your own words to a team mate or mentor what this code does
- The code is hard to read due to poor naming. Can you improve it? (there might be more than just the names to improve)
function filterItems(list) {
let result = [];
for (let i = 0; i < list.length; i++) {
if (list[i].price < 30 && list[i].inStock) {
if (list[i].price < 20) {
let item = list[i].item;
result.push(item);
}
}
}
if (result.length === 0) {
return [];
}
return result;
}
let shpitms = [
{ item: "Apple", price: 1.5, inStock: true },
{ item: "Laptop", price: 999.99, inStock: true },
{ item: "T-shirt", price: 15, inStock: false },
{ item: "Bananas", price: 2, inStock: true },
{ item: "Headphones", price: 25, inStock: true }
];
console.log(filterItems(shpitms));
// Expected Output: ["Apple", "Bananas"]
Tip
The best way to verify your understanding of a topic or some code is to explain it to someone else!
In this exercise, you will learn how to handle errors in JavaScript. Proper error handling is crucial for building robust applications.
-
Learn how errors work in Javascript and explain it to a classmate and a mentor.
- How can I create a custom error?
- What does it mean to "handle" an error?
- What happens if you don't handle errors in your code?
- How can custom error classes improve error handling in your applications?
-
Basic Error Handling
- Write a function
divide(a, b)
that returns the result of dividinga
byb
. - What happens if you call divide(5, 0)
- Make sure the method logs a meaningful error:
Division by zero is not allowed
- Write a function
-
Handling Multiple Errors
- Use a
try...catch
block to handle errors that occurs.
function logOnlySmallNumbers(number) { // make this function throw an error if the number is higher than 100 // otherwise log the number } function logSmallNumbers(numbers) { // make sure this method logs all the small numbers by handling any errors for (const number of numbers) { logOnlySmallNumbers(number); } } logSmallNumbers([40, 150, 9999, 20, -10, 3000]);
- Use a
You will create a basic shopping cart system that calculates the total price, applies discounts, and adds taxes.
-
Create a Visitor Object
- Write a function createVisitor(name, age, height, hasPass) and returns an object with these properties. Height should be in cm.
-
Calculate Ticket Price
- Write a function calculateTicketPrice(visitor).
- Rules:
- Kids (under 12 years) → $10
- Teens (12-17 years) → $15
- Adults (18+) → $20
- Season Pass Holders → Free!
-
Ride Eligibility Check 🎡
- Write a function
canRide(visitor, minHeight)
that returnstrue
if the visitor is tall enough, otherwisefalse
. - Example:
canRide({ name: "Alice", height: 140 }, 120); // true canRide({ name: "Bob", height: 110 }, 120); // false
- Write a function
-
Group Visitors & Total Revenue
- Create an array
visitors
with at least 5 visitor objects. - Write a function
calculateTotalRevenue(visitors)
that:- Loops through all visitors.
- Adds up the total ticket sales.
- Returns the total amount earned by the park.
- Create an array
-
Display Visitor Details
- Write a function
displayVisitorInfo(visitor)
that logs:
Name: Jake | Age: 14 | Height: 150cm | Ticket Price: $15
- Write a function