Skip to content

Commit 00da1d2

Browse files
committed
5-20-2020 tutorial
1 parent e256a46 commit 00da1d2

3 files changed

Lines changed: 355 additions & 19 deletions

File tree

challenges/functions.js

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,73 @@
22
// Callbacks
33

44
// Step 1: Create a higher-order function that will accept 3 parameters. The first two can take any argument. The last is your callback. This function should return your callback that passed the first two parameters as its argument.
5+
function highOrder(param1, param2, cb) {
6+
return cb(param1, param2);
7+
}
8+
// Step 2: Create several functions that you will callback with the previous higher order function.
9+
// The first will return the sum of two numbers.
10+
function add(num1, num2) {
11+
return num1 + num2;
12+
}
13+
console.log(highOrder(2, 13, add), "Add Function");
14+
console.log(highOrder(43154, 45345624, add));
15+
console.log(highOrder(3, 26, add));
16+
// The second will return the product of two numbers.
17+
function multiply(num1, num2) {
18+
return num1 * num2;
19+
}
20+
console.log(highOrder(2, 13, multiply), "Multiply function",highOrder(2, 14, add), "Add Function", highOrder("Jay", "Leach", stringReturn));
21+
console.table(highOrder(2, 13, multiply), "Multiply function",highOrder(2, 14, add))
22+
// The third will return the modulus of the two numbers.
23+
// The fourth will return the quotient of the two numbers.
24+
// The fifth will return the square root of the two numbers.
25+
// The sixth will accept a first and last name and return 'Hey, firstName lastName, youre a wicked cool dev'.
26+
function stringReturn(firstString, secondString) {
27+
return `Hey, ${firstString} ${secondString}, youre a wicked cool dev`
28+
}
29+
console.log(highOrder("Jay", "Leach", stringReturn));
30+
// The seventh will return whether the firstString is included the secondString.
31+
// The eigth will return whether the firstNumber is greater than the secondNumber.
32+
// The ninth will return whether the firstNumber is less than the secondNumber.
33+
// The tenth will return whether the firstNumber is greater than or equal to the secondNumber.
34+
// The eleventh will return whether the firstNumber is less than or equal to the secondNumber.
35+
// The twelvth will return whether the firstNumber is equivalent to the secondNumber.
36+
// The thirteenth will return a string 'Hey the number is firstNumber' if firstNumber is greater than the secondNumber. If it isn't, return a string 'Hey the number is secondNumber'
37+
538

6-
/* Step 2: Create several functions that you will callback with the previous higher order function.
7-
The first will return the sum of two numbers.
8-
The second will return the product of two numbers.
9-
The third will return the modulus of the two numbers.
10-
The fourth will return the quotient of the two numbers.
11-
The fifth will return the square root of the two numbers.
12-
The sixth will accept a first and last name and return 'Hey, firstName lastName, youre a wicked cool dev'.
13-
The seventh will return whether the firstString is included the secondString.
14-
The eigth will return whether the firstNumber is greater than the secondNumber.
15-
The ninth will return whether the firstNumber is less than the secondNumber.
16-
The tenth will return whether the firstNumber is greater than or equal to the secondNumber.
17-
The eleventh will return whether the firstNumber is less than or equal to the secondNumber.
18-
The twelvth will return whether the firstNumber is equivalent to the secondNumber.
19-
The thirteenth will return a string 'Hey the number is firstNumber' if firstNumber is greater than the secondNumber. If it isn't, return a string 'Hey the number is secondNumber'
20-
*/
2139

