chore(learn): audit javascript algorithms and data structures (#41092)
* chore(learn): audit basic algorithm scripting * chore(learn): audit basic data structures * chore(learn): audit basic javascript * chore(learn): audit debugging * chore(learn): audit es6 * chore(learn): audit functional programming * chore(learn): audit intermidate algorithms * chore(learn): audit js projects * chore(learn): audit object oriented programming * chore(learn): audit regex * fix(learn): remove stray . * fix(learn): string to code * fix(learn): missed some * fix(learn): clarify strings Based on Randy's feedback, clarifies string instances where quotes were removed in favour of back ticks. * fix: apply suggestions - thanks Randy! :) Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: non-suggestion comments * chore(learn): remove comments from codes Removes the comments from the description and instruction code blocks to ensure that all relevant information is translatable. * fix: Apply suggestions from code review Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * fix: revert crowdin fix * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-algorithm-scripting/mutations.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: Apply suggestions from code review Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-arrays.md Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * fix: Apply suggestions from code review Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * chore: change voice * fix: Christopher Nolan * fix: expressions would evaluate * fix: will -> would * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/add-methods-after-inheritance.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: to work to push * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-for-loops.md Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/add-methods-after-inheritance.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
This commit is contained in:
committed by
GitHub
parent
57f3c80345
commit
7117919d36
@ -10,16 +10,16 @@ dashedName: add-elements-to-the-end-of-an-array-using-concat-instead-of-push
|
||||
|
||||
Functional programming is all about creating and using non-mutating functions.
|
||||
|
||||
The last challenge introduced the `concat` method as a way to combine arrays into a new one without mutating the original arrays. Compare `concat` to the `push` method. `Push` adds an item to the end of the same array it is called on, which mutates that array. Here's an example:
|
||||
The last challenge introduced the `concat` method as a way to combine arrays into a new one without mutating the original arrays. Compare `concat` to the `push` method. `push` adds an item to the end of the same array it is called on, which mutates that array. Here's an example:
|
||||
|
||||
```js
|
||||
var arr = [1, 2, 3];
|
||||
arr.push([4, 5, 6]);
|
||||
// arr is changed to [1, 2, 3, [4, 5, 6]]
|
||||
// Not the functional programming way
|
||||
```
|
||||
|
||||
`Concat` offers a way to add new items to the end of an array without any mutating side effects.
|
||||
`arr` would have a modified value of `[1, 2, 3, [4, 5, 6]]`, which is not the functional programming way.
|
||||
|
||||
`concat` offers a way to add new items to the end of an array without any mutating side effects.
|
||||
|
||||
# --instructions--
|
||||
|
||||
|
@ -12,7 +12,7 @@ The last several challenges covered a number of useful array and string methods
|
||||
|
||||
Let's combine what we've learned to solve a practical problem.
|
||||
|
||||
Many content management sites (CMS) have the titles of a post added to part of the URL for simple bookmarking purposes. For example, if you write a Medium post titled "Stop Using Reduce", it's likely the URL would have some form of the title string in it (".../stop-using-reduce"). You may have already noticed this on the freeCodeCamp site.
|
||||
Many content management sites (CMS) have the titles of a post added to part of the URL for simple bookmarking purposes. For example, if you write a Medium post titled `Stop Using Reduce`, it's likely the URL would have some form of the title string in it (`.../stop-using-reduce`). You may have already noticed this on the freeCodeCamp site.
|
||||
|
||||
# --instructions--
|
||||
|
||||
@ -34,19 +34,19 @@ Your code should not use the `replace` method for this challenge.
|
||||
assert(!code.match(/\.?[\s\S]*?replace/g));
|
||||
```
|
||||
|
||||
`urlSlug("Winter Is Coming")` should return `"winter-is-coming"`.
|
||||
`urlSlug("Winter Is Coming")` should return the string `winter-is-coming`.
|
||||
|
||||
```js
|
||||
assert(urlSlug('Winter Is Coming') === 'winter-is-coming');
|
||||
```
|
||||
|
||||
`urlSlug(" Winter Is Coming")` should return `"winter-is-coming"`.
|
||||
`urlSlug(" Winter Is Coming")` should return the string `winter-is-coming`.
|
||||
|
||||
```js
|
||||
assert(urlSlug(' Winter Is Coming') === 'winter-is-coming');
|
||||
```
|
||||
|
||||
`urlSlug("A Mind Needs Books Like A Sword Needs A Whetstone")` should return `"a-mind-needs-books-like-a-sword-needs-a-whetstone"`.
|
||||
`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`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -55,7 +55,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
`urlSlug("Hold The Door")` should return `"hold-the-door"`.
|
||||
`urlSlug("Hold The Door")` should return the string `hold-the-door`.
|
||||
|
||||
```js
|
||||
assert(urlSlug('Hold The Door') === 'hold-the-door');
|
||||
|
@ -15,12 +15,12 @@ Here's an example:
|
||||
```js
|
||||
var arr = ["Hello", "World"];
|
||||
var str = arr.join(" ");
|
||||
// Sets str to "Hello World"
|
||||
```
|
||||
|
||||
`str` would have a value of the string `Hello World`.
|
||||
# --instructions--
|
||||
|
||||
Use the `join` method (among others) inside the `sentensify` function to make a sentence from the words in the string `str`. The function should return a string. For example, "I-like-Star-Wars" would be converted to "I like Star Wars". For this challenge, do not use the `replace` method.
|
||||
Use the `join` method (among others) inside the `sentensify` function to make a sentence from the words in the string `str`. The function should return a string. For example, `I-like-Star-Wars` would be converted to `I like Star Wars`. For this challenge, do not use the `replace` method.
|
||||
|
||||
# --hints--
|
||||
|
||||
@ -42,13 +42,13 @@ assert(!code.match(/\.?[\s\S]*?replace/g));
|
||||
assert(typeof sentensify('May-the-force-be-with-you') === 'string');
|
||||
```
|
||||
|
||||
`sentensify("May-the-force-be-with-you")` should return `"May the force be with you"`.
|
||||
`sentensify("May-the-force-be-with-you")` should return the string `May the force be with you`.
|
||||
|
||||
```js
|
||||
assert(sentensify('May-the-force-be-with-you') === 'May the force be with you');
|
||||
```
|
||||
|
||||
`sentensify("The.force.is.strong.with.this.one")` should return `"The force is strong with this one"`.
|
||||
`sentensify("The.force.is.strong.with.this.one")` should return the string `The force is strong with this one`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -57,7 +57,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
`sentensify("There,has,been,an,awakening")` should return `"There has been an awakening"`.
|
||||
`sentensify("There,has,been,an,awakening")` should return the string `There has been an awakening`.
|
||||
|
||||
```js
|
||||
assert(
|
||||
|
@ -12,9 +12,10 @@ dashedName: combine-two-arrays-using-the-concat-method
|
||||
|
||||
```js
|
||||
[1, 2, 3].concat([4, 5, 6]);
|
||||
// Returns a new array [1, 2, 3, 4, 5, 6]
|
||||
```
|
||||
|
||||
The returned array would be `[1, 2, 3, 4, 5, 6]`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Use the `concat` method in the `nonMutatingConcat` function to concatenate `attach` to the end of `original`. The function should return the concatenated array.
|
||||
|
@ -15,40 +15,38 @@ In other words, it restructures a function so it takes one argument, then return
|
||||
Here's an example:
|
||||
|
||||
```js
|
||||
//Un-curried function
|
||||
function unCurried(x, y) {
|
||||
return x + y;
|
||||
}
|
||||
|
||||
//Curried function
|
||||
function curried(x) {
|
||||
return function(y) {
|
||||
return x + y;
|
||||
}
|
||||
}
|
||||
//Alternative using ES6
|
||||
|
||||
const curried = x => y => x + y
|
||||
|
||||
curried(1)(2) // Returns 3
|
||||
curried(1)(2)
|
||||
```
|
||||
|
||||
`curried(1)(2)` would return `3`.
|
||||
|
||||
This is useful in your program if you can't supply all the arguments to a function at one time. You can save each function call into a variable, which will hold the returned function reference that takes the next argument when it's available. Here's an example using the curried function in the example above:
|
||||
|
||||
```js
|
||||
// Call a curried function in parts:
|
||||
var funcForY = curried(1);
|
||||
console.log(funcForY(2)); // Prints 3
|
||||
console.log(funcForY(2)); // 3
|
||||
```
|
||||
|
||||
Similarly, <dfn>partial application</dfn> can be described as applying a few arguments to a function at a time and returning another function that is applied to more arguments. Here's an example:
|
||||
|
||||
```js
|
||||
//Impartial function
|
||||
function impartial(x, y, z) {
|
||||
return x + y + z;
|
||||
}
|
||||
var partialFn = impartial.bind(this, 1, 2);
|
||||
partialFn(10); // Returns 13
|
||||
partialFn(10); // 13
|
||||
```
|
||||
|
||||
# --instructions--
|
||||
|
@ -12,10 +12,11 @@ A common pattern while working with arrays is when you want to remove items and
|
||||
|
||||
```js
|
||||
var cities = ["Chicago", "Delhi", "Islamabad", "London", "Berlin"];
|
||||
cities.splice(3, 1); // Returns "London" and deletes it from the cities array
|
||||
// cities is now ["Chicago", "Delhi", "Islamabad", "Berlin"]
|
||||
cities.splice(3, 1);
|
||||
```
|
||||
|
||||
Here `splice` returns the string `London` and deletes it from the cities array. `cities` will have the value `["Chicago", "Delhi", "Islamabad", "Berlin"]`.
|
||||
|
||||
As we saw in the last challenge, the `slice` method does not mutate the original array, but returns a new one which can be saved into a variable. Recall that the `slice` method takes two arguments for the indices to begin and end the slice (the end is non-inclusive), and returns those items in a new array. Using the `slice` method instead of `splice` helps to avoid any array-mutating side effects.
|
||||
|
||||
# --instructions--
|
||||
|
@ -15,9 +15,10 @@ Here's an example:
|
||||
```js
|
||||
var arr = ["Cat", "Dog", "Tiger", "Zebra"];
|
||||
var newArray = arr.slice(1, 3);
|
||||
// Sets newArray to ["Dog", "Tiger"]
|
||||
```
|
||||
|
||||
`newArray` would have the value `["Dog", "Tiger"]`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Use the `slice` method in the `sliceArray` function to return part of the `anim` array given the provided `beginSlice` and `endSlice` indices. The function should return an array.
|
||||
|
@ -19,17 +19,21 @@ function ascendingOrder(arr) {
|
||||
});
|
||||
}
|
||||
ascendingOrder([1, 5, 2, 3, 4]);
|
||||
// Returns [1, 2, 3, 4, 5]
|
||||
```
|
||||
|
||||
This would return the value `[1, 2, 3, 4, 5]`.
|
||||
|
||||
```js
|
||||
function reverseAlpha(arr) {
|
||||
return arr.sort(function(a, b) {
|
||||
return a === b ? 0 : a < b ? 1 : -1;
|
||||
});
|
||||
}
|
||||
reverseAlpha(['l', 'h', 'z', 'b', 's']);
|
||||
// Returns ['z', 's', 'l', 'h', 'b']
|
||||
```
|
||||
|
||||
This would return the value `['z', 's', 'l', 'h', 'b']`.
|
||||
|
||||
JavaScript's default sorting method is by string Unicode point value, which may return unexpected results. Therefore, it is encouraged to provide a callback function to specify how to sort the array items. When such a callback function, normally called `compareFunction`, is supplied, the array elements are sorted according to the return value of the `compareFunction`: If `compareFunction(a,b)` returns a value less than 0 for two elements `a` and `b`, then `a` will come before `b`. If `compareFunction(a,b)` returns a value greater than 0 for two elements `a` and `b`, then `b` will come before `a`. If `compareFunction(a,b)` returns a value equal to 0 for two elements `a` and `b`, then `a` and `b` will remain unchanged.
|
||||
|
||||
# --instructions--
|
||||
|
@ -15,13 +15,13 @@ Here are two examples that split one string by spaces, then another by digits us
|
||||
```js
|
||||
var str = "Hello World";
|
||||
var bySpace = str.split(" ");
|
||||
// Sets bySpace to ["Hello", "World"]
|
||||
|
||||
var otherString = "How9are7you2today";
|
||||
var byDigits = otherString.split(/\d/);
|
||||
// Sets byDigits to ["How", "are", "you", "today"]
|
||||
```
|
||||
|
||||
`bySpace` would have the value `["Hello", "World"]` and `byDigits` would have the value `["How", "are", "you", "today"]`.
|
||||
|
||||
Since strings are immutable, the `split` method makes it easier to work with them.
|
||||
|
||||
# --instructions--
|
||||
|
@ -17,9 +17,10 @@ var numbers = [1, 5, 8, 0, 10, 11];
|
||||
numbers.every(function(currentValue) {
|
||||
return currentValue < 10;
|
||||
});
|
||||
// Returns false
|
||||
```
|
||||
|
||||
The `every` method would return `false` here.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Use the `every` method inside the `checkPositive` function to check if every element in `arr` is positive. The function should return a Boolean value.
|
||||
|
@ -24,12 +24,14 @@ const users = [
|
||||
];
|
||||
|
||||
const usersUnder30 = users.filter(user => user.age < 30);
|
||||
console.log(usersUnder30); // [ { name: 'Amy', age: 20 }, { name: 'camperCat', age: 10 } ]
|
||||
console.log(usersUnder30);
|
||||
```
|
||||
|
||||
The console would display the value `[ { name: 'Amy', age: 20 }, { name: 'camperCat', age: 10 } ]`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
The variable `watchList` holds an array of objects with information on several movies. Use a combination of `filter` and `map` on `watchList` to assign a new array of objects with only `title` and `rating` keys. The new array should only include objects where `imdbRating` is greater than or equal to 8.0. Note that the rating values are saved as strings in the object and you may need to convert them into numbers to perform mathematical operations on them.
|
||||
The variable `watchList` holds an array of objects with information on several movies. Use a combination of `filter` and `map` on `watchList` to assign a new array of objects with only `title` and `rating` keys. The new array should only include objects where `imdbRating` is greater than or equal to 8.0. Note that the `rating` values are saved as strings in the object and you may need to convert them into numbers to perform mathematical operations on them.
|
||||
|
||||
# --hints--
|
||||
|
||||
|
@ -30,9 +30,11 @@ const users = [
|
||||
];
|
||||
|
||||
const names = users.map(user => user.name);
|
||||
console.log(names); // [ 'John', 'Amy', 'camperCat' ]
|
||||
console.log(names);
|
||||
```
|
||||
|
||||
The console would display the value ` [ 'John', 'Amy', 'camperCat' ]`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
The `watchList` array holds objects with information on several movies. Use `map` on `watchList` to assign a new array of objects with only `title` and `rating` keys to the `ratings` variable. The code in the editor currently uses a `for` loop to do this, so you should replace the loop functionality with your `map` expression.
|
||||
|
@ -26,9 +26,11 @@ const users = [
|
||||
];
|
||||
|
||||
const sumOfAges = users.reduce((sum, user) => sum + user.age, 0);
|
||||
console.log(sumOfAges); // 64
|
||||
console.log(sumOfAges);
|
||||
```
|
||||
|
||||
The console would display the value `64`.
|
||||
|
||||
In another example, see how an object can be returned containing the names of the users as properties with their ages as values.
|
||||
|
||||
```js
|
||||
@ -42,12 +44,14 @@ const usersObj = users.reduce((obj, user) => {
|
||||
obj[user.name] = user.age;
|
||||
return obj;
|
||||
}, {});
|
||||
console.log(usersObj); // { John: 34, Amy: 20, camperCat: 10 }
|
||||
console.log(usersObj);
|
||||
```
|
||||
|
||||
The console would display the value `{ John: 34, Amy: 20, camperCat: 10 }`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
The variable `watchList` holds an array of objects with information on several movies. Use `reduce` to find the average IMDB rating of the movies **directed by Christopher Nolan**. Recall from prior challenges how to `filter` data and `map` over it to pull what you need. You may need to create other variables, and return the average rating from `getRating` function. Note that the rating values are saved as strings in the object and need to be converted into numbers before they are used in any mathematical operations.
|
||||
The variable `watchList` holds an array of objects with information on several movies. Use `reduce` to find the average IMDB rating of the movies directed by `Christopher Nolan`. Recall from prior challenges how to `filter` data and `map` over it to pull what you need. You may need to create other variables, and return the average rating from `getRating` function. Note that the rating values are saved as strings in the object and need to be converted into numbers before they are used in any mathematical operations.
|
||||
|
||||
# --hints--
|
||||
|
||||
@ -77,7 +81,7 @@ Your code should not use a `for` loop.
|
||||
assert(!code.match(/for\s*?\([\s\S]*?\)/g));
|
||||
```
|
||||
|
||||
Your code should return correct output after modifying the `watchList` object.
|
||||
Your code should return the correct output after modifying the `watchList` object.
|
||||
|
||||
```js
|
||||
assert(getRating(watchList.filter((_, i) => i < 1 || i > 2)) === 8.55);
|
||||
|
@ -17,9 +17,10 @@ var numbers = [10, 50, 8, 220, 110, 11];
|
||||
numbers.some(function(currentValue) {
|
||||
return currentValue < 10;
|
||||
});
|
||||
// Returns true
|
||||
```
|
||||
|
||||
The `some` method would return `true`.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Use the `some` method inside the `checkPositive` function to check if any element in `arr` is positive. The function should return a Boolean value.
|
||||
|
Reference in New Issue
Block a user