0% found this document useful (0 votes)
3 views7 pages

Logbook Format

The document contains a logbook answersheet with programming exercises related to JavaScript objects and functions. It includes tasks such as creating a car object, calculating a student's average score, and updating a course object. Each question is followed by a structured answer demonstrating the required coding techniques.

Uploaded by

jsphere16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Logbook Format

The document contains a logbook answersheet with programming exercises related to JavaScript objects and functions. It includes tasks such as creating a car object, calculating a student's average score, and updating a course object. Each question is followed by a structured answer demonstrating the required coding techniques.

Uploaded by

jsphere16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Subject Name Here

Logbook answersheet

Name:
MSU Enrollment Number:
Section A
Question 1:
Create an object representing a car with properties like brand, model, and year.
Access each property using both dot and bracket notation.

Answer:

// Creating an object representing a car


const car = {
brand: "Toyota",
model: "Camry",
year: 2022
};

// Accessing properties using dot notation


console.log("Using dot notation:");
console.log("Brand:", car.brand);
console.log("Model:", car.model);
console.log("Year:", car.year);

// Accessing properties using bracket notation


console.log("\nUsing bracket notation:");
console.log("Brand:", car["brand"]);
console.log("Model:", car["model"]);
console.log("Year:", car["year"]);

// Adding proper comments


// 'car' is an object with properties 'brand', 'model', and 'year'.
// Properties are accessed using both dot notation (car.property) and bracket
notation (car["property"]).

// Output:
// Using dot notation:
// Brand: Toyota
// Model: Camry
// Year: 2022
// Using bracket notation:
// Brand: Toyota
// Model: Camry
// Year: 2022

Question 2: xyz
Answer: same format as Question 1

Question 3: xyz
Answer: same format as Question 1

Question 4: xyz
Answer: same format as Question 1
Section B
Question 1:
Write a function that takes a student object as input and calculates the average
score of the student. The student object should have the following structure:
javascript
Copy code
const student = {
name: "John Doe",
scores: [85, 90, 78, 92, 88]
};
Return the average score and print it in the console.

Answer:
// Function to calculate the average score of a student
function calculateAverage(student) {
// Destructure the scores array from the student object
const { scores } = student;

// Calculate the total score using reduce


const totalScore = scores.reduce((sum, score) => sum + score, 0);

// Calculate the average by dividing the total score by the number of scores
const averageScore = totalScore / scores.length;

// Return the average score


return averageScore;
}

// Example student object


const student = {
name: "John Doe",
scores: [85, 90, 78, 92, 88]
};

// Calculate and print the average score


const average = calculateAverage(student);
console.log(`The average score of ${student.name} is: ${average.toFixed(2)}`);

// Output:
// The average score of John Doe is: 86.60

Question 2: xyz
Answer: same format as Question 1

Question 3: xyz
Answer: same format as Question 1

Section C

Question 1:

You are given an object representing a course with properties name and students
(an array of student names). Write a function that:

● Adds a new student to the course.


● Removes the last student from the course.
● Returns the updated object.

Answer:

// Function to update the course object


function updateCourse(course, newStudent) {
// Add the new student to the course
course.students.push(newStudent);

// Remove the last student from the course


const removedStudent = course.students.pop();

// Return the updated course object


return course;
}

// Example course object


const course = {
name: "JavaScript Fundamentals",
students: ["Alice", "Bob", "Charlie"]
};

// Adding a new student and updating the course


const updatedCourse = updateCourse(course, "David");

// Display the updated course object


console.log("Updated Course:", updatedCourse);

/*
Expected Output:
Updated Course: {
name: "JavaScript Fundamentals",
students: ["Alice", "Bob", "Charlie"]
}
*/

Question 2: xyz
Answer: same format as Question 1
Question 3: xyz
Answer: same format as Question 1

You might also like