Homework

The Solar System

Create an HTML page and a JavaScript file and link the two.

The following object, solarSystem, is a common data structure: an array of objects. You will see this particular data structure more and more as you venture into JSON and servers. This one is an array of objects which also contains arrays.

Note that the objects do not have names. They are just anonymous objects listed in an array.

Copy and paste this object into your .js file:

const solarSystem = [
  { name: 'Mercury', ringSystem: false, moons: [] },
  { name: 'Venus', ringSystem: false, moons: [] },
  { name: 'Earth', ringSystem: false, moons: ['The Moon'] },
  { name: 'Mars', ringSystem: false, moons: ['Phobos', 'Deimos'] },
  { name: 'Jupiter', ringSystem: true, moons: ['Europa', 'Ganymede', 'Io', 'Callisto'] },
  { name: 'Saturn', ringSystem: true, moons: ['Titan', 'Enceladus', 'Rhea', 'Mimas'] },
  { name: 'Uranus', ringSystem: true, moons: ['Miranda', 'Titania', 'Ariel', 'Umbriel'] },
  { name: 'Neptune', ringSystem: true, moons: ['Triton', 'Nereid'] }
]

Write JavaScript that does the following:

  1. Print the array of Jupiter's moons to the console (no for loop, just print the entire array).

  2. Print the name of Neptune's moon "Nereid" to the console.

  3. Add a new moon called "Endor" to Venus' moons array.

  4. Add a Pluto object to the solarSystem array using .push. The object should contain Pluto's name, ringSystem boolean, and moons array (which includes "Charon").

  5. Add a new key value pair to the the Earth object: the key should be 'diameter', and the value should be Earth's diameter in miles represented as a string.

  6. Change Mercury's ringSystem boolean to true.

  7. Change Uranus' moon "Umbriel" to "Oberon"

  8. Iterate through the solarSystem array and print only the objects that have a ringSystem (where ringSystem: true), and ignore the others. You want to make a loop that goes over each item in the array, and include an if statement.

    Example output:

    { name: 'Mercury', ringSystem: true, moons: [] }
    { name: 'Jupiter', ringSystem: true, moons: [ 'Europa', 'Ganymede', 'Io', 'Callisto' ] }
    { name: 'Saturn', ringSystem: true, moons: [ 'Titan', 'Enceladus', 'Rhea', 'Mimas' ] }
    { name: 'Uranus', ringSystem: true, moons: [ 'Miranda', 'Titania', 'Ariel', 'Oberon' ] }
    { name: 'Neptune', ringSystem: true, moons: [ 'Triton', 'Nereid' ] }

Friends

Create a separate HTML page and a JavaScript file and link the two. Copy the code block below into your .js file and answer the numbered questions.

const users = {
  theresa: {
    fullName: 'Theresa Smith',
    admin: true,
    age: 45,
    friends: ['freddie', 'meredith']
  },
  freddie: {
    fullName: 'Frederick Jones',
    admin: false,
    age: 32,
    friends: ['meredith']
  },
  meredith: {
    fullName: 'Meredith Johnson',
    admin: false,
    age: 56,
    friends: ['freddie', 'sally', 'theresa']
  },
  sally: {
    fullName: 'Sally Brown',
    admin: false,
    age: 28,
    friends: ['meredith']
  }
}


// 1. How would you access theresa's full name (i.e. the string "Theresa Smith")?


// 2. How would you access Freddie's age?


// 3. Create a variable `username` and set it to `sally`
// Write an `if` to see if `username` is an admin or not. It should output either "Is an Admin" or "Not an admin"
// (try it out with different values for the `username` variable to check it works)


// 4. How would you add a new friend "sally" to Freddie's list of friends?


// 5. How would you add yourself to the users object? (fill out the same info as everyone else).


// 6. Write a loop that outputs the full names of each of Meredith's friends.

Last updated