Skip to content

Main #17

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 2 commits into
base: master
Choose a base branch
from
Open

Main #17

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
12 changes: 12 additions & 0 deletions exercises/SpaceLocation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"use strict";
exports.__esModule = true;
exports.SpaceLocation = void 0;
// Paste in the provided code here:
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
@@ -1 +1,10 @@
// Paste in the provided code here:
export class SpaceLocation {
kilometersAway: number;
name: string;

constructor(name: string, kilometersAway: number) {
this.name = name;
this.kilometersAway = kilometersAway;
}
}
61 changes: 61 additions & 0 deletions exercises/parts1-5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"use strict";
// URL for the instructions:
// https://education.launchcode.org/intro-to-professional-web-dev/chapters/typescript/exercises.html
exports.__esModule = true;
// Part 1: Declare (5) Variables With Type
// let spacecraftName: string = "Determination";
// let speedMph: number = 17500;
var kilometersToMars = 225000000;
var kilometersToTheMoon = 384400;
// let milesPerKilometer: number = 0.621;
var kilometersToJupiter = 588000000;
// Part 2: Print Days to Mars
// let milesToMars: number = kilometersToMars * milesPerKilometer;
// let hoursToMars: number = milesToMars / speedMph;
// let daysToMars: number = hoursToMars / 24;
// Code an output statement here (use a template literal):
// console.log(`It will take ${spacecraftName} ${daysToMars} days to get to Mars.`);
// Part 3: Create a Function ("getDaysToLocation")
// function getDaysToLocation(kilometersAway:number):number {
// let milesToLocation: number = kilometersAway * milesPerKilometer
// let hoursToLocation: number = milesToLocation / speedMph;
// let daysToLocation: number = hoursToLocation / 24;
// return daysToLocation;
// };
// Move your output statement from part 2 here. Update the template literal to call
// the function and print the outputs for a Mars trip and a moon trip.
//console.log(`It will take ${spacecraftName} ${getDaysToLocation(kilometersToMars)} days to get to Mars.`);
//console.log(`It will take ${spacecraftName} ${getDaysToLocation(kilometersToJupiter)} days to get to Jupiter.`);
// Part 4: Create a 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 milesToLocation = kilometersAway * this.milesPerKilometer;
var hoursToLocation = milesToLocation / this.speedMph;
var daysToLocation = hoursToLocation / 24;
return daysToLocation;
};
Spacecraft.prototype.printDaysToLocation = function (location) {
console.log("".concat(this.name, " would take ").concat(this.getDaysToLocation(location.kilometersAway), " days to get to ").concat(location.name, "."));
};
;
return Spacecraft;
}());
// Create an instance of the class here:
var spaceShuttle = new Spacecraft('Determination', 17500);
// Move your output statements from part 3 here. Update the template literals use the
// instance of the class.
// console.log(`It will take ${spaceShuttle.name} ${spaceShuttle.getDaysToLocation(kilometersToMars)} days to get to Mars.`);
// console.log(`It will take ${spaceShuttle.name} ${spaceShuttle.getDaysToLocation(kilometersToTheMoon)} days to get to the moon.`);
// Part 5: Export and Import the SpaceLocation Class
// Add the required import statement BEFORE the part 1 concent.
var SpaceLocation_1 = require("./SpaceLocation");
// Add the printDaysToLocation function to the Spacecraft class.
// 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));
54 changes: 52 additions & 2 deletions exercises/parts1-5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,95 @@


// Part 1: Declare (5) Variables With Type


// let spacecraftName: string = "Determination";
// let speedMph: number = 17500;
let kilometersToMars: number = 225000000;
let kilometersToTheMoon: number = 384400;
// let milesPerKilometer: number = 0.621;
let kilometersToJupiter: number = 588000000;

// Part 2: Print Days to Mars

// let milesToMars: number = kilometersToMars * milesPerKilometer;
// let hoursToMars: number = milesToMars / speedMph;
// let daysToMars: number = hoursToMars / 24;


// Code an output statement here (use a template literal):

// console.log(`It will take ${spacecraftName} ${daysToMars} days to get to Mars.`);


// Part 3: Create a Function ("getDaysToLocation")

// function getDaysToLocation(kilometersAway:number):number {

// let milesToLocation: number = kilometersAway * milesPerKilometer
// let hoursToLocation: number = milesToLocation / speedMph;
// let daysToLocation: number = hoursToLocation / 24;

// return daysToLocation;
// };


// Move your output statement from part 2 here. Update the template literal to call
// the function and print the outputs for a Mars trip and a moon trip.

//console.log(`It will take ${spacecraftName} ${getDaysToLocation(kilometersToMars)} days to get to Mars.`);

//console.log(`It will take ${spacecraftName} ${getDaysToLocation(kilometersToJupiter)} days to get to Jupiter.`);


// Part 4: Create a 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 milesToLocation: number = kilometersAway * this.milesPerKilometer
let hoursToLocation: number = milesToLocation / this.speedMph;
let daysToLocation: number = hoursToLocation / 24;

return daysToLocation;
}

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);


// Move your output statements from part 3 here. Update the template literals use the
// instance of the class.

// console.log(`It will take ${spaceShuttle.name} ${spaceShuttle.getDaysToLocation(kilometersToMars)} days to get to Mars.`);

// console.log(`It will take ${spaceShuttle.name} ${spaceShuttle.getDaysToLocation(kilometersToTheMoon)} days to get to the moon.`);

// Part 5: Export and Import the SpaceLocation Class
// Add the required import statement BEFORE the part 1 concent.

import {SpaceLocation} from './SpaceLocation';


// Add the printDaysToLocation function to the Spacecraft class.


// Paste in the code from step 6 here:

spaceShuttle.printDaysToLocation(new SpaceLocation('Mars', kilometersToMars));
spaceShuttle.printDaysToLocation(new SpaceLocation('the Moon', kilometersToTheMoon));
11 changes: 11 additions & 0 deletions studio/Astronaut.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict";
exports.__esModule = true;
exports.Astronaut = void 0;
var Astronaut = /** @class */ (function () {
function Astronaut(massKg, name) {
this.massKg = massKg;
this.name = name;
}
return Astronaut;
}());
exports.Astronaut = Astronaut;
14 changes: 14 additions & 0 deletions studio/Astronaut.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Payload } from "./Payload";

export class Astronaut implements Payload {

name: string;
massKg: number;

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


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

export class Cargo implements Payload {

massKg: number;
material: string;

constructor (massKg: number, material: string) {
this.massKg = massKg;
this.material = material;
}

}
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;
40 changes: 40 additions & 0 deletions studio/Rocket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"use strict";
exports.__esModule = true;
exports.Rocket = void 0;
var Rocket = /** @class */ (function () {
function Rocket(name, totalCapacityKg) {
this.cargoItems = [];
this.astronauts = [];
this.name = name;
this.totalCapacityKg = totalCapacityKg;
}
Rocket.prototype.sumMass = function (items) {
var totalMass = 0;
for (var i = 0; i < items.length; i++) {
totalMass += items[i].massKg;
}
return totalMass;
};
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)) {
return false;
}
this.cargoItems.push(cargo);
return true;
};
Rocket.prototype.addAstronaut = function (astronaut) {
if (!this.canAdd(astronaut)) {
return false;
}
this.astronauts.push(astronaut);
return true;
};
return Rocket;
}());
exports.Rocket = Rocket;
56 changes: 56 additions & 0 deletions studio/Rocket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Cargo } from './Cargo';
import { Astronaut } from './Astronaut';
import { Payload } from './Payload';

export class Rocket {

name: string;
totalCapacityKg: number;
cargoItems: Cargo[] = [];
astronauts: Astronaut[] = [];

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

sumMass( items: Payload[] ): number {

let totalMass = 0;

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

return totalMass;
}

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)) {
return false;
}

this.cargoItems.push(cargo);
return true;
}

addAstronaut(astronaut: Astronaut): boolean{

if (!this.canAdd(astronaut)) {
return false;
}

this.astronauts.push(astronaut);
return true;

}

}
46 changes: 46 additions & 0 deletions studio/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"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;
// TODO:
// * Code the Astronaut, Cargo, and Rocket classes in new files.
// * Import the three classes into this file.
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("".concat(astronaut.name, ": ").concat(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("".concat(c.material, ": ").concat(loaded));
}
console.log("Final cargo and astronaut mass: ".concat(falcon9.currentMassKg(), " kg."));
Loading