Home | HTML Hacks | Data Types Hacks | DOM Hacks | JavaScript Hacks | JS Debugging Hacks |
Hacks
Create a JavaScript snippet below with the following requirements:
- Create an object representing yourself as a person. The object should have keys for your name, age, current classes, interests, and two more of your choosing
- Your object must contain keys whose values are arrays. The arrays can be arrays of strings, numbers, or even other objects if you would like
- Print the entire object after declaring it
- Manipulate the arrays within the object and print the entire object as well as the specific changed key afterwards
- Perform mathematical operations on fields in your object such as +, -, /, % etc. and print the results along with a message contextualizing them
- Use typeof to determine the types of at least 3 fields in your object
%%js
const person = { // this whole thing is the object
name: "Brandon So", // my name is a string
age: 16, // my age is a number
currentClasses: ["AP CSP", "AP English Language", "AP Physics", "AP Calculus"],
interests: ["Programming", "Volleyball", "Working Out"],
favoriteMovies: ["Avengers Endgame", "Moneyball"],
hoursOfWorkPerDay: 2,
};
// Print the entire object
console.log("Name:", person.name);
console.log("Age:", person.age);
console.log("Interests:", person.interests);
console.log("Classes:", person.currentClasses);
console.log("Favorite Movies:", person.favoriteMovies);
// Manipulation of the arrays within the object
person.currentClasses.push("US History");
person.interests.pop();
person.favoriteMovies[1] = "The Godfather";
// Print the entire object after manipulation
console.log("Interests:", person.interests);
console.log("Classes:", person.currentClasses);
console.log("Favorite Movies:", person.favoriteMovies);
console.log("Hours of Homework:", person.hoursOfWorkPerDay);
// Perform mathematical operations on fields
const totalHoursOfWorkPerWeek = person.hoursOfWorkPerDay * 7;
const ageInFiveYears = person.age + 5;
// Print results with contextual messages
console.log("Total hours of work per week:", totalHoursOfWorkPerWeek);
console.log("Age in five years:", ageInFiveYears);
// Determine the types of 3 fields in my Object
console.log("Name is a", typeof person.name);
console.log("Age is a", typeof person.age);
console.log("Interests is an", typeof person.interests);
<IPython.core.display.Javascript object>