Exercise Solutions: TypeScript¶
Part 1 - Declare Variables With Type¶
Declare the variables with type based on the information given in the table.
1 2 3 4 5
let spacecraftName: string = 'Determination'; let speedMph: number = 17500; let kilomitersToMars: number = 225000000; let kilometersToTheMoon: number = 384400; let milesPerKilometer: number = 0.621;
Part 2 - Print Days to Mars¶
Use a template literal to print variables.
console.log(`${spacecraftName} would take ${daysToMars} days to get to Mars.`);
Part 3 - Create a Function¶
In the space indicated, define a function that calculates the days it would take to travel to a location.
1 2 3 4 5
function getDaysToLocation(kilometersAway: number): number { let milesAway: number = kilometersAway * milesPerKilometer; let hours: number = milesAway / speedMph; return hours / 24; }
Part 4 - Create a Spacecraft Class¶
Create a Spacecraft
class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Spacecraft { milesPerKilometer: number = 0.621; name: string; speedMph: number; constructor(name: string, speedMph: number) { this.name = name; this.speedMph = speedMph; } getDaysToLocation(kilometersAway: number): number { let milesAway: number = kilometersAway * this.milesPerKilometer; let hoursToMars: number = milesAway / this.speedMph; return hoursToMars / 24; } }
Part 5 - Export and Import the SpaceLocation Class¶
After following the steps in Part 5, your Spacecraft
class should look slightly different:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class Spacecraft {
milesPerKilometer: number = 0.621;
name: string;
speedMph: number;
constructor(name: string, speedMph: number) {
this.name = name;
this.speedMph = speedMph;
}
getDaysToLocation(kilometersAway: number): number {
let milesAway: number = kilometersAway * this.milesPerKilometer;
let hours: number = milesAway / this.speedMph;
return hours / 24;
}
printDaysToLocation(location: SpaceLocation) {
console.log(`${this.name} would take ${this.getDaysToLocation(location.kilometersAway)} days to get to ${location.name}.`);
}
}
|