Skip to content

Solutions #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode/
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cSpell.ignoreWords": [
"esnext"
]
}
10 changes: 10 additions & 0 deletions exercises/SpaceLocation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";
exports.__esModule = true;
var SpaceLocation = /** @class */ (function () {
function SpaceLocation(name, kilometersAway) {
this.name = name;
this.kilometersAway = kilometersAway;
}
return SpaceLocation;
}());
exports.SpaceLocation = SpaceLocation;
9 changes: 9 additions & 0 deletions exercises/SpaceLocation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class SpaceLocation {
kilometersAway: number;
name: string;

constructor(name: string, kilometersAway: number) {
this.name = name;
this.kilometersAway = kilometersAway;
}
}
15 changes: 0 additions & 15 deletions exercises/parts1-3.ts

This file was deleted.

33 changes: 33 additions & 0 deletions exercises/parts1-5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"use strict";
// URL for the instructions:
// https://education.launchcode.org/intro-to-professional-web-dev/chapters/typescript/exercises.html
exports.__esModule = true;
// Part 5: Import statement.
var SpaceLocation_1 = require("./SpaceLocation");
// Part 1: Remaining variables.
var kilometersToMars = 225000000;
var kilometersToTheMoon = 384400;
// Part 2: Content moved into class. Output statements updated.
// Part 3: Content moved into class. Output statements updated.
// Part 4: Define your Spacecraft class:
var Spacecraft = /** @class */ (function () {
function Spacecraft(name, speedMph) {
this.milesPerKilometer = 0.621;
this.name = name;
this.speedMph = speedMph;
}
Spacecraft.prototype.getDaysToLocation = function (kilometersAway) {
var milesAway = kilometersAway * this.milesPerKilometer;
var hours = milesAway / this.speedMph;
return hours / 24;
};
Spacecraft.prototype.printDaysToLocation = function (location) {
console.log(this.name + " would take " + this.getDaysToLocation(location.kilometersAway) + " days to get to " + location.name + ".");
};
return Spacecraft;
}());
// Create an instance of the class here:
var spaceShuttle = new Spacecraft('Determination', 17500);
// Part 5: Paste in the code from step 6 here:
spaceShuttle.printDaysToLocation(new SpaceLocation_1.SpaceLocation('Mars', kilometersToMars));
spaceShuttle.printDaysToLocation(new SpaceLocation_1.SpaceLocation('the Moon', kilometersToTheMoon));
46 changes: 46 additions & 0 deletions exercises/parts1-5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// URL for the instructions:
// https://education.launchcode.org/intro-to-professional-web-dev/chapters/typescript/exercises.html

// Part 5: Import statement.
import { SpaceLocation } from './SpaceLocation';

// Part 1: Remaining variables.
let kilometersToMars: number = 225000000;
let kilometersToTheMoon: number = 384400;

// Part 2: Content moved into class. Output statements updated.


// Part 3: Content moved into class. Output statements updated.


