Created
November 7, 2016 23:52
-
-
Save basekays/514010982f5e2f26bc422b5e655d0468 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //your code here! | |
| /* | |
| Q1. Using your knowledge of JavaScript data types (i.e. numbers, strings, booleans, | |
| arrays and objects), represent our solar system as data (hint: the link is to a | |
| Wikipedia article – you should use it). You can include as many attributes as you | |
| like, but the following are required: | |
| - Age of the Solar System | |
| - Distance from the center of the galaxy | |
| - Number of known comets | |
| - The Planets | |
| * Name | |
| *Size (in Earth-masses) | |
| *Distance of each from sun | |
| *Number of known satellites (orbiting bodies, e.g moons) | |
| */ | |
| var solarsystem = { | |
| age: "4.568 billion years", | |
| distance: "27,000±1,000 ly", | |
| comets: "3,406", | |
| planet1: { | |
| name: "Earth", | |
| size: "size", | |
| distance: "distance", | |
| satellites: "satellites" | |
| }, | |
| planet2: { | |
| name: "Earth", | |
| size: "size", | |
| distance: "distance", | |
| satellites: "satellites" | |
| }, | |
| planet3: { | |
| name: "Earth", | |
| size: "size", | |
| distance: "distance", | |
| satellites: "satellites" | |
| } | |
| } | |
| function solarSystem(age, distanceFromGalaxy, numOfComets, planet) { | |
| return { | |
| age: age, | |
| distance: distanceFromGalaxy, | |
| comets: numOfComets, | |
| planets: planet | |
| } | |
| } | |
| function planetMaker(name, size, distanceFromSun, numOfSatellites) { | |
| return { | |
| name: name, | |
| size: size, | |
| distance: distanceFromSun, | |
| satellites: numOfSatellites | |
| } | |
| } | |
| var mercury = planetMaker("Mercury", "2,440 km radius", "57.91 million km", 0); | |
| var venus = planetMaker("Venus", "6,052 km radius","108.2 million km", 0); | |
| var earth = planetMaker("Earth", "6,37 km radius", "149.6 million km", 1); | |
| var mars = planetMaker("Mars", "3,390 km radius", "227.9 million km", 2); | |
| var jupiter = planetMaker("Jupiter", "69,911 km radius", "778.5 million km", 63); | |
| var saturn = planetMaker("Saturn", "58,232 km radius", "1.429 billion km", 60); | |
| var uranus = planetMaker("Uranus", "25,362 km radius", "2.871 billion km", 27); | |
| var neptune = planetMaker("Neptune", "24,622 km radius", "4.498 billion km", 13); | |
| var planets = [mercury, earth, mars, jupiter, saturn, uranus, neptune]; | |
| solarSystem("4.568 billion years", "27,000±1,000 ly", "3,406", planets); | |
| //Q2 | |
| /* | |
| Complete the below function called range that takes two integers as parameters, | |
| start and end, and returns an array containing all the whole numbers between them | |
| starting with start and up to end (you can use a for loop, while loop, each, or | |
| repetition with function invocation). The function definition should look like this: | |
| function range(start, end) { | |
| // YOUR CODE HERE | |
| } | |
| range(0, 4); // => [0, 1, 2, 3] | |
| range(2, 7); // => [2, 3, 4, 5, 6] | |
| range(10, 10); // => [] | |
| range(10, 2); // => [] | |
| */ | |
| //for | |
| function range(start, end) { | |
| var resultArr = []; | |
| for (var counter = start; counter<end; counter++) { | |
| resultArr.push(counter); | |
| } | |
| return resultArr; | |
| } | |
| //Q3 | |
| /* | |
| Given the following array of people, write a function that, when passed people as a | |
| parameter, returns the person (that is, your function should return an object) with | |
| the longest name (first, middle & last). | |
| */ | |
| var people = [ | |
| {name: {first: "Alyssa", middle: "P.", last: "Hacker"}, age: 26}, | |
| {name: {first: "Ben", last: "Bitdiddle"}, age: 34}, | |
| {name: {first: "Eva", middle: "Lu", last: "Ator"}, age: 40}, | |
| {name: {first: "Lem", middle: "E.", last: "Tweakit"}, age: 45}, | |
| {name: {first: "Louis", last: "Reasoner"}, age: 21} | |
| ]; | |
| /* | |
| HINT: It might be helpful to have a fullName function that, when given a person as a | |
| parameter, returns a person's full name. | |
| */ | |
| function fullName(person) { | |
| for (var i=0; i<people.length; i++) { | |
| if (person === people[i].name.first) { | |
| if (people[i].name.middle === undefined) { | |
| return people[i].name.first + " " + people[i].name.last; | |
| } | |
| return people[i].name.first + " " + people[i].name.middle + " " + people[i].name.last; | |
| } | |
| } | |
| } | |
| function longestName(people){ | |
| var longest = ""; | |
| for (var i=0; i<people.length; i++) { | |
| if (longest.length < fullName(people[i].name.first).length) { | |
| longest = fullName(people[i].name.first); | |
| } | |
| } | |
| return longest; | |
| } | |
| longestName(people); | |
| // => {name: {first: "Alyssa", middle: "P.", last: "Hacker"}, age: 26} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment