Skip to content

Commit c5e9f86

Browse files
committed
Functional Programming (ex. 17 to 24)
+ Finished Functional Programming module
1 parent f23b497 commit c5e9f86

10 files changed

+182
-9
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Sort an Array Alphabetically using the sort Method:
3+
Use the sort method in the alphabeticalOrder function to sort the elements of arr in alphabetical order.
4+
The function should return the sorted array.
5+
6+
- Your code should use the sort method.
7+
- alphabeticalOrder(["a", "d", "c", "a", "z", "g"]) should return ["a", "a", "c", "d", "g", "z"].
8+
- alphabeticalOrder(["x", "h", "a", "m", "n", "m"]) should return ["a", "h", "m", "m", "n", "x"].
9+
- alphabeticalOrder(["a", "a", "a", "a", "x", "t"]) should return ["a", "a", "a", "a", "t", "x"].
10+
*/
11+
function alphabeticalOrder(arr) {
12+
// Only changed code below this line
13+
return arr.sort();
14+
// or
15+
// return arr.sort((a, b) => a === b ? 0 : a > b ? 1 : -1);
16+
// Only changed code above this line
17+
}
18+
19+
console.log(alphabeticalOrder(["a", "d", "c", "a", "z", "g"]));
20+
console.log(alphabeticalOrder(["x", "h", "a", "m", "n", "m"]));
21+
console.log(alphabeticalOrder(["a", "a", "a", "a", "x", "t"]));
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Return a Sorted Array Without Changing the Original Array:
3+
Use the sort method in the nonMutatingSort function to sort the elements of an array in ascending order.
4+
The function should return a new array, and not mutate the globalArray variable.
5+
6+
- Your code should use the sort method.
7+
- The globalArray variable should not change.
8+
- nonMutatingSort(globalArray) should return [2, 3, 5, 6, 9].
9+
- nonMutatingSort(globalArray) should not be hard coded.
10+
- The function should return a new array, not the array passed to it.
11+
- nonMutatingSort([1, 30, 4, 21, 100000]) should return [1, 4, 21, 30, 100000].
12+
- nonMutatingSort([140000, 104, 99]) should return [99, 104, 140000].
13+
*/
14+
const globalArray = [5, 6, 3, 2, 9];
15+
16+
function nonMutatingSort(arr) {
17+
// Only changed code below this line
18+
return [...arr].sort(function (a, b) {
19+
return a - b;
20+
});
21+
// Only changed code above this line
22+
}
23+
24+
console.log("nonMutatingSort", nonMutatingSort(globalArray));
25+
console.log("nonMutatingSort_1", nonMutatingSort([1, 30, 4, 21, 100000]));
26+
console.log("nonMutatingSort_2", nonMutatingSort([140000, 104, 99]));
27+
console.log("globalArray", globalArray);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Split a String into an Array Using the split Method:
3+
Use the split method inside the splitify function to split str into an array of words.
4+
The function should return the array.
5+
Note that the words are not always separated by spaces, and the array should not contain punctuation.
6+
7+
- Your code should use the split method.
8+
- splitify("Hello World,I-am code") should return ["Hello", "World", "I", "am", "code"].
9+
- splitify("Earth-is-our home") should return ["Earth", "is", "our", "home"].
10+
- splitify("This.is.a-sentence") should return ["This", "is", "a", "sentence"].
11+
*/
12+
function splitify(str) {
13+
// Only changed code below this line
14+
return str.split(/[^A-z]/);
15+
// Only changed code above this line
16+
}
17+
18+
console.log(splitify("Hello World,I-am code"));
19+
console.log(splitify("Earth-is-our home"));
20+
console.log(splitify("This.is.a-sentence"));
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Combine an Array into a String Using the join Method:
3+
Use the join method (among others) inside the sentensify function to make a sentence from the words in the string str.
4+
The function should return a string. For example, I-like-Star-Wars would be converted to I like Star Wars.
5+
For this challenge, do not use the replace method.
6+
7+
- Your code should use the join method.
8+
- Your code should not use the replace method.
9+
- sentensify("May-the-force-be-with-you") should return a string.
10+
- sentensify("May-the-force-be-with-you") should return the string May the force be with you.
11+
- sentensify("The.force.is.strong.with.this.one") should return the string The force is strong with this one.
12+
- sentensify("There,has,been,an,awakening") should return the string There has been an awakening.
13+
*/
14+
function sentensify(str) {
15+
// Only changed code below this line
16+
return str.split(/[^A-z]/).join(" ");
17+
// Only changed code above this line
18+
}
19+
20+
console.log(sentensify("May-the-force-be-with-you"));
21+
console.log(sentensify("The.force.is.strong.with.this.one"));
22+
console.log(sentensify("There,has,been,an,awakening"));
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Apply Functional Programming to Convert Strings to URL Slugs:
3+
Fill in the urlSlug function so it converts a string title and returns the hyphenated version for the URL.
4+
You can use any of the methods covered in this section, and don't use replace. Here are the requirements:
5+
The input is a string with spaces and title-cased words
6+
The output is a string with the spaces between words replaced by a hyphen (-)
7+
The output should be all lower-cased letters
8+
The output should not have any spaces
9+
10+
- Your code should not use the replace method for this challenge.
11+
- urlSlug("Winter Is Coming") should return the string winter-is-coming.
12+
- urlSlug(" Winter Is Coming") should return the string winter-is-coming.
13+
- urlSlug("A Mind Needs Books Like A Sword Needs A Whetstone") should return the string a-mind-needs-books-like-a-sword-needs-a-whetstone.
14+
- urlSlug("Hold The Door") should return the string hold-the-door.
15+
*/
16+
// Only changed code below this line
17+
function urlSlug(title) {
18+
return title
19+
.trim()
20+
.split(/[\W]/)
21+
.filter(string => string.length > 0)
22+
.join("-")
23+
.toLowerCase();
24+
}
25+
// Only changed code above this line
26+
console.log(urlSlug("A Mind Needs Books Like A Sword Needs A Whetstone"));
27+
console.log(urlSlug("Winter Is Coming"));
28+
console.log(urlSlug(" Winter Is Coming"));
29+
console.log(urlSlug("Hold The Door"));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Use the every Method to Check that Every Element in an Array Meets a Criteria:
3+
Use the every method inside the checkPositive function to check if every element in arr is positive.
4+
The function should return a Boolean value.
5+
6+
- Your code should use the every method.
7+
- checkPositive([1, 2, 3, -4, 5]) should return false.
8+
- checkPositive([1, 2, 3, 4, 5]) should return true.
9+
- checkPositive([1, -2, 3, -4, 5]) should return false.
10+
*/
11+
function checkPositive(arr) {
12+
// Only changed code below this line
13+
return arr.every(number => number > 0);
14+
// Only changed code above this line
15+
}
16+
17+
console.log(checkPositive([1, 2, 3, -4, 5]));
18+
console.log(checkPositive([1, 2, 3, 4, 5]));
19+
console.log(checkPositive([1, -2, 3, -4, 5]));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Use the some Method to Check that Any Elements in an Array Meet a Criteria:
3+
Use the some method inside the checkPositive function to check if any element in arr is positive.
4+
The function should return a Boolean value.
5+
6+
- Your code should use the some method.
7+
- checkPositive([1, 2, 3, -4, 5]) should return true.
8+
- checkPositive([1, 2, 3, 4, 5]) should return true.
9+
- checkPositive([-1, -2, -3, -4, -5]) should return false.
10+
*/
11+
function checkPositive(arr) {
12+
// Only change code below this line
13+
return arr.some(value => value > 0);
14+
// Only change code above this line
15+
}
16+
17+
console.log(checkPositive([1, 2, 3, -4, 5]));
18+
console.log(checkPositive([1, 2, 3, 4, 5]));
19+
console.log(checkPositive([-1, -2, -3, -4, -5]));
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
Introduction to Currying and Partial Application:
3+
Fill in the body of the add function so it uses currying to add parameters x, y, and z.
4+
5+
- add(10)(20)(30) should return 60.
6+
- add(1)(2)(3) should return 6.
7+
- add(11)(22)(33) should return 66.
8+
- Your code should include a final statement that returns x + y + z.
9+
*/
10+
function add(x) {
11+
// Only changed code below this line
12+
return y => z => x + y + z
13+
// Only changed code above this line
14+
}
15+
16+
console.log(add(10)(20)(30));

08-functional-programming/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ freeCodeCamp module description:
2525
- [X] [ 14 - Add Elements to the End of an Array Using concat Instead of push](14-add-elements-to-the-end-of-an-array-using-concat-instead-of-push.js)
2626
- [X] [ 15 - Use the reduce Method to Analyze Data](15-use-the-reduce-method-to-analyze-data.js)
2727
- [X] [ 16 - Use Higher-Order Functions map, filter, or reduce to Solve a Complex Problem](16-use-higher-order-functions-map-filter-or-reduce-to-solve-a-complex-problem.js)
28-
- [ ] [ 17 - Sort an Array Alphabetically using the sort Method]()
29-
- [ ] [ 18 - Return a Sorted Array Without Changing the Original Array]()
30-
- [ ] [ 19 - Split a String into an Array Using the split Method]()
31-
- [ ] [ 20 - Combine an Array into a String Using the join Method]()
32-
- [ ] [ 21 - Apply Functional Programming to Convert Strings to URL Slugs]()
33-
- [ ] [ 22 - Use the every Method to Check that Every Element in an Array Meets a Criteria]()
34-
- [ ] [ 23 - Use the some Method to Check that Any Elements in an Array Meet a Criteria]()
35-
- [ ] [ 24 - Introduction to Currying and Partial Application]()
28+
- [X] [ 17 - Sort an Array Alphabetically using the sort Method](17-sort-an-array-alphabetically-using-the-sort-method.js)
29+
- [X] [ 18 - Return a Sorted Array Without Changing the Original Array](18-return-a-sorted-array-without-changing-the-original-array.js)
30+
- [X] [ 19 - Split a String into an Array Using the split Method](19-split-a-string-into-an-array-using-the-split-method.js)
31+
- [X] [ 20 - Combine an Array into a String Using the join Method](20-combine-an-array-into-a-string-using-the-join-method.js)
32+
- [X] [ 21 - Apply Functional Programming to Convert Strings to URL Slugs](21-apply-functional-programming-to-convert-strings-to-url-slugs.js)
33+
- [X] [ 22 - Use the every Method to Check that Every Element in an Array Meets a Criteria](22-use-the-every-method-to-check-that-every-element-in-an-array-meets-a-criteria.js)
34+
- [X] [ 23 - Use the some Method to Check that Any Elements in an Array Meet a Criteria](23-use-the-some-method-to-check-that-any-elements-in-an-array-meet-a-criteria.js)
35+
- [X] [ 24 - Introduction to Currying and Partial Application](24-introduction-to-currying-and-partial-application.js)
3636

3737
⬅️ [Back to main file](../README.md)
3838

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ freeCodeCamp course description:
1616
- [X] [ 05 - Basic Data Structures](/05-basic-data-structures/README.md)
1717
- [X] [ 06 - Basic Algorithm Scripting](/06-basic-algorithm-scripting/README.md)
1818
- [X] [ 07 - Object Oriented Programming](/07-object-oriented-programming/README.md)
19-
- [ ] [ 08 - Functional Programming](/08-functional-programming/README.md)
19+
- [X] [ 08 - Functional Programming](/08-functional-programming/README.md)
2020
- [ ] [ 09 - Intermediate Algorithm Scripting]()
2121
- [ ] [ 10 - JavaScript Algorithms and Data Structures Projects]()
2222

0 commit comments

Comments
 (0)