Skip to content

Commit 990b3cc

Browse files
committed
starting on answer key
1 parent 94c9fa3 commit 990b3cc

File tree

3 files changed

+164
-6
lines changed

3 files changed

+164
-6
lines changed

challenges/functions.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
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.
55

6+
function consume(x, y, cb) {
7+
return cb(x, y);
8+
}
9+
610
/* Step 2: Create several functions that you will callback with the previous higher order function.
711
The first will return the sum of two numbers.
812
The second will return the product of two numbers.
@@ -18,5 +22,78 @@ The eleventh will return whether the firstNumber is less than or equal to the se
1822
The twelvth will return whether the firstNumber is equivalent to the secondNumber.
1923
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'
2024
*/
25+
function add(num1, num2) {
26+
return num1 + num2;
27+
}
28+
29+
// The second will return the product of two numbers.
30+
function multiply(num1, num2) {
31+
return num1 * num2;
32+
}
33+
// The third will return the modulus of the two numbers.
34+
function remainder(num1, num2) {
35+
return num1 % num2;
36+
}
37+
// The fourth will return the quotient of the two numbers.
38+
function divide(num1, num2) {
39+
return num1/num2;
40+
}
41+
// The fifth will return the square root of the two numbers.
42+
function square(num1, num2) {
43+
let var1 = Math.sqrt(num1);
44+
let var2 = Math.sqrt(num2);
45+
// return var1 + " " + var2;
46+
return `${var1} ${var2}`;
47+
}
48+
// The sixth will accept a first and last name and return 'Hey, firstName lastName, youre a wicked cool dev'.
49+
function stringReturn(firstName, lastName) {
50+
return `Hey, ${firstName} ${lastName}, youre a wicked cool dev`;
51+
}
52+
// The seventh will return whether the firstString is included the secondString.
53+
function stringIncludes(firstString, secondString) {
54+
return secondString.includes(firstString);
55+
}
56+
// The eigth will return whether the firstNumber is greater than the secondNumber.
57+
function numCompare1(x, y) {
58+
return x > y;
59+
}
60+
// The ninth will return whether the firstNumber is less than the secondNumber.
61+
function numCompare2(x,y) {
62+
return x < y;
63+
}
64+
// The tenth will return whether the firstNumber is greater than or equal to the secondNumber.
65+
function numCompare3(x,y) {
66+
return x >= y;
67+
}
68+
// The eleventh will return whether the firstNumber is less than or equal to the secondNumber.
69+
function numCompare4(x,y) {
70+
return x <= y;
71+
}
72+
// The twelvth will return whether the firstNumber is equivalent to the secondNumber.
73+
function numCompare5(x,y) {
74+
return x === y;
75+
}
76+
// 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'
77+
function numCompare6(x, y) {
78+
if(x > y) {
79+
return `Hey the number is ${x}`;
80+
} else {
81+
return `Hey the number is ${y}`;
82+
}
83+
}
84+
2185

2286
// Step 3: Check your work using console.log and invoke your higher order function.
87+
console.log(consume(1, 2, add), "This is to add");
88+
console.log(consume(2, 2, multiply));
89+
console.log(consume(2, 3, remainder));
90+
console.log(consume(2, 4, divide));
91+
console.log(consume(4, 16, square));
92+
console.log(consume('bob', 'bobbington', stringReturn));
93+
console.log(consume('bob', 'bob went to the store', stringIncludes));
94+
console.log(consume(2,5, numCompare1));
95+
console.log(consume(2,5, numCompare2));
96+
console.log(consume(2,5, numCompare3));
97+
console.log(consume(2,5, numCompare4));
98+
console.log(consume(2,5, numCompare5));
99+
console.log(consume(2,5, numCompare6));

challenges/objects-arrays.js

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,43 @@
77
// Object 2 -- tiger, carnivorous, 400lb, 6ft, india
88

99
// Object 3 -- koala, herbivorous, 40lb, 3ft, australia
10-
10+
let dog = {
11+
name: 'dog',
12+
diet: 'omnivorous',
13+
weight: 25,
14+
length: '2ft',
15+
area: 'worldwide',
16+
bark: function() {
17+
return "Woof!";
18+
}
19+
};
20+
let tiger = {
21+
name: 'tiger',
22+
diet: 'carnivorous',
23+
weight: 400,
24+
length: '6ft',
25+
area: 'India',
26+
hunt: function() {
27+
if (Math.floor(Math.random() * 10) > 7) {
28+
this.weight += 5;
29+
return 'Tiger hunt successful';
30+
} else {
31+
this.weight -= 5;
32+
return 'Tiger hunt failed';
33+
}
34+
}
35+
};
36+
let koala = {
37+
name: "koala",
38+
diet: "herbivorous",
39+
weight: 40,
40+
length:"3ft",
41+
area: "australia",
42+
climb: function() {
43+
this.weight += 1;
44+
return "Eucalyptus is great!";
45+
}
46+
};
1147
/* Using the console, log the following
1248
1349
How much the dog weighs
@@ -16,12 +52,17 @@ Where are koalas located
1652
What is the length of all 3 objects
1753
1854
*/
55+
console.log(dog.weight);
56+
console.log(tiger.diet);
57+
console.log(koala.area);
58+
console.log(`Dog: ${dog.length}, Tiger: ${tiger.length}, Koala: ${koala.length}`);
1959

2060
// Create a new bark method for the dog. When called, return "Woof!"
2161
// Create a new hunt method for the tiger. When called, if Math.random(10) is greater than 7, return "Tiger hunt successful" and add 5lb to the weight. If it is not, return "Tiger goes hungry" and subtract 5lbs to the weight.
2262
// Create a new climb method for the koala. When called, return "Eucalyptus is great"
23-
24-
63+
console.log(dog.bark());
64+
console.log(tiger.hunt(), tiger.weight);
65+
console.log(koala.climb(), koala.weight);
2566
// Arrays
2667

2768
// Given this array of game characters, complete the following tasks
@@ -39,8 +80,29 @@ const gameCharacters = [
3980
// Step 1: Create a new array that will contain all games. Once you have it made, sort it ascending[A-Z]. Log the result. Solve two ways:
4081

4182
// For Loop
83+
let newArr = [];
84+
85+
for(let i =0; i < gameCharacters.length; i++) {
86+
newArr.push(gameCharacters[i].game);
87+
}
88+
newArr.sort();
89+
console.log(newArr);
4290

4391
// Map or forEach
92+
let newArr2 = gameCharacters.map(character => character.game).sort();
93+
// let newArr2 = gameCharacaters.map(function(character) { return character.game;});
94+
95+
console.log(newArr2, "mapping");
96+
97+
let newArr3 = [];
98+
99+
gameCharacters.forEach(character => {
100+
newArr3.push(character.game);
101+
});
102+
newArr3.sort();
103+
console.log(newArr3, 'foreach');
104+
105+
44106

45107

46108
// Step 2: Create a new array that will contain the name and mission of each character.Log the result. Solve two ways:

challenges/prototypes.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,30 @@
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

55
// 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+
}
610

7-
// 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)
11+
// 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
12+
PentagonalPyramid.prototype.volume = function() {
13+
let formulaVolume = ((5/12)*(Math.sin(54)/Math.cos(54)))*(this.height)*(Math.pow(this.edge, 2));
14+
return formulaVolume;
15+
}
816

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/4) * (Math.sin(54)/Math.cos(54)) * (this.edge * this.edge)) + (((5*this.edge)/2) * Math.sqrt((this.height * this.height) + (((this.edge * (Math.sin(54)/Math.cos(54)))/2)**);
20+
let formulaSurfaceArea = 5
21+
return formulaSurfaceArea;
22+
}
23+
console.log(PentagonalPyramid, "Constructor");
1124
// Step 4: New Object -- You should create a new object that uses PentagonalPyramid. Give it the length, width, and height.
12-
25+
const pyramid = new PentagonalPyramid( {
26+
'edge': 5,
27+
'height': 2
28+
});
1329
// Test your volume and surfaceArea methods here using console.log
30+
console.log(pyramid);
31+
console.log(pyramid.volume());
32+
console.log(pyramid.surfaceArea());

0 commit comments

Comments
 (0)