// Part 4: Define your Spacecraft class:

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}.`);
}
}

// Create an instance of the class here:
let spaceShuttle = new Spacecraft('Determination', 17500);


// Part 5: Paste in the code from step 6 here:
spaceShuttle.printDaysToLocation(new SpaceLocation('Mars', kilometersToMars));
spaceShuttle.printDaysToLocation(new SpaceLocation('the Moon', kilometersToTheMoon));
20 changes: 0 additions & 20 deletions exercises/parts4-5.ts

This file was deleted.

32 changes: 16 additions & 16 deletions exercises/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"compilerOptions": {
"target": "es6",
"lib": ["esnext", "dom"],
"module": "commonjs",
"moduleResolution": "node",
"strict": true,
"jsx": "react",
"allowJs": true,
"sourceMap": true,
"inlineSources": true,
"types": ["node"],
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true
}
}
{
"compilerOptions": {
"target": "es6",
"lib": ["esnext", "dom"],
"module": "commonjs",
"moduleResolution": "node",
"strict": true,
"jsx": "react",
"allowJs": true,
"sourceMap": true,
"inlineSources": true,
"types": ["node"],
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true
}
}

Empty file removed studio.ts
Empty file.
10 changes: 10 additions & 0 deletions studio/Astronaut.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";
exports.__esModule = true;
var Astronaut = /** @class */ (function () {
function Astronaut(massKg, name) {
this.name = name;
this.massKg = massKg;
}
return Astronaut;
}());
exports.Astronaut = Astronaut;
11 changes: 11 additions & 0 deletions studio/Astronaut.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Payload } from './Payload';

export class Astronaut implements Payload {
massKg: number;
name: string;

constructor (massKg: number, name: string) {
this.name = name;
this.massKg = massKg;
}
}
10 changes: 10 additions & 0 deletions studio/Cargo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";
exports.__esModule = true;
var Cargo = /** @class */ (function () {
function Cargo(massKg, material) {
this.material = material;
this.massKg = massKg;
}
return Cargo;
}());
exports.Cargo = Cargo;
11 changes: 11 additions & 0 deletions studio/Cargo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Payload } from './Payload';

export class Cargo implements Payload {
massKg: number;
material: string;

constructor (massKg: number, material: string) {
this.material = material;
this.massKg = massKg;
}
}
2 changes: 2 additions & 0 deletions studio/Payload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
exports.__esModule = true;
3 changes: 3 additions & 0 deletions studio/Payload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface Payload {
massKg: number;
}
43 changes: 43 additions & 0 deletions studio/Rocket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"use strict";
exports.__esModule = true;
var Rocket = /** @class */ (function () {
function Rocket(name, totalCapacityKg) {
this.cargoItems = [];
this.astronauts = [];
this.name = name;
this.totalCapacityKg = totalCapacityKg;
}
Rocket.prototype.sumMass = function (items) {
var sum = 0;
for (var i = 0; i < items.length; i++) {
sum += items[i].massKg;
}
return sum;
};
Rocket.prototype.currentMassKg = function () {
return this.sumMass(this.astronauts) + this.sumMass(this.cargoItems);
};
Rocket.prototype.canAdd = function (item) {
return (this.currentMassKg() + item.massKg) <= this.totalCapacityKg;
};
Rocket.prototype.addCargo = function (cargo) {
if (this.canAdd(cargo)) {
this.cargoItems.push(cargo);
return true;
}
else {
return false;
}
};
Rocket.prototype.addAstronaut = function (astronaut) {
if (this.canAdd(astronaut)) {
this.astronauts.push(astronaut);
return true;
}
else {
return false;
}
};
return Rocket;
}());
exports.Rocket = Rocket;
50 changes: 50 additions & 0 deletions studio/Rocket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Payload } from './Payload';
import { Cargo } from './Cargo';
import { Astronaut } from './Astronaut';

export class Rocket implements Payload {
name: string;
totalCapacityKg: number;
cargoItems: Cargo[] = [];
astronauts: Astronaut[] = [];
massKg: number;

constructor (name: string, totalCapacityKg: number) {
this.name = name;
this.totalCapacityKg = totalCapacityKg;
}

sumMass (items: Payload[]): number {
let sum: number = 0;
for (let i=0; i < items.length; i++) {
sum += items[i].massKg;
}
return sum;
}

currentMassKg (): number {
return this.sumMass(this.astronauts) + this.sumMass(this.cargoItems);
}

canAdd(item: Payload): boolean {
return (this.currentMassKg() + item.massKg) <= this.totalCapacityKg;
}

addCargo(cargo: Cargo): boolean {
if (this.canAdd(cargo)) {
this.cargoItems.push(cargo);
return true;
} else {
return false;
}
}

addAstronaut(astronaut: Astronaut): boolean {
if (this.canAdd(astronaut)) {
this.astronauts.push(astronaut);
return true;
} else {
return false;
}
}
}
43 changes: 43 additions & 0 deletions studio/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"use strict";
// Instructions are published in the online book. The URL is:
// https://education.launchcode.org/intro-to-professional-web-dev/chapters/typescript/studio.html
exports.__esModule = true;
var Astronaut_1 = require("./Astronaut");
var Cargo_1 = require("./Cargo");
var Rocket_1 = require("./Rocket");
var falcon9 = new Rocket_1.Rocket('Falcon 9', 7500);
var astronauts = [
new Astronaut_1.Astronaut(75, 'Mae'),
new Astronaut_1.Astronaut(81, 'Sally'),
new Astronaut_1.Astronaut(99, 'Charles')
];
for (var i = 0; i < astronauts.length; i++) {
var astronaut = astronauts[i];
var status_1 = '';
if (falcon9.addAstronaut(astronaut)) {
status_1 = "On board";
}
else {
status_1 = "Not on board";
}
console.log(astronaut.name + ": " + status_1);
}
var cargo = [
new Cargo_1.Cargo(3107.39, "Satellite"),
new Cargo_1.Cargo(1000.39, "Space Probe"),
new Cargo_1.Cargo(753, "Water"),
new Cargo_1.Cargo(541, "Food"),
new Cargo_1.Cargo(2107.39, "Tesla Roadster"),
];
for (var i = 0; i < cargo.length; i++) {
var c = cargo[i];
var loaded = '';
if (falcon9.addCargo(c)) {
loaded = "Loaded";
}
else {
loaded = "Not loaded";
}
console.log(c.material + ": " + loaded);
}
console.log("Final cargo and astronaut mass: " + falcon9.currentMassKg() + " kg.");
Loading