title | type | competencies | creator | ||||
---|---|---|---|---|---|---|---|
Solar System |
homework |
JS basics |
|
- Gain practice accessing and manipulating elements in arrays and objects.
- Gain experience with Git: add and commit your code frequently.
- Introduce automated tests to test your code.
- Gain valuable debugging skills when tests fail.
- Write your first bug report.
cd
into today's homework directory, and run npm install
in terminal -- this will download any assignment-specific JavaScript dependencies into to a directory called node_modules
.
There are automated tests built into this assignment so that you can check your work. You can run these tests by running npm test
in terminal in the homework
directory. Ideally, you should run the tests after each problem. You will have to scroll up to see the summary.
Because tests are provided for you, do not change the variable names. Here is an example of completing the first problem and running npm test
:
Here is an example of all tests passing:
- Follow the prompts below. Given the
solarSystem
data structure... - Write your javascript code in
homework.js
. - Open up a new tab in terminal (
cmd
+t
). Runnpm test
to check each solution. - When you're ready to submit, you will take a screen shot of your tests (like above) and submit that along with the normal homework submission format.
Remember, we are not "hard coding" values to the variables. Use your knowledge to access and change the data. Refer to the class notes if you need to. If the tests pass, but you have hard-coded the values, you won't receive credit for the problem.
Top Tip! If your tests aren't passing, use your debugging skills to check for: syntax, data type (ie. does it ask for an integer or a string?), console log your variables, console log the data type with typeof
, pseudocode, etc. Remember, you can run your code in terminal with node homework.js
.
var solarSystem = [
{ name: "Mercury", ringSystem: false, moons: [] },
{ name: "Venus", ringSystem: false, moons: [] },
{ name: "Earth", ringSystem: false, moons: ["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"] },
{ name: "Pluto", ringSystem: false, moons: ["Charon", "Styx", "Nix", "Kerberos", "Hydra"] }
];
- Set
marsMoons
to the array of Mars's moons. var marsMoons = solarSystem[3].moons;
- Set
jupiterMoons
to the array of Jupiter's moons. - 🎯 Commit -m "Problem 1 passed"
- Set
neptuneMoon
to Neptune's moon "Nereid". - 🎯 Commit -m "Problem 2 passed"
- Add a new moon called "Endor" to Venus's moons array. Save this moon in
venusMoon
. - 🎯 Commit -m "Problem 3 passed"
- Set
plutoMoonCount
to the total number of Pluto's moons. - 🎯 Commit -m "Problem 4 passed"
- Change Uranus' moon "Umbriel" to "Oberon".
- 🎯 Commit -m "Problem 5 passed"
- Add a new key value pair to the the Earth object: the key should be 'diameter', and the value should be Earth's diameter (hint: it's 7917.5 miles) as a number.
- 🎯 Commit -m "Problem 6 passed"
- Whoops! There's an error in our array. Change Mercury's ringSystem boolean to true.
- 🎯 Commit -m "Problem 7 passed"
- Pluto was considered a planet in our solar system from 1930 - 2006. Bye, Pluto we miss you! :( Remove the Pluto object from
solarSystem
and save the new solarSystem to thenewSolarSystem
variable. - 🎯 Commit -m "Problem 8 passed"
- Using a for loop, calculate the total number of moons in the
newSolarSystem
and save that number in thetotalMoons
variable. - 🎯 Commit -m "Problem 9 passed"
- Using a
while
loop, iterate throughnewSolarSystem
and push all of the objects' name values into an array. Assign the array to theallPlanets
variable.
// Expected output for allPlanets:
[ 'Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune' ]
- 🎯 Commit -m "Problem 10 passed"
- Write a
while
loop with an if/else statement so that only the objects with a ringSystem (where ringSystem: true) name values gets pushed into an array. Assign the array to theringPlanets
variable.
// Expected output for ringPlanets:
[ 'Mercury', 'Jupiter', 'Saturn', 'Uranus', 'Neptune' ]
- 🎯 Commit -m "Problem 11 passed"
- This one's a little tricky. Write a loop (your choice) that iterates through
newSolarSystem
so that only moon names that are an even amount of letters long get stored as an array, saved in theevenMoons
variable AND the moon names that are odd-numbered in length get stored as an array inoddMoons
.
// Expected output for oddMoons:
[ 'Endor', 'Titan', 'Enceladus', 'Mimas', 'Miranda', 'Titania', 'Ariel']
//Expected output for evenMoons:
[ 'Moon', 'Phobos', 'Deimos', 'Europa', 'Ganymede', 'Io', 'Callisto', 'Rhea', 'Triton', 'Nereid' ]
- 🎯 Commit -m "Problem 12 passed"
You've already written Javascript functions in Fundamentals, and tomorrow we are going to review Javascript functions. Have a stab at this problem! There are tests provided for this as well.
In homework.js
, a calc
function has been created. Build the function so that:
- the function takes 3 parameters: num1, num, and operation (in that order)
- if the function receives the string "add" for the operation parameter, it will return the sum of num1 and num2
- "sub" will return the difference between num1 and num2.
- "mul" will return the product
- "div" will return the quotient
- "exp" will return num1 to the power of num2
- 🎯 Commit -m "YAAAS! Reach Goal passed"
When you're ready, create an issue on the class repo with a title in the format "First and Last Name -- Week XX Day XX". The issue body should have:
- A link to your forked repo (ie. to your
homework
folder) - A 'comfort' score on how you feel about your answers, from 1 (very uncomfortable) to 5 (very comfortable)
- A 'completeness' score, from 1 (didn't do it) to 5 (finished all of it)
- A 'win'
- A 'struggle'
- A 'comment'
- A screen shot of your tests (like above).