Home HTML Data Types DOM JavaScript JS Debugging

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 with console.log after declaring it
  • Manipulate the arrays within the object and print the entire object with console.log as well as the specific changed key afterwards
  • Perform mathematical operations on fields in your object such as +, -, /, % etc. and print the results with console.log along with a message contextualizing them
  • Use typeof to determine the types of at least 3 fields in your object
%%js
//Create an object representing myself as a person
const person = {
  name: "Batman",
  age: 36,
  villans: ["Penguin", "Joker", "Two-Face", "Bane"],
  interests: ["Fighting Crime", "Justice", "Balling out",],
  heightInInches: 75,
  weightInPounds: 172,
};
// Print the entire object
console.log("Original Object:");
console.log(person);
// Manipulate the arrays within the object
person.villans.push("The Riddler");
person.interests.shift();
// Print the entire object after manipulation
console.log("\nObject after Manipulation:");
console.log(person);
// Perform mathematical operations on fields
const bmi = (person.weightInPounds / (person.heightInInches * person.heightInInches)) * 703;
const yearsUntilRetirement = 65 - person.age;
// Print results with context
console.log("\nCalculations:");
console.log(`BMI: ${bmi.toFixed(2)}`);
console.log(`Years until retirement: ${yearsUntilRetirement}`);
// Use typeof to determine field types
console.log("\nField Types:");
console.log(`Name is a ${typeof person.name}`);
console.log(`Age is a ${typeof person.age}`);
console.log(`Interests is an array: ${Array.isArray(person.interests)}`);
<IPython.core.display.Javascript object>