Skip to content

Commit 092e8b0

Browse files
Quinton KornegayQuinton Kornegay
Quinton Kornegay
authored and
Quinton Kornegay
committed
More Functions Exercises
1 parent 1b81e49 commit 092e8b0

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

more-on-functions/exercises/practice-your-skills.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ a) If passed a number, return the tripled value.
55
b) If passed a string, return the string “ARRR!”
66
c) Be sure to test your function before moving on the next part.
77
*/
8+
let tripledValue = function (element) {
9+
if (typeof element === "number") {
10+
return element * 3;
11+
} else if (typeof element === "string") {
12+
return "ARRR!";
13+
} else {
14+
return element;
15+
}
16+
}
17+
18+
console.log(tripledValue(43));
19+
820

921
/* Add to your code! Use your fuction and the map method to change an array as follows:
1022
a) Triple any the numbers.
@@ -13,3 +25,7 @@ c) Print the new array to confirm your work.
1325
*/
1426

1527
let arr = ['Elocution', 21, 'Clean teeth', 100];
28+
29+
let mappedArray = arr.map(tripledValue);
30+
31+
console.log(mappedArray);

more-on-functions/exercises/raid-a-shuttle.js

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ function holdStatus(arr){
2121
let fuelLevel = 200000;
2222
let cargoHold = ['meal kits', 'space suits', 'first-aid kit', 'satellite', 'gold', 'water', 'AE-35 unit'];
2323

24-
console.log("Fuel level: " + checkFuel(fuelLevel));
25-
console.log("Hold status: " + holdStatus(cargoHold));
24+
// console.log("Fuel level: " + checkFuel(fuelLevel));
25+
// console.log("Hold status: " + holdStatus(cargoHold));
2626

2727
/* Steal some fuel from the shuttle:
28-
* /
28+
*/
2929

3030
//a). Define an anonymous function and set it equal to a variable with a normal, non-suspicious name. The function takes one parameter. This will be the fuel level on the shuttle.
3131

@@ -35,8 +35,21 @@ console.log("Hold status: " + holdStatus(cargoHold));
3535

3636
//d). Decide where to best place your function call to gather our new fuel.
3737

38+
let checkForCleanFuel = function(a) {
39+
if (checkFuel(a) === 'green') {
40+
return a - 100000;
41+
} else if (checkFuel(a) === 'yellow') {
42+
return a - 50000;
43+
} else {
44+
return a;
45+
}
46+
}
47+
// console.log("New Fuel Level: " + checkForCleanFuel(fuelLevel));
48+
// console.log("Fuel Level: " + fuelLevel)
49+
50+
3851
/* Next, liberate some of that glorious cargo.
39-
* /
52+
*/
4053

4154
//a). Define another anonymous function with an array as a parameter, and set it equal to another innocent variable.
4255

@@ -45,13 +58,35 @@ console.log("Hold status: " + holdStatus(cargoHold));
4558
//c). The cargo hold has better security than the fuel tanks. It counts how many things are in storage. You need to replace what you steal with something worthless. The count MUST stay the same, or you’ll get caught and thrown into the LaunchCode brig.
4659

4760
//d). Don’t get hasty, matey! Remember to test your function.
61+
let fakeCargo = ['football pads', 'silver chain'];
62+
stolenCargo = [];
63+
64+
let checkForBomb = function (arr) {
65+
if (typeof arr === 'object') {
66+
stolenCargo = arr.splice(3,2);
67+
return arr.concat(fakeCargo);
68+
} else {
69+
return arr;
70+
}
71+
}
72+
73+
console.log(checkForBomb(cargoHold));
74+
console.log(stolenCargo);
75+
4876

4977
/* Finally, you need to print a receipt for the accountant. Don’t laugh! That genius knows MATH and saves us more gold than you can imagine.
50-
* /
78+
*/
5179

5280
//a). Define a function called irs that can take fuelLevel and cargoHold as arguments.
5381

5482
//b). Call your anonymous fuel and cargo functions from within irs.
5583

5684
//c). Use a template literal to return, "Raided _____ kg of fuel from the tanks, and stole ____ and ____ from the cargo hold."
5785

86+
let irs = function (levelOfFuel, itemsInCargo) {
87+
let arr = checkForBomb(itemsInCargo);
88+
let arr2 = checkForCleanFuel(levelOfFuel);
89+
return `Raided ${checkForCleanFuel(fuelLevel)} kg of fuel from the tanks, and stole ${stolenCargo[0]} and ${stolenCargo[1]} from the cargo hold.`
90+
}
91+
92+
console.log(irs());

0 commit comments

Comments
 (0)