Skip to content

Commit a1371a5

Browse files
Merge pull request LaunchCodeEducation#10 from LaunchCodeEducation/more-on-functions
More on functions
2 parents 5fc4d86 + 2cf9110 commit a1371a5

20 files changed

+291
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "Chapter 11: More on Functions Chapter Examples Directory",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"readline-sync": "^1.4.10"
14+
}
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function reverse(str) {
2+
let lettersArray = str.split('');
3+
let reversedLettersArray = lettersArray.reverse();
4+
return reversedLettersArray.join('');
5+
}
6+
7+
console.log(reverse("LaunchCode"));
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const input = require('readline-sync');
2+
let userInput = input.question("Please enter a number:");
3+
let logger = function(errorMsg) {
4+
console.log("ERROR: " + errorMsg);
5+
};
6+
if (userInput < 0) {
7+
// ______("Invalid input");
8+
}
9+
10+
// Fill in the blank in line 7 (then uncomment it) so that it logs an error message if userInput is negative.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function decreasingSum(integer) {
2+
if (integer === 1){
3+
return integer;
4+
} else {
5+
//call decreasingSum function again
6+
}
7+
}
8+
9+
console.log(decreasingSum(5));
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function combineEntries(arrayName) {
2+
if (arrayName.length <= 1){
3+
return arrayName[0];
4+
} else {
5+
//console.log(arrayName[0], arrayName.slice(1));
6+
return arrayName[0] + combineEntries(arrayName.slice(1));
7+
}
8+
}
9+
10+
//First, run the code to see the result.
11+
12+
//Next, uncomment the console.log statement above to see how each call to combineEntries looks at a different section of the original array.
13+
14+
let arr = ['L', 'C', '1', '0', '1'];
15+
16+
console.log(combineEntries(arr));
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "Chapter 11: More on Functions Chapter Examples Directory",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"readline-sync": "^1.4.10"
14+
}
15+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function printMessage() {
2+
console.log("The future is now!");
3+
}
4+
5+
setTimeout(printMessage, 5000);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
let nums = [3.14, 42, 4811];
2+
3+
// TODO: Write a mapping function
4+
// and pass it to .map()
5+
let halved = nums.map();
6+
7+
console.log(halved);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
let names = ["Chris", "Jim", "Sally", "Blake", "Paul", "John", "Courtney", "Carly"];
2+
3+
// TODO: Write a mapping function
4+
// and pass it to .map()
5+
let firstInitials = names.map();
6+
7+
console.log(firstInitials);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "Chapter 11: More on Functions Chapter Examples Directory",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"readline-sync": "^1.4.10"
14+
}
15+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function callMe(func) {
2+
func();
3+
}
4+
5+
callMe("Al");
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const input = require('readline-sync');
2+
3+
function getValidInput(prompt, isValid) {
4+
5+
let userInput = input.question(prompt);
6+
7+
while (!isValid(userInput)) {
8+
console.log("Invalid input. Try again.");
9+
userInput = input.question(prompt);
10+
}
11+
12+
return userInput;
13+
}
14+
15+
// TODO 1: write a validator
16+
// that ensures input starts with "a"
17+
18+
// TODO 2: write a validator
19+
// that ensures input is a vowel
20+
21+
// Be sure to test your code!
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "Chapter 11: More on Functions Chapter Examples Directory",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"readline-sync": "^1.4.10"
14+
}
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//The following concept check assumes that only positive integers are passed to the function.
2+
3+
function factorial(integer){
4+
if (/*enter base case test here*/){
5+
return integer;
6+
} else {
7+
return integer*(factorial(integer-1));
8+
}
9+
}
10+
11+
console.log(factorial(4));
12+
13+
//Skill boost! Add validation to return an error message if the function is passed a string, negative number or a decimal value.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function removeI(arr) {
2+
if (/* Enter base case test here */){
3+
return arr;
4+
} else {
5+
arr.splice(arr.indexOf('i'),1);
6+
return removeI(arr);
7+
}
8+
};
9+
10+
let arrayToChange = ['One', 'i', 'c', 'X', 'i', 'i', 54];
11+
12+
console.log(removeI(arrayToChange));
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//Create an anonymous function and set it equal to a variable.
2+
3+
/* Your function should:
4+
a) If passed a number, return the tripled value.
5+
b) If passed a string, return the string “ARRR!”
6+
c) Be sure to test your function before moving on the next part.
7+
*/
8+
9+
/* Add to your code! Use your fuction and the map method to change an array as follows:
10+
a) Triple any the numbers.
11+
b) Replace any strings with “ARRR!”
12+
c) Print the new array to confirm your work.
13+
*/
14+
15+
let arr = ['Elocution', 21, 'Clean teeth', 100];
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
function checkFuel(level) {
2+
if (level > 100000){
3+
return 'green';
4+
} else if (level > 50000){
5+
return 'yellow';
6+
} else {
7+
return 'red';
8+
}
9+
}
10+
11+
function holdStatus(arr){
12+
if (arr.length < 7) {
13+
return `Spaces available: ${7-arr.length}.`;
14+
} else if (arr.length > 7){
15+
return `Over capacity by ${arr.length-7} items.`;
16+
} else {
17+
return "Full";
18+
}
19+
}
20+
21+
let fuelLevel = 200000;
22+
let cargoHold = ['meal kits', 'space suits', 'first-aid kit', 'satellite', 'gold', 'water', 'AE-35 unit'];
23+
24+
console.log("Fuel level: " + checkFuel(fuelLevel));
25+
console.log("Hold status: " + holdStatus(cargoHold));
26+
27+
/* Steal some fuel from the shuttle:
28+
* /
29+
30+
//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.
31+
32+
//b). You must siphon off fuel without alerting the TAs. Inside your function, you want to reduce the fuel level as much as possible WITHOUT changing the color returned by the checkFuel function.
33+
34+
//c). Once you figure out how much fuel to pump out, return that value.
35+
36+
//d). Decide where to best place your function call to gather our new fuel.
37+
38+
/* Next, liberate some of that glorious cargo.
39+
* /
40+
41+
//a). Define another anonymous function with an array as a parameter, and set it equal to another innocent variable.
42+
43+
//b). You need to swipe two items from the cargo hold. Choose well. Stealing water ain’t gonna get us rich. Put the swag into a new array and return it from the function.
44+
45+
//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.
46+
47+
//d). Don’t get hasty, matey! Remember to test your function.
48+
49+
/* Finally, you need to print a receipt for the accountant. Dont laugh! That genius knows MATH and saves us more gold than you can imagine.
50+
* /
51+
52+
//a). Define a function called irs that can take fuelLevel and cargoHold as arguments.
53+
54+
//b). Call your anonymous fuel and cargo functions from within irs.
55+
56+
//c). Use a template literal to return, "Raided _____ kg of fuel from the tanks, and stole ____ and ____ from the cargo hold."
57+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//1) Create a function with an array of numbers as its parameter. The function should iterate through the array and return the minimum value from the array. Hint: Use what you know about if statements to identify and store the smallest value within the array.
2+
3+
//Sample arrays for testing:
4+
let nums1 = [5, 10, 2, 42];
5+
let nums2 = [-2, 0, -10, -44, 5, 3, 0, 3];
6+
let nums3 = [200, 5, 4, 10, 8, 5, -3.3, 4.4, 0];
7+
8+
//Using one of the test arrays as the argument, call your function inside the console.log statement below.
9+
10+
console.log(/* your code here */);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//Sample arrays for testing:
2+
let nums1 = [5, 10, 2, 42];
3+
let nums2 = [-2, 0, -10, -44, 5, 3, 0, 3];
4+
let nums3 = [200, 5, 4, 10, 8, 5, -3.3, 4.4, 0];
5+
6+
//Sort each array in ascending order.
7+
8+
//Sort each array in decending order.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function findMinValue(arr){
2+
let min = arr[0];
3+
for (i = 0; i < arr.length; i++){
4+
if (arr[i] < min){
5+
min = arr[i];
6+
}
7+
}
8+
return min;
9+
}
10+
11+
//Create a function with an array of numbers as its parameter. This function will return a new array with the numbers sorted from least to greatest value.
12+
13+
/*Within the function:
14+
1) Define a new, empty array to hold the final sorted numbers.
15+
2) Use the findMinValue function to find the minimum value in the old array.
16+
3) Add the minimum value to the new array, and remove the minimum value from the old array.
17+
4) Repeat parts b & c until the old array is empty.
18+
5) Return the new sorted array.
19+
6) Be sure to print the results in order to verify your code.*/
20+
21+
//Your function here...
22+
23+
/* BONUS MISSION: Refactor your sorting function to use recursion below:
24+
*/
25+
26+
//Sample arrays for testing:
27+
let nums1 = [5, 10, 2, 42];
28+
let nums2 = [-2, 0, -10, -44, 5, 3, 0, 3];
29+
let nums3 = [200, 5, 4, 10, 8, 5, -3.3, 4.4, 0];

0 commit comments

Comments
 (0)