Skip to content
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
59 changes: 56 additions & 3 deletions challenges/classes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,60 @@
// 1. Copy and paste your prototype in here and refactor into class syntax.
class Cuboid {
constructor(length, width, height) {
this._length = length;
this._width = width;
this._height = height;
}

set length(i){
if(typeof(i)==="number")this._length=i;
else throw 'Cube set to non number';
}
set width(i){
if(typeof(i)==="number")this._width=i;
else throw 'Cube set to non number';
}
set height(i){
if(typeof(i)==="number")this._height=i;
else throw 'Cube set to non number';
}
volume() {return (this._length * this._width * this._height);}
surfaceArea(){return 2*(this._length*this._width + this._length*this._height + this._width*this._height);}
}

var cuboid = new Cuboid(4,5,5);
// Test your volume and surfaceArea methods by uncommenting the logs below:
// console.log(cuboid.volume()); // 100
// console.log(cuboid.surfaceArea()); // 130
console.log(cuboid.volume()); // 100
console.log(cuboid.surfaceArea()); // 130

// Stretch Task: Extend the base class CuboidMaker with a sub class called CubeMaker. Find out the formulas for volume and surface area for cubes and create those methods using the dimension properties from CuboidMaker. Test your work by logging out your volume and surface area.
// Test your volume and surfaceArea methods by uncommenting the logs below:

// Stretch Task: Extend the base class CuboidMaker with a sub class called CubeMaker. Find out the formulas for volume and surface area for cubes and create those methods using the dimension properties from CuboidMaker. Test your work by logging out your volume and surface area.

class Cube extends Cuboid{
constructor (a){
super(a,a,a);
}
volume() {
return this._length**3;
}
surfaceArea() {
return 6* this._length**2;
}

set length(i){
if(typeof(i)==="number")this._width=this._height=this._length=i;
else throw 'Cube set to non number';
}
set width(i){
if(typeof(i)==="number")this._width=this._height=this._length=i;
else throw 'Cube set to non number';
}
set height(i){
if(typeof(i)==="number")this._width=this._height=this._length=i;
else throw 'Cube set to non number';
}
}
cube = new Cube(5);
console.log(cube.volume()); // 125
console.log(cube.surfaceArea()); // 150
20 changes: 16 additions & 4 deletions challenges/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,32 @@


/* Step 3: Check your work by un-commenting the following calls to consume(): */
// consume(2,2,add); // 4
// consume(10,16,multiply); // 160
// consume("Mary","Poppins", greeting); // Hello Mary Poppins, nice to meet you!
console.log(consume(2,2,add)); // 4
console.log(consume(10,16,multiply)); // 160
console.log(consume("Mary","Poppins", greeting)); // Hello Mary Poppins, nice to meet you!


// ==== Closures ====

// Explain in your own words why `nestedfunction()` can access the variable `internal`.

// Explanation:

//The inner function has access to the variables in the outer function scope even if the outer function has long expired, so long as the inner function still has the possibility of being called. (hold off the garbage trucks)

const external = "I'm outside the function";
function consume (x,y,cb){
return cb(x,y);
}
function add(x,y){
return x+y;

}
function multiply(x,y){
return x*y;
}
function greeting(firstName,lastName){
return "Hello "+firstName+ " " + lastName +", nice to meet you!";
}
function myFunction() {
console.log(external);
const internal = "Hello! I'm inside myFunction!";
Expand Down
50 changes: 36 additions & 14 deletions challenges/objects-arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,31 @@

// Using your dinosaur objects, log answers to these questions:

// How much did tyrannosaurus weigh?
console.log();

// What was the diet of a velociraptor?
console.log();
function dinoFactory(type,nature,weight,length,period){
let dino = {type:type,nature:nature,weight:weight,length:length,period:period};
return dino;
}

// How long was a stegosaurus?
console.log();
let jurassicPark = [];
jurassicPark.push(dinoFactory("tyrannosaurus","carnivorous","7000kg","12m","Late Cretaceious"));
jurassicPark.push(dinoFactory("stegosaurus","herbivorous","2000kg","9m","Late Jurassic"));
jurassicPark.push(dinoFactory("velociraptor","carnivorous","15kg","1.8m","Late Cretaceious"));

// What time period did tyrannosaurus live in?
console.log();
//could also of assigned them names like let tyrannosaurus = dinoFactory("tyrannosaurus","carnivorous","7000kg","12m","Late Cretaceious");

// How much did tyrannosaurus weigh?
console.log(jurassicPark[0].weight);

// What was the diet of a velociraptor?
console.log(jurassicPark[2].nature);
// How long was a stegosaurus?
console.log(jurassicPark[2].length);
// What time period did tyrannosaurus live in?
console.log(jurassicPark[0].period);
// Create a new roar method for the tyrannosaurus. When called, return "RAWERSRARARWERSARARARRRR!" Log the result.
console.log();
jurassicPark[0].roar = function() {return "RAWERSRARARWERSARARARRRR";};
console.log(jurassicPark[0].roar());


// ==== Arrays ====
Expand All @@ -47,9 +57,12 @@ const graduates = [{"id":1,"first_name":"Cynde","university":"Missouri Southern
{"id":10,"first_name":"Hube","university":"Universitat Rovira I Virgili Tarragona","email":"hlethbrig9@foxnews.com"}]

/* Request 1: Create a new array called universities that contains all the univeristies in the graduates array.

Once you have the new array created, sort the universities alphabetically and log the result. */
const universities = [];
for (let i = 0; i < graduates.length;++i) {
universities.push(graduates[i]["university"]);
} // this works nicely until two students graduate from the same school

console.log(universities)

/* Request 2: Create a new array called contactInfo that contains both first name and email of each student.
Expand All @@ -59,14 +72,20 @@ Name email@example.com

Log the result of your new array. */
const contactInfo = [];
for (let i = 0; i < graduates.length;++i) {
contactInfo.push(graduates[i]["first_name"] + " "+graduates[i]["email"]);
}
console.log(contactInfo);


/* Request 3: Find out how many universities have the string "Uni" included in their name. Create a new array called uni that contains them all. Log the result. */
const uni = [];
for (let i = 0; i < universities.length;++i) {
if(universities[i].toLowerCase().search("uni") > 0)
uni.push(universities[i])
}
console.log(uni);


// ==== ADVANCED Array Methods ====

// Given this zoo data from around the United States, follow the instructions below. Use the specific array methods in the requests below to solve the problems.
Expand All @@ -89,7 +108,10 @@ The zoo wants to display both the scientific name and the animal name in front o

*/
const animalNames = [];
console.log(animalNames);
zooAnimals.forEach((cv) => {
animalNames.push([{name:cv['animal_name'],scientific:cv['scientific_name']}]);
});
animalNames.forEach(cv => {console.log(cv);});

/* Request 2: .map()

Expand All @@ -105,15 +127,15 @@ console.log(lowerCase);
The zoos are concenred about animals with a lower population count. Find out which animals have a population less than 5.

*/
const largerPopulation = [];
const largerPopulation = zooAnimals.filter( (cv) =>(cv.population < 5)) //if these are strings the comparison could fail..(but they're not)
console.log(largerPopulation);

/* Request 4: .reduce()

The zoos need to know their total animal population across the United States. Find the total population from all the zoos using the .reduce() method.

*/
const populationTotal = 0;
const populationTotal = zooAnimals.reduce((total, cv)=> (total + cv.population),0);
console.log(populationTotal);


Expand Down
21 changes: 13 additions & 8 deletions challenges/prototypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,34 @@
/* == Step 1: Base Constructor ==
Create a constructor function named CuboidMaker that accepts properties for length, width, and height
*/
function CuboidMaker(length, width, height) {
this.length = length;
this.width = width;
this.height = height;

}

/* == Step 2: Volume Method ==
Create a method using CuboidMaker's prototype that returns the volume of a given cuboid's length, width, and height

Formula for cuboid volume: length * width * height
*/

CuboidMaker.prototype.volume = () => (this.length * this.width * this.height);

/* == Step 3: Surface Area Method ==
Create another method using CuboidMaker's prototype that returns the surface area of a given cuboid's length, width, and height.

Formula for cuboid surface area of a cube: 2 * (length * width + length * height + width * height)
*/
Formula for cuboid surface area of a cube: 2 * (length * width + length * height + width * height)
*/
CuboidMaker.prototype.volume = function(){return this.length * this.width * this.height;};

CuboidMaker.prototype.surfaceArea = function(){return 2*(this.length*this.width + this.length*this.height + this.width*this.height)};

/* == Step 4: Create a new object that uses CuboidMaker ==
Create a cuboid object that uses the new keyword to use our CuboidMaker constructor
Add properties and values of length: 4, width: 5, and height: 5 to cuboid.
*/

var cuboid = new CuboidMaker(4,5,5);
// Test your volume and surfaceArea methods by uncommenting the logs below:
// console.log(cuboid.volume()); // 100
// console.log(cuboid.surfaceArea()); // 130


console.log(cuboid.volume()); // 100
console.log(cuboid.surfaceArea()); // 130