diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..dbe9c82b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode/ \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..90a54399 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "cSpell.ignoreWords": [ + "esnext" + ] +} \ No newline at end of file diff --git a/exercises/SpaceLocation.js b/exercises/SpaceLocation.js new file mode 100644 index 00000000..3977adb7 --- /dev/null +++ b/exercises/SpaceLocation.js @@ -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; diff --git a/exercises/SpaceLocation.ts b/exercises/SpaceLocation.ts index e69de29b..fe5fb77f 100644 --- a/exercises/SpaceLocation.ts +++ b/exercises/SpaceLocation.ts @@ -0,0 +1,9 @@ +export class SpaceLocation { + kilometersAway: number; + name: string; + + constructor(name: string, kilometersAway: number) { + this.name = name; + this.kilometersAway = kilometersAway; + } +} \ No newline at end of file diff --git a/exercises/parts1-3.ts b/exercises/parts1-3.ts deleted file mode 100644 index 830b4fe1..00000000 --- a/exercises/parts1-3.ts +++ /dev/null @@ -1,15 +0,0 @@ -// URL for the instructions: -// https://education.launchcode.org/intro-to-professional-web-dev/chapters/typescript/exercises.html - -// Part 1: Add the 5 variables here - - - - - -// Code part 2 here: - - - - -// Part 3: Define the 'getDaysToLocation' function here: diff --git a/exercises/parts1-5.js b/exercises/parts1-5.js new file mode 100644 index 00000000..ecc38af9 --- /dev/null +++ b/exercises/parts1-5.js @@ -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)); diff --git a/exercises/parts1-5.ts b/exercises/parts1-5.ts new file mode 100644 index 00000000..6b1141f7 --- /dev/null +++ b/exercises/parts1-5.ts @@ -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)); \ No newline at end of file diff --git a/exercises/parts4-5.ts b/exercises/parts4-5.ts deleted file mode 100644 index 90f12597..00000000 --- a/exercises/parts4-5.ts +++ /dev/null @@ -1,20 +0,0 @@ -// Part 5 - Add your import statement to line 2: - - -let kilometersToMars: number = 225000000; -let kilometersToTheMoon: number = 384400; - - -// The variables that are commented out will be moved into the Spacecraft class -// let spaceCraft: string = "Space Shuttle"; -// let speedMph: number = 17500; -// let milesPerKilometer: number = 0.621; - -// This function will also be moved into the Spacecraft class -// function getDaysToLocation(kilometersAway: number): number { -// let milesAway: number = kilometersAway * milesPerKilometer; -// let hours: number = milesAway / speedMph; -// return hours / 24; -// } - -// Part 4 - Define your Spacecraft class here: \ No newline at end of file diff --git a/exercises/tsconfig.json b/exercises/tsconfig.json index 51f08b09..a4f9fa4a 100644 --- a/exercises/tsconfig.json +++ b/exercises/tsconfig.json @@ -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 + } + } \ No newline at end of file diff --git a/studio.ts b/studio.ts deleted file mode 100644 index e69de29b..00000000 diff --git a/studio/Astronaut.js b/studio/Astronaut.js new file mode 100644 index 00000000..34ff9e73 --- /dev/null +++ b/studio/Astronaut.js @@ -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; diff --git a/studio/Astronaut.ts b/studio/Astronaut.ts new file mode 100644 index 00000000..f7f6b8fc --- /dev/null +++ b/studio/Astronaut.ts @@ -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; + } + } \ No newline at end of file diff --git a/studio/Cargo.js b/studio/Cargo.js new file mode 100644 index 00000000..4f627519 --- /dev/null +++ b/studio/Cargo.js @@ -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; diff --git a/studio/Cargo.ts b/studio/Cargo.ts new file mode 100644 index 00000000..41aa9968 --- /dev/null +++ b/studio/Cargo.ts @@ -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; + } +} \ No newline at end of file diff --git a/studio/Payload.js b/studio/Payload.js new file mode 100644 index 00000000..64cbe53a --- /dev/null +++ b/studio/Payload.js @@ -0,0 +1,2 @@ +"use strict"; +exports.__esModule = true; diff --git a/studio/Payload.ts b/studio/Payload.ts new file mode 100644 index 00000000..84a2430d --- /dev/null +++ b/studio/Payload.ts @@ -0,0 +1,3 @@ +export interface Payload { + massKg: number; +} \ No newline at end of file diff --git a/studio/Rocket.js b/studio/Rocket.js new file mode 100644 index 00000000..ae8c1c7e --- /dev/null +++ b/studio/Rocket.js @@ -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; diff --git a/studio/Rocket.ts b/studio/Rocket.ts new file mode 100644 index 00000000..1d9ae791 --- /dev/null +++ b/studio/Rocket.ts @@ -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; + } + } +} \ No newline at end of file diff --git a/studio/index.js b/studio/index.js new file mode 100644 index 00000000..c6982c87 --- /dev/null +++ b/studio/index.js @@ -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."); diff --git a/studio/index.ts b/studio/index.ts new file mode 100644 index 00000000..89f60f92 --- /dev/null +++ b/studio/index.ts @@ -0,0 +1,47 @@ +// Instructions are published in the online book. The URL is: +// https://education.launchcode.org/intro-to-professional-web-dev/chapters/typescript/studio.html + +import { Astronaut } from './Astronaut'; +import { Cargo } from './Cargo'; +import { Rocket } from './Rocket'; + +let falcon9: Rocket = new Rocket('Falcon 9', 7500); + +let astronauts: Astronaut[] = [ + new Astronaut(75, 'Mae'), + new Astronaut(81, 'Sally'), + new Astronaut(99, 'Charles') +]; + +for (let i = 0; i < astronauts.length; i++) { + let astronaut = astronauts[i]; + let status = ''; + if (falcon9.addAstronaut(astronaut)) { + status = "On board"; + } else { + status = "Not on board"; + } + console.log(`${astronaut.name}: ${status}`); +} + +let cargo: Cargo[] = [ + new Cargo(3107.39, "Satellite"), + new Cargo(1000.39, "Space Probe"), + new Cargo(753, "Water"), + new Cargo(541, "Food"), + new Cargo(2107.39, "Tesla Roadster"), +]; + +for (let i = 0; i < cargo.length; i++) { + let c = cargo[i]; + let 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.`); +