2240
// Step 3: Check your work using console.log and invoke your higher order function.
41+
42+
const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum', 'Book', 'Pen', 'Headphones', 'Candy'];
43+
44+
function getLength(arr, cb) {
45+
// getLength passes the length of the array into the callback
46+
cb(arr.length);
47+
}
48+
49+
getLength(items, (lengthoflist) => {
50+
console.log(lengthoflist);
51+
});
52+
53+
function last(arr, cb) {
54+
// last passes the last item of the array into the callback
55+
cb(arr[arr.length - 1]);
56+
}
57+
58+
last(items, (lastoflist) => {
59+
console.log(lastoflist)
60+
})
61+
62+
function contains(item, arr, cb) {
63+
// contains checks if an item is present inside of the given array/list.
64+
// Pass true to the callback if so, otherwise false.
65+
if(arr.includes(item)) {
66+
return cb(true);
67+
} else {
68+
return cb(false);
69+
}
70+
}
71+
72+
contains('bob', items, function(value) {
73+
console.log(value);
74+
})

challenges/objects-arrays.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,58 @@ const gameCharacters = [
6161
// For Loop
6262

6363
// Map or forEach or filter
64+
65+
66+
67+
// A local community center is holding a fund raising 5k fun run and has invited 50 small businesses to make a small donation on their behalf for some much needed updates to their facilities. Each business has assigned a representative to attend the event along with a small donation.
68+
// Scroll to the bottom of the list to use some advanced array methods to help the event director gather some information from the businesses.
69+
const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"cseiler0@wired.com","shirt_size":"2XL","company_name":"Divanoodle","donation":75},
70+
{"id":2,"first_name":"Whitaker","last_name":"Ierland","email":"wierland1@angelfire.com","shirt_size":"2XL","company_name":"Wordtune","donation":148},
71+
{"id":3,"first_name":"Julieta","last_name":"McCloid","email":"jmccloid2@yahoo.com","shirt_size":"S","company_name":"Riffpedia","donation":171},
72+
{"id":4,"first_name":"Martynne","last_name":"Paye","email":"mpaye3@sciencedaily.com","shirt_size":"XL","company_name":"Wordware","donation":288},
73+
{"id":5,"first_name":"Gussy","last_name":"Raraty","email":"graraty4@ucoz.ru","shirt_size":"L","company_name":"Oozz","donation":291},
74+
{"id":6,"first_name":"Yule","last_name":"Tommasetti","email":"ytommasetti5@state.gov","shirt_size":"S","company_name":"Yodo","donation":27},
75+
{"id":7,"first_name":"Kathie","last_name":"Majury","email":"kmajury6@guardian.co.uk","shirt_size":"3XL","company_name":"Zoomcast","donation":261},
76+
{"id":8,"first_name":"Tanner","last_name":"Branton","email":"tbranton7@tmall.com","shirt_size":"2XL","company_name":"Realmix","donation":28},
77+
{"id":9,"first_name":"Sarina","last_name":"Lasham","email":"slasham8@toplist.cz","shirt_size":"XL","company_name":"Gigashots","donation":110},
78+
{"id":10,"first_name":"Bertie","last_name":"Lonergan","email":"blonergan9@issuu.com","shirt_size":"3XL","company_name":"Skinte","donation":62},
79+
{"id":11,"first_name":"Trevor","last_name":"Studd","email":"tstudda@networkadvertising.org","shirt_size":"S","company_name":"Cogidoo","donation":76},
80+
{"id":12,"first_name":"Malachi","last_name":"Okeshott","email":"mokeshottb@chron.com","shirt_size":"M","company_name":"DabZ","donation":91},
81+
{"id":13,"first_name":"Berget","last_name":"Logsdail","email":"blogsdailc@wix.com","shirt_size":"M","company_name":"Mymm","donation":9},
82+
{"id":14,"first_name":"Loise","last_name":"Rivlin","email":"lrivlind@behance.net","shirt_size":"2XL","company_name":"Linktype","donation":204},
83+
{"id":15,"first_name":"Christan","last_name":"Kendall","email":"ckendalle@example.com","shirt_size":"XS","company_name":"Skinix","donation":252},
84+
{"id":16,"first_name":"Kayla","last_name":"Whitwam","email":"kwhitwamf@deliciousdays.com","shirt_size":"2XL","company_name":"Oyope","donation":147},
85+
{"id":17,"first_name":"Heddie","last_name":"Heningam","email":"hheningamg@tripadvisor.com","shirt_size":"L","company_name":"Skinix","donation":172},
86+
{"id":18,"first_name":"Mace","last_name":"Ballinger","email":"mballingerh@a8.net","shirt_size":"S","company_name":"Nlounge","donation":266},
87+
{"id":19,"first_name":"Nola","last_name":"Abberley","email":"nabberleyi@jalbum.net","shirt_size":"XL","company_name":"Photospace","donation":148},
88+
{"id":20,"first_name":"Nadine","last_name":"Tresler","email":"ntreslerj@marketwatch.com","shirt_size":"3XL","company_name":"Wikido","donation":292},
89+
{"id":21,"first_name":"Ulrikaumeko","last_name":"Vuittet","email":"uvuittetk@gov.uk","shirt_size":"S","company_name":"Skinte","donation":102},
90+
{"id":22,"first_name":"Saunder","last_name":"Spennock","email":"sspennockl@icq.com","shirt_size":"3XL","company_name":"Kwimbee","donation":213},
91+
{"id":23,"first_name":"Carmel","last_name":"Woffinden","email":"cwoffindenm@wikispaces.com","shirt_size":"S","company_name":"Rooxo","donation":137},
92+
{"id":24,"first_name":"Marielle","last_name":"Kimmel","email":"mkimmeln@jimdo.com","shirt_size":"M","company_name":"Livetube","donation":96},
93+
{"id":25,"first_name":"Brucie","last_name":"Burris","email":"bburriso@slate.com","shirt_size":"2XL","company_name":"Wordtune","donation":128},
94+
{"id":26,"first_name":"Juan","last_name":"Berzon","email":"jberzonp@soup.io","shirt_size":"3XL","company_name":"Einti","donation":234},
95+
{"id":27,"first_name":"Sacha","last_name":"Olsen","email":"solsenq@reverbnation.com","shirt_size":"2XL","company_name":"Viva","donation":190},
96+
{"id":28,"first_name":"Jamey","last_name":"O'Nolan","email":"jonolanr@samsung.com","shirt_size":"XL","company_name":"Skinix","donation":31},
97+
{"id":29,"first_name":"Morrie","last_name":"Rainard","email":"mrainards@yale.edu","shirt_size":"XS","company_name":"Podcat","donation":52},
98+
{"id":30,"first_name":"Fidel","last_name":"Roskelly","email":"froskellyt@ibm.com","shirt_size":"XS","company_name":"Avavee","donation":5},
99+
{"id":31,"first_name":"Toni","last_name":"MacSweeney","email":"tmacsweeneyu@parallels.com","shirt_size":"M","company_name":"Jaloo","donation":82},
100+
{"id":32,"first_name":"Jessey","last_name":"Walhedd","email":"jwalheddv@slashdot.org","shirt_size":"L","company_name":"Trilia","donation":5},
101+
{"id":33,"first_name":"Karola","last_name":"Piper","email":"kpiperw@ucsd.edu","shirt_size":"3XL","company_name":"Yombu","donation":110},
102+
{"id":34,"first_name":"Marley","last_name":"Mitchenson","email":"mmitchensonx@webeden.co.uk","shirt_size":"M","company_name":"Zoonoodle","donation":97},
103+
{"id":35,"first_name":"Marrilee","last_name":"Thrasher","email":"mthrashery@opensource.org","shirt_size":"XL","company_name":"Bluejam","donation":17},
104+
{"id":36,"first_name":"Tye","last_name":"Manie","email":"tmaniez@netscape.com","shirt_size":"L","company_name":"Kanoodle","donation":30},
105+
{"id":37,"first_name":"Charleen","last_name":"Sheering","email":"csheering10@mit.edu","shirt_size":"3XL","company_name":"Jatri","donation":262},
106+
{"id":38,"first_name":"Valma","last_name":"Eynaud","email":"veynaud11@archive.org","shirt_size":"XS","company_name":"Jaxbean","donation":212},
107+
{"id":39,"first_name":"Dollie","last_name":"McDarmid","email":"dmcdarmid12@tinyurl.com","shirt_size":"S","company_name":"Kayveo","donation":74},
108+
{"id":40,"first_name":"Minna","last_name":"Hymas","email":"mhymas13@cornell.edu","shirt_size":"XS","company_name":"Vimbo","donation":101},
109+
{"id":41,"first_name":"Jsandye","last_name":"Frend","email":"jfrend14@ca.gov","shirt_size":"XS","company_name":"Latz","donation":156},
110+
{"id":42,"first_name":"Yevette","last_name":"Hacket","email":"yhacket15@wp.com","shirt_size":"XL","company_name":"Lazzy","donation":291},
111+
{"id":43,"first_name":"Hank","last_name":"Zebedee","email":"hzebedee16@ezinearticles.com","shirt_size":"L","company_name":"Gigashots","donation":241},
112+
{"id":44,"first_name":"Jodie","last_name":"Stawell","email":"jstawell17@yale.edu","shirt_size":"S","company_name":"Jaxspan","donation":262},
113+
{"id":45,"first_name":"Falito","last_name":"Karsh","email":"fkarsh18@pcworld.com","shirt_size":"S","company_name":"Mycat","donation":239},
114+
{"id":46,"first_name":"Reginauld","last_name":"Purselowe","email":"rpurselowe19@thetimes.co.uk","shirt_size":"L","company_name":"Jabbersphere","donation":11},
115+
{"id":47,"first_name":"Vida","last_name":"Tydd","email":"vtydd1a@dropbox.com","shirt_size":"S","company_name":"Quaxo","donation":55},
116+
{"id":48,"first_name":"Anderea","last_name":"MacGiolla Pheadair","email":"amacgiollapheadair1b@xing.com","shirt_size":"2XL","company_name":"Kwimbee","donation":214},
117+
{"id":49,"first_name":"Bel","last_name":"Alway","email":"balway1c@ow.ly","shirt_size":"S","company_name":"Voolia","donation":107},
118+
{"id":50,"first_name":"Shell","last_name":"Baine","email":"sbaine1d@intel.com","shirt_size":"M","company_name":"Gabtype","donation":171}];

challenges/prototypes.js

Lines changed: 233 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,241 @@
22

33
// Task: You are to build a pentagonal pyramid that can return values for its volume as well as its surface areas. Follow the steps to achieve this.
44

5-
// Step 1: Base Constructor -- You should create a constructor function names PentagonalPyramid that will accept properties for its base edge, and heights.
5+
// Step 1: Base Constructor -- You should create a constructor function names PentagonalPyramid that will accept properties for its base, edge, and heights.
6+
function PentagonalPyramid(attr) {
7+
this.edge = attr.edge;
8+
this.height = attr.height;
9+
this.length = attr.length;
10+
}
611

712
// Step 2: Volume Method -- You should create a prototype for PentagonalPyramid that returns the volume of a pentagonal pyramid. Curious about the formula??? Use Google (A DEVELOPERS BEST FRIEND)
8-
13+
PentagonalPyramid.prototype.volume = function() {
14+
let formulaVolume = (5/6) * this.edge * this.height * this.length;
15+
return formulaVolume;
16+
}
917
// Step 3: Surface Area Method -- You should create a prototype fro PentagonalPyramid that returns the surface area of a pentagonal pyramid. Curious about the formula??? Use Google (A DEVELOPERS BEST FRIEND)
10-
18+
PentagonalPyramid.prototype.surfaceArea = function() {
19+
let formulaSurfaceArea = (((5/2) * this.length * this.edge) + ((5/2) * this.edge * this.height));
20+
return formulaSurfaceArea
21+
}
22+
console.log(PentagonalPyramid, "Constructor");
1123
// Step 4: New Object -- You should create a new object that uses PentagonalPyramid. Give it the length, width, and height.
12-
24+
const pyramid = new PentagonalPyramid( {
25+
'edge': 5,
26+
'height': 2,
27+
'length': 4
28+
});
1329
// Test your volume and surfaceArea methods here using console.log
30+
console.log(pyramid, 'pyramid');
31+
console.log(pyramid.volume());
32+
console.log(pyramid.surfaceArea());
33+
34+
/*
35+
Object oriented design is commonly used in video games. For this part of the assignment you will be implementing several constructor functions with their correct inheritance hierarchy.
36+
In this file you will be creating three constructor functions: GameObject, CharacterStats, Humanoid.
37+
At the bottom of this file are 3 objects that all end up inheriting from Humanoid. Use the objects at the bottom of the page to test your constructor functions.
38+
Each constructor function has unique properties and methods that are defined in their block comments below:
39+
*/
40+
41+
/*
42+
=== GameObject ===
43+
* createdAt
44+
* name
45+
* dimensions (These represent the character's size in the video game)
46+
* destroy() // prototype method that returns: `${this.name} was removed from the game.`
47+
*/
48+
function GameObject(buggy) {
49+
this.createdAt = buggy.createdAt;
50+
this.name = buggy.name;
51+
this.dimensions = buggy.dimensions;
52+
}
53+
54+
GameObject.prototype.destroy = function() {
55+
return `${this.name} was removed from the game.`
56+
}
57+
/*
58+
=== CharacterStats ===
59+
* healthPoints
60+
* takeDamage() // prototype method -> returns the string '<object name> took damage.'
61+
* should inherit destroy() from GameObject's prototype
62+
*/
63+
function CharacterStats(bob) {
64+
GameObject.call(this, bob);
65+
this.healthPoints = bob.healthPoints;
66+
}
67+
68+
//This is the Inheritance
69+
CharacterStats.prototype = Object.create(GameObject.prototype)
70+
// Prototype Method is Created here
71+
CharacterStats.prototype.takeDamage = function() {
72+
return `${this.name} took damage.`
73+
}
74+
75+
/*
76+
=== Humanoid (Having an appearance or character resembling that of a human.) ===
77+
* team
78+
* weapons
79+
* language
80+
* greet() // prototype method -> returns the string '<object name> offers a greeting in <object language>.'
81+
* should inherit destroy() from GameObject through CharacterStats
82+
* should inherit takeDamage() from CharacterStats
83+
*/
84+
function Humanoid(q) {
85+
CharacterStats.call(this, q);
86+
this.team = q.team;
87+
this.weapons = q.weapons;
88+
this.language = q.language;
89+
}
90+
//This is the inheritance
91+
Humanoid.prototype = Object.create(CharacterStats.prototype);
92+
// Prototype Method is created here
93+
Humanoid.prototype.greet = function() {
94+
return `${this.name} offers a greeting in ${this.language}`
95+
}
96+
97+
/*
98+
* Inheritance chain: GameObject -> CharacterStats -> Humanoid
99+
* Instances of Humanoid should have all of the same properties as CharacterStats and GameObject.
100+
* Instances of CharacterStats should have all of the same properties as GameObject.
101+
*/
102+
103+
// Test you work by un-commenting these 3 objects and the list of console logs below:
104+
105+
const mage = new Humanoid({
106+
createdAt: new Date(),
107+
dimensions: {
108+
length: 2,
109+
width: 1,
110+
height: 1,
111+
},
112+
healthPoints: 5,
113+
name: 25,
114+
team: 'Mage Guild',
115+
weapons: [
116+
'Staff of Shamalama',
117+
],
118+
language: 'Common Tongue',
119+
});
120+
121+
const swordsman = new Humanoid({
122+
createdAt: new Date(),
123+
dimensions: {
124+
length: 2,
125+
width: 2,
126+
height: 2,
127+
},
128+
healthPoints: 15,
129+
name: 'Sir Mustachio',
130+
team: 'The Round Table',
131+
weapons: [
132+
'Giant Sword',
133+
'Shield',
134+
],
135+
language: 'Common Tongue',
136+
});
137+
138+
const archer = new Humanoid({
139+
createdAt: new Date(),
140+
dimensions: 1,
141+
healthPoints: 10,
142+
name: 'Lilith',
143+
team: 'Forest Kingdom',
144+
weapons: [
145+
'Bow',
146+
'Dagger',
147+
],
148+
language: 'Elvish',
149+
});
150+
const arr = [archer, swordsman, mage];
151+
console.log(arr);
152+
console.log(mage.createdAt);
153+
console.log(archer.dimensions);
154+
console.log(swordsman.healthPoints);
155+
console.log(mage.name);
156+
console.log(archer.language);
157+
console.log(archer.greet());
158+
console.log(swordsman.greet());
159+
console.log(mage.takeDamage());
160+
console.log(swordsman.destroy());
161+
162+
console.log(swordsman.dimensions, "EXAMPLE");
163+
console.log(archer.dimensions, "EXAMPLE");
164+
165+
166+
167+
// Villain Contructor Function
168+
function Villain(attr) {
169+
Humanoid.call(this, attr);
170+
this.lightningBolt = attr.lightningBolt;
171+
this.opponentHealth = attr.opponentHealth;
172+
}
173+
174+
// Inheritance
175+
Villain.prototype = Object.create(Humanoid.prototype);
176+
177+
// Protype Method is Created here
178+
Villain.prototype.attack = function() {
179+
this.opponentHealth -= 10;
180+
return `A flash of light! The great ${this.name} will finish you and use your bones for stew!`;
181+
}
182+
183+
// Hero Contructor Function
184+
function Hero(attr) {
185+
Humanoid.call(this, attr);
186+
this.holyLight = attr.holyLight;
187+
this.opponentHealth = attr.opponentHealth;
188+
}
189+
// Inheritance
190+
Hero.prototype = Object.create(Humanoid.prototype);
191+
// Protype Method is Created here
192+
Hero.prototype.returnFire = function() {
193+
this.opponentHealth -= 15;
194+
return `${this.name} fires back, unshaken.`
195+
}
196+
197+
198+
const Maestro = new Villain({
199+
createdAt: new Date(),
200+
dimensions: {
201+
length: 1,
202+
width: 2,
203+
height: 5,
204+
},
205+
healthPoints: 15,
206+
name: 'Maestro',
207+
team: 'Forest Kingdom',
208+
weapons: [
209+
'Staff',
210+
'Dagger',
211+
'Spellbook',
212+
],
213+
language: 'Demonic',
214+
lightningBolt: 'Lightning Strike',
215+
opponentHealth: 20
216+
})
217+
218+
const HeroMan = new Hero({
219+
createdAt: new Date(),
220+
dimensions: {
221+
length: 1,
222+
width: 2,
223+
height: 4,
224+
},
225+
healthPoints: 20,
226+
name: 'HeroMan',
227+
team: 'Forest Kingdom',
228+
weapons: [
229+
'Bow',
230+
'Sword&Shield',
231+
],
232+
language: 'Human',
233+
holyLight: 'Holy Light, strike with thine might',
234+
opponentHealth: 15,
235+
});
236+
237+
238+
console.log(`${HeroMan.name} walked into ${Maestro.name}s trap!`);
239+
console.log(`${Maestro.attack()}`);
240+
console.log(`${HeroMan.name} recoils, and now has ${Maestro.opponentHealth} Health`);
241+
console.log(`${HeroMan.returnFire()}`);
242+
console.log(`${Maestro.name} falters, crumbling to the ground, leaving ${HeroMan.opponentHealth} Health left`);

0 commit comments

Comments
 (0)