Javascript Data Types/Lists
A Tech Talk on javascript data types and how to use with lists
Home | HTML | Data Types | DOM | JavaScript | JS Debugging | Review Ticket |
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
- [X]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
// This defines the variable under which all the information is typed in
const personObject = {
Name: "Sri Vaidyanathan Suryanarayanan",
Age: 15,
CurrentClasses: ["Physics 1", "AP Computer Science Principles", "Photography 1", "Math 3A", "AP Chemistry"],
Interests: ["Photography", "New Experiences", "Traveling the world", "Understanding people", "Exploring national parks"],
FavoriteMovies: [
{
name: "Interstellar",
director: "Christopher Nolan",
lead: "Matthew McConaughey",
},
{
name: "Inception",
director: "Christopher Nolan",
lead: "Leonardo DiCaprio",
},
{
name: "Spirited",
director: "Sean Anders",
lead: "Will Ferrell and Ryan Reynolds",
},
],
favoriteArtists: [
{
name: "Taylor Swift",
favoriteSong: "Lover"
},
{
name: "Morgan Wallen",
favoriteSong: "Last Night",
},
{
name: "Olivia Rodrigo",
favoriteSong: "good 4 u",
},
],
// These values are used later for calculating stuff like BMI which fulfill the calculations requirement of this assignment.
heightInFeet: 70.8,
weightInPounds: 160,
};
// This is to print the initial version of my database before making any changes like add or pull.
console.log("Before changes by manipulating data:");
console.log(personObject);
// Manipulating the objects already in the data requires one to pull or push, and by the requirement in the rubric, two changes have been made to the original code, a new song is added and a movie is taken out of the results on the console.
personObject.FavoriteMovies = personObject.FavoriteMovies.filter(movie => movie.name !== "Spirited");
personObject.favoriteArtists.push({ name: "Ed Sheeran", favoriteSong: "perfect" });
// These code lines below detail a few mathematical functions created using data from above, like the age, height, and weight, etc.
const bmi = (personObject.weightInPounds / (personObject.heightInFeet * personObject.heightInFeet)) * 703;
const ageInMonths = personObject.Age * 12;
const ageInDays = personObject.Age * 365;
const ageInWeeks = personObject.Age * 52.2;
// This prints out the output after the mild data manipulation done above to the existing inputs.
console.log("\nThis is after manipulation:");
console.log(personObject);
// This prints out all the mathematical functions, the four functions that have been coded above which includes the days etc
console.log("\nBMI (Body Mass Index):", bmi.toFixed(2));
console.log("Age in Months:", ageInMonths);
console.log("Age in Days:", ageInDays);
console.log("Age In Weeks:", ageInWeeks);
// Below here the command typeof is used to determine the types of data.
console.log("\nData Types:");
console.log("Name:", typeof personObject.Name);
console.log("Age:", typeof personObject.Age);
console.log("Current Classes:", typeof personObject.CurrentClasses);
console.log("Interests:", typeof personObject.Interests);
console.log("Favorite Movies:", typeof personObject.FavoriteMovies);
console.log("Favorite Artists:", typeof personObject.favoriteArtists);
<IPython.core.display.Javascript object>
<a href="https://www.youtube.com/shorts/fuXUDFSyZQY"> Results</a>