Login to your GitHub account.
Fork the typescript-lc101-projects repository.
Use the terminal to clone your fork from GitHub. If you need a reminder for how to do this, refer to the Git studio.
Use the terminal to navigate into the typescript-lc101-projects
folder,
then into the exercises
subfolder.
$ ls
typescript-lc101-projects
$ cd typescript-lc101-projects
$ ls
exercises studio
$ cd exercises
$ ls
SpaceLocation.ts parts1-5.ts tsconfig.json
Run VSCode and open the typescript-lc101-projects
folder. From the file
tree, select the parts1-5.ts
file.
In the space indicated, declare and assign a variable for each of the following:
Variable Name | Type | Value |
---|---|---|
spacecraftName | string | 'Determination' |
speedMph | number | 17500 |
kilometersToMars | number | 225000000 |
kilometersToTheMoon | number | 384400 |
milesPerKilometer | number | 0.621 |
In the same file you opened in Part 1, do the following.
milesToMars
is a number with the value of
kilometersToMars * milesPerKilometer
.hoursToMars
is a number with the value of
milesToMars / speedMph
.daysToMars
is a number with the value of hoursToMars / 24
.console.log
statement that prints out the days to Mars.${spacecraftName}
and
${daysToMars}
..ts
file, then use the
command node parts1-5.js
to run the JavaScript file created during the
build process.Terminal
$ tsc parts1-5.ts
$ node parts1-5.js
Determination would take 332.67857142857144 days to get to Mars.
getDaysToLocation
kilometersAway
must be a number.milesAway
and hoursToLocation
instead of milesToMars
and hoursToMars
.number
.${getDaysToLocation(kilometersToMars)}
and ${spacecraftName}
.${getDaysToLocation(kilometersToTheMoon)}
and ${spacecraftName}
..ts
file, then run the
parts1-5.js
file again.Terminal
$ tsc parts1-5.ts
$ node parts1-5.js
Determination would take 332.67857142857144 days to get to Mars.
Determination would take 0.5683628571428571 days to get to the Moon.
Organize getDaysToLocation
and the variables for name, speed, and miles per
kilometer by moving them into a class.
Define a class named Spacecraft
.
milesPerKilometer: number = 0.621;
name: string;
speedMph: number;
name
is the first parameter and it MUST be a string.speedMph
is the second parameter and it MUST be a number.this.name
and this.speedMph
.Note
Once you complete the constructor, be sure to remove the variables you
defined in part 1 (spacecraftName
, milesPerKilometer
, and
speedMph
.
Move the function getDaysToLocation
, defined in Part 3, into the
Spacecraft
class.
this.milesPerKilometer
and this.speedMph
.Create an instance of the Spacecraft
class.
let spaceShuttle = new Spacecraft('Determination', 17500);
Print out the days to Mars.
${spaceShuttle.getDaysToLocation(kilometersToMars)}
and
${spaceShuttle.name}
.Print out the days to the Moon.
${spaceShuttle.getDaysToLocation(kilometersToTheMoon)}
and
${spaceShuttle.name}
.Use the terminal in VSCode to recompile your .ts
file, then run the
.js
file again.
Terminal
$ tsc parts1-5.ts
$ node parts1-5.js
Determination would take 332.67857142857144 days to get to Mars.
Determination would take 0.5683628571428571 days to get to the Moon.
From the file tree in VSCode, open the SpaceLocation.ts
file.
Paste in the code provided below.
export
keyword. That is what allows us to import it later.1export class SpaceLocation {
2 kilometersAway: number;
3 name: string;
4
5 constructor(name: string, kilometersAway: number) {
6 this.name = name;
7 this.kilometersAway = kilometersAway;
8 }
9}
Add the function printDaysToLocation
to the Spacecraft
class.
SpaceLocation
.1printDaysToLocation(location: SpaceLocation) {
2 console.log(`${this.name} would take ${this.getDaysToLocation(location.kilometersAway)} days to get to ${location.name}.`);
3}
Import SpaceLocation
into parts1-5.ts
.
import { SpaceLocation } from './SpaceLocation';
to the top of
parts1-5.ts
.Replace the earlier console.log
statements by using the class instance
to print out the days to Mars and the Moon.
47spaceShuttle.printDaysToLocation(new SpaceLocation('Mars', kilometersToMars));
48spaceShuttle.printDaysToLocation(new SpaceLocation('the Moon', kilometersToTheMoon));
Use the terminal in VSCode to compile your .ts
file, then run the
.js
file again.
Terminal
$ tsc parts1-5.ts
$ node parts1-5.js
Determination would take 332.67857142857144 days to get to Mars.
Determination would take 0.5683628571428571 days to get to the Moon.
The typescript-lc101-projects
repository has two branches---master
and
solutions
. 'Nuff said.