|
1 | 1 | // URL for the instructions: |
2 | 2 | // https://education.launchcode.org/intro-to-professional-web-dev/chapters/typescript/exercises.html |
3 | 3 |
|
| 4 | +// Part 5: Add import statement here: |
| 5 | +import { SpaceLocation } from './SpaceLocation'; |
| 6 | + |
4 | 7 | // Part 1: Add the 5 variables here |
5 | 8 |
|
6 | | -let spacecraftName: string = "Determination"; |
7 | | -let speedMph: number = 17500; |
8 | 9 | let kilometersToMars: number = 225000000; |
9 | 10 | let kilometersToTheMoon: number = 384400; |
10 | | -let milesPerKilometer: number = 0.621; |
11 | | - |
12 | 11 |
|
13 | 12 | // Code part 2 here: |
14 | | -let milesToMars: number = kilometersToMars * milesPerKilometer; |
15 | | -let hoursToMars: number = milesToMars/speedMph; |
16 | | -let daysToMars: number = hoursToMars/24; |
| 13 | + |
17 | 14 |
|
18 | 15 | // Code the output statement here (use a template literal): |
19 | | -console.log(`${spacecraftName} would take ${daysToMars} days to get to Mars.`); |
| 16 | + |
20 | 17 |
|
21 | 18 |
|
22 | 19 | // Part 3: |
23 | 20 | // Code the "getDaysToLocation" function here: |
24 | 21 |
|
25 | 22 |
|
| 23 | +// Call the function and print the outputs for the Mars trip and the moon trip: |
26 | 24 |
|
27 | 25 |
|
| 26 | +// Part 4: |
| 27 | +/* Move the variables spacecraftName, speedMph, and milesPerKilometer into the |
| 28 | + Spacecraft class. Also move your getDaysToLocation function into the Spacecraft |
| 29 | + class. */ |
| 30 | + |
| 31 | +// Define your Spacecraft class here: |
| 32 | +class Spacecraft { |
| 33 | + name: string; |
| 34 | + speedMph: number; |
| 35 | + milesPerKilometer: number = 0.621; |
| 36 | + |
| 37 | + constructor(name: string, speedMph: number) { |
| 38 | + this.name = name; |
| 39 | + this.speedMph = speedMph; |
| 40 | + } |
| 41 | + |
| 42 | + getDaysToLocation(kilometersAway: number): number { |
| 43 | + let milesAway: number = kilometersAway * this.milesPerKilometer; |
| 44 | + let hoursToLocation: number = milesAway/this.speedMph; |
| 45 | + |
| 46 | + return hoursToLocation/24; |
| 47 | + } |
| 48 | + |
| 49 | + printDaysToLocation(location: SpaceLocation) { |
| 50 | + console.log(`${this.name} would take ${this.getDaysToLocation(location.kilometersAway)} days to get to ${location.name}.`); |
| 51 | + } |
| 52 | + |
| 53 | +} |
| 54 | + |
| 55 | +// Create an instance of the class here: |
| 56 | +let spaceShuttle = new Spacecraft('Determination', 17500) |
28 | 57 |
|
29 | | -// Call the function and print the outputs for the Mars trip and the moon trip: |
30 | 58 |
|
| 59 | +// Print two outputs - one for the trip to Mars and one for the trip to the moon. |
| 60 | +spaceShuttle.printDaysToLocation(new SpaceLocation('Mars', kilometersToMars)); |
| 61 | +spaceShuttle.printDaysToLocation(new SpaceLocation('the Moon', kilometersToTheMoon)); |
0 commit comments