fix(guide): restructure curriculum guide articles (#36501)
* fix: restructure certifications guide articles * fix: added 3 dashes line before prob expl * fix: added 3 dashes line before hints * fix: added 3 dashes line before solutions
This commit is contained in:
@@ -2,18 +2,25 @@
|
||||
title: Add Elements to the End of an Array Using concat Instead of push
|
||||
---
|
||||
|
||||
## Add Elements to the End of an Array Using concat Instead of push
|
||||
# Add Elements to the End of an Array Using concat Instead of push
|
||||
|
||||
---
|
||||
## Problem Explanation
|
||||
Where the `push` method adds new element to the end of the orginal array, the `concat` method creates a new array containing the elements from the original array and the new element. The original array remains the same when using `concat`.
|
||||
|
||||
#### Relevant Links:
|
||||
#### Relevant Links
|
||||
- [Array.prototype.concat()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat)
|
||||
|
||||
## Solution
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
function nonMutatingPush(original, newItem) {
|
||||
|
||||
// Add your code below this line
|
||||
|
||||
|
||||
return original.concat(newItem);
|
||||
|
||||
// Add your code above this line
|
||||
@@ -22,5 +29,5 @@ function nonMutatingPush(original, newItem) {
|
||||
var first = [1, 2, 3];
|
||||
var second = [4, 5];
|
||||
nonMutatingPush(first, second);
|
||||
|
||||
```
|
||||
```
|
||||
</details>
|
@@ -1,9 +1,13 @@
|
||||
---
|
||||
title: Apply Functional Programming to Convert Strings to URL Slugs
|
||||
---
|
||||
## Apply Functional Programming to Convert Strings to URL Slugs
|
||||
# Apply Functional Programming to Convert Strings to URL Slugs
|
||||
|
||||
### Solution
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
// the global variable
|
||||
@@ -11,29 +15,37 @@ var globalTitle = "Winter Is Coming";
|
||||
|
||||
// Add your code below this line
|
||||
function urlSlug(title) {
|
||||
return title.split(/\W/).filter((obj)=>{
|
||||
return obj !=='';
|
||||
}).join('-').toLowerCase();
|
||||
|
||||
return title
|
||||
.split(/\W/)
|
||||
.filter(obj => {
|
||||
return obj !== "";
|
||||
})
|
||||
.join("-")
|
||||
.toLowerCase();
|
||||
}
|
||||
// Add your code above this line
|
||||
|
||||
var winterComing = urlSlug(globalTitle); // Should be "winter-is-coming"
|
||||
```
|
||||
</details>
|
||||
|
||||
### Alternative solution
|
||||
<details><summary>Solution 2 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
|
||||
// the global variable
|
||||
var globalTitle = "Winter Is Coming";
|
||||
|
||||
// Add your code below this line
|
||||
function urlSlug(title) {
|
||||
return title.toLowerCase().trim().split(/\s+/).join('-');
|
||||
return title
|
||||
.toLowerCase()
|
||||
.trim()
|
||||
.split(/\s+/)
|
||||
.join("-");
|
||||
}
|
||||
// Add your code above this line
|
||||
|
||||
var winterComing = urlSlug(globalTitle); // Should be "winter-is-coming"
|
||||
|
||||
```
|
||||
|
||||
</details>
|
@@ -1,28 +1,40 @@
|
||||
---
|
||||
title: Avoid Mutations and Side Effects Using Functional Programming
|
||||
---
|
||||
## Avoid Mutations and Side Effects Using Functional Programming
|
||||
|
||||
### Problem Explanation
|
||||
# Avoid Mutations and Side Effects Using Functional Programming
|
||||
|
||||
---
|
||||
## Problem Explanation
|
||||
|
||||
Fill in the code for the function `incrementer` so it returns the value of the global variable `fixedValue` increased by one. `fixedValue` should not change, no matter how many times the function `incrememter` is called.
|
||||
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
|
||||
Using the increment operator (`++`) on `fixedValue` will mutate `fixedValue`, meaning it will no longer reference the same value it was assigned with.
|
||||
|
||||
### Solution:
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
// the global variable
|
||||
var fixedValue = 4;
|
||||
|
||||
function incrementer () {
|
||||
function incrementer() {
|
||||
// Add your code below this line
|
||||
return fixedValue + 1;
|
||||
|
||||
|
||||
// Add your code above this line
|
||||
}
|
||||
|
||||
var newValue = incrementer(); // Should equal 5
|
||||
console.log(fixedValue); // Should print 4
|
||||
```
|
||||
</details>
|
||||
|
@@ -1,17 +1,22 @@
|
||||
---
|
||||
title: Combine an Array into a String Using the join Method
|
||||
---
|
||||
## Combine an Array into a String Using the join Method
|
||||
# Combine an Array into a String Using the join Method
|
||||
|
||||
### Problem Explanation
|
||||
---
|
||||
## Problem Explanation
|
||||
|
||||
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.
|
||||
|
||||
#### Relevant Links:
|
||||
#### Relevant Links
|
||||
- [Array.prototype.join()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join)
|
||||
- [String.prototype.split()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split)
|
||||
- [Regular Expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions)
|
||||
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint1
|
||||
You may need to convert the string to an array first.
|
||||
|
||||
@@ -19,12 +24,18 @@ You may need to convert the string to an array first.
|
||||
You may need to use regular expression to split the string.
|
||||
|
||||
|
||||
### Solution:
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
function sentensify(str) {
|
||||
// Add your code below this line
|
||||
return str.split(/\W/).join(' ');
|
||||
return str.split(/\W/).join(" ");
|
||||
// Add your code above this line
|
||||
}
|
||||
sentensify("May-the-force-be-with-you");
|
||||
```
|
||||
</details>
|
||||
|
@@ -1,21 +1,34 @@
|
||||
---
|
||||
title: Combine Two Arrays Using the concat Method
|
||||
---
|
||||
## Combine Two Arrays Using the concat Method
|
||||
# Combine Two Arrays Using the concat Method
|
||||
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
- The concat method is used to join two or more arrays or strings.
|
||||
|
||||
### Hint 2
|
||||
- This method does not mutate the existing arrays, but returns a new array.
|
||||
|
||||
## Solution
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
function nonMutatingConcat(original, attach) {
|
||||
// Add your code below this line
|
||||
|
||||
return original.concat(attach);
|
||||
|
||||
|
||||
return original.concat(attach);
|
||||
|
||||
// Add your code above this line
|
||||
}
|
||||
var first = [1, 2, 3];
|
||||
var second = [4, 5];
|
||||
nonMutatingConcat(first, second);
|
||||
```
|
||||
</details>
|
||||
|
@@ -2,60 +2,73 @@
|
||||
title: Implement map on a Prototype
|
||||
---
|
||||
|
||||
## Implement map on a Prototype
|
||||
# Implement map on a Prototype
|
||||
|
||||
### Solution 1 - Solve this challenge using `this` and the forEach method
|
||||
|
||||
The `this` keyword gives us access to the object we are calling myMap on.
|
||||
---
|
||||
## Solutions
|
||||
|
||||
From there we can use the forEach method to add elements to already declared empty array as we modify each element with the given callback method.
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
// the global Array
|
||||
var s = [23, 65, 98, 5];
|
||||
|
||||
Array.prototype.myMap = function(callback){
|
||||
Array.prototype.myMap = function(callback) {
|
||||
var newArray = [];
|
||||
// Add your code below this line
|
||||
this.forEach(a => newArray.push(callback(a)));
|
||||
// Add your code above this line
|
||||
return newArray;
|
||||
|
||||
};
|
||||
|
||||
var new_s = s.myMap(function(item){
|
||||
var new_s = s.myMap(function(item) {
|
||||
return item * 2;
|
||||
});
|
||||
```
|
||||
|
||||
### Solution 2 - Solve this challenge using a "for" loop and `this`
|
||||
#### Code Explanation
|
||||
|
||||
The use of a "for" loop allows us to apply the callback function to every item in the Global array and then push the modified items to the empty new array that is returned in the end.
|
||||
- Solve this challenge using `this` and the forEach method
|
||||
|
||||
- The `this` keyword gives us access to the object we are calling myMap on.
|
||||
|
||||
- From there we can use the forEach method to add elements to already declared empty array as we modify each element with the given callback method.
|
||||
</details>
|
||||
|
||||
|
||||
<details><summary>Solution 2 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
// The global Array
|
||||
var s = [23, 65, 98, 5];
|
||||
|
||||
Array.prototype.myMap = function(callback){
|
||||
Array.prototype.myMap = function(callback) {
|
||||
var newArray = [];
|
||||
|
||||
|
||||
// Add your code below this line
|
||||
for (let i = 0; i < this.length; i++) {
|
||||
newArray.push(callback(this[i]));
|
||||
}
|
||||
// Add your code above this line
|
||||
|
||||
|
||||
return newArray;
|
||||
};
|
||||
|
||||
var new_s = s.myMap(function(item){
|
||||
var new_s = s.myMap(function(item) {
|
||||
return item * 2;
|
||||
});
|
||||
```
|
||||
|
||||
### Useful Links
|
||||
#### Code Explanation
|
||||
- Solve this challenge using a "for" loop and `this`
|
||||
|
||||
- The use of a "for" loop allows us to apply the callback function to every item in the Global array and then push the modified items to the empty new array that is returned in the end.
|
||||
|
||||
#### Relevant Links
|
||||
|
||||
[this. JavaScript MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this)<br/>
|
||||
[this. Javascript W3Schools](https://www.w3schools.com/js/js_this.asp)
|
||||
[for loop MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for)
|
||||
[Array.prototype MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype)
|
||||
[Array.prototype MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype)
|
||||
</details>
|
@@ -1,47 +1,46 @@
|
||||
---
|
||||
title: Implement the filter Method on a Prototype
|
||||
---
|
||||
## Implement the filter Method on a Prototype
|
||||
# Implement the filter Method on a Prototype
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
|
||||
// the global Array
|
||||
var s = [23, 65, 98, 5];
|
||||
|
||||
Array.prototype.myFilter = function(callback){
|
||||
Array.prototype.myFilter = function(callback) {
|
||||
var newArray = [];
|
||||
// Add your code below this line
|
||||
this.forEach(function(x) {
|
||||
if (callback(x) == true) {
|
||||
newArray.push(x);
|
||||
}
|
||||
})
|
||||
});
|
||||
// Add your code above this line
|
||||
return newArray;
|
||||
|
||||
};
|
||||
|
||||
```
|
||||
</details>
|
||||
|
||||
## Another solution using for looop!
|
||||
|
||||
<details><summary>Solution 2 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
|
||||
Array.prototype.myFilter = function(callback){
|
||||
Array.prototype.myFilter = function(callback) {
|
||||
var newArray = [];
|
||||
// Add your code below this line
|
||||
for (let i=0; i<this.length;i++){
|
||||
if(callback(this[i])=== true ){
|
||||
newArray.push(this[i]);
|
||||
for (let i = 0; i < this.length; i++) {
|
||||
if (callback(this[i]) === true) {
|
||||
newArray.push(this[i]);
|
||||
}
|
||||
}
|
||||
// Add your code above this line
|
||||
return newArray;
|
||||
|
||||
};
|
||||
|
||||
var new_s = s.myFilter(function(item){
|
||||
var new_s = s.myFilter(function(item) {
|
||||
return item % 2 === 1;
|
||||
});
|
||||
|
||||
```
|
||||
</details>
|
||||
|
@@ -1,13 +1,11 @@
|
||||
---
|
||||
title: Functional Programming
|
||||
---
|
||||
## Functional Programming
|
||||
# Functional Programming
|
||||
|
||||
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/mathematics/quadratic-equations/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.
|
||||
|
||||
<a href='https://github.com/freecodecamp/guides/blob/master/README.md' target='_blank' rel='nofollow'>This quick style guide will help ensure your pull request gets accepted</a>.
|
||||
|
||||
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
|
||||
|
||||
#### More Information:
|
||||
<!-- Please add any articles you think might be helpful to read before writing the article -->
|
||||
|
@@ -1,21 +1,24 @@
|
||||
---
|
||||
title: Introduction to Currying and Partial Application
|
||||
---
|
||||
## Introduction to Currying and Partial Application
|
||||
# Introduction to Currying and Partial Application
|
||||
|
||||
### Solution
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
|
||||
function add(x) {
|
||||
// Add your code below this line
|
||||
return function(y) {
|
||||
return function(z) {
|
||||
return x + y + z;
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
// Add your code above this line
|
||||
}
|
||||
add(10)(20)(30);
|
||||
|
||||
```
|
||||
</details>
|
||||
|
@@ -1,35 +1,39 @@
|
||||
---
|
||||
title: Learn About Functional Programming
|
||||
---
|
||||
## Learn About Functional Programming
|
||||
# Learn About Functional Programming
|
||||
A function has an input or a parameter ``` const myFunc = (input) => { ...code to execute... } ```. In this case the input is how many cups of tea to be created.
|
||||
<br/>
|
||||
|
||||
### Method
|
||||
---
|
||||
## Problem Explanation
|
||||
|
||||
Only one line of code must be changed to pass this challenege. The `getTea()` function must be called and stored in the `tea4TeamFCC` variable. Make sure to read through the `getTea()` function and understand exactly what it does. The function takes in one variable - `numOfCups`. A `teaCups[]` array is first made and a for loop is set up to count down the number of cups passed into the function. A new cup of tea is then pushed to the array through every iteration of the for loop.
|
||||
|
||||
Thus resulting in an array full of the amount of teacups originally passed into the `getTea()` function.
|
||||
|
||||
### Solution
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
|
||||
/**
|
||||
* A long process to prepare tea.
|
||||
* @return {string} A cup of tea.
|
||||
**/
|
||||
const prepareTea = () => 'greenTea';
|
||||
const prepareTea = () => "greenTea";
|
||||
|
||||
/**
|
||||
* Get given number of cups of tea.
|
||||
* @param {number} numOfCups Number of required cups of tea.
|
||||
* @return {Array<string>} Given amount of tea cups.
|
||||
**/
|
||||
const getTea = (numOfCups) => {
|
||||
const getTea = numOfCups => {
|
||||
const teaCups = [];
|
||||
|
||||
for(let cups = 1; cups <= numOfCups; cups += 1) {
|
||||
|
||||
for (let cups = 1; cups <= numOfCups; cups += 1) {
|
||||
const teaCup = prepareTea();
|
||||
teaCups.push(teaCup);
|
||||
}
|
||||
@@ -44,4 +48,5 @@ const tea4TeamFCC = getTea(40); // :(
|
||||
// Add your code above this line
|
||||
|
||||
console.log(tea4TeamFCC);
|
||||
```
|
||||
```
|
||||
</details>
|
@@ -2,26 +2,31 @@
|
||||
title: Pass Arguments to Avoid External Dependence in a Function
|
||||
---
|
||||
|
||||
## Pass Arguments to Avoid External Dependence in a Function
|
||||
# Pass Arguments to Avoid External Dependence in a Function
|
||||
|
||||
## Hint: 1
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
|
||||
Try to pass argument to function and return increased value of this argument.
|
||||
|
||||
|
||||
**Solution ahead!**
|
||||
|
||||
## Basic Code Solution:
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
|
||||
// the global variable
|
||||
var fixedValue = 4;
|
||||
|
||||
// Add your code below this line
|
||||
function incrementer (value) {
|
||||
function incrementer(value) {
|
||||
return value + 1;
|
||||
|
||||
|
||||
// Add your code above this line
|
||||
}
|
||||
|
||||
@@ -29,7 +34,8 @@ var newValue = incrementer(fixedValue); // Should equal 5
|
||||
console.log(fixedValue); // Should print 4
|
||||
```
|
||||
|
||||
### Method
|
||||
#### Code Explanation
|
||||
|
||||
This code will provide the same result as the last challenge, only this time we will pass the `fixedValue` into the function as a parameter.
|
||||
</details>
|
||||
|
||||
|
@@ -1,41 +1,54 @@
|
||||
---
|
||||
title: Refactor Global Variables Out of Functions
|
||||
---
|
||||
## Refactor Global Variables Out of Functions
|
||||
# Refactor Global Variables Out of Functions
|
||||
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
- If you're having trouble with changing bookList, try using a copy of the array in your functions.
|
||||
|
||||
### Hint 2
|
||||
- Here's some more information about [how JavaScript handles function arguments](https://codeburst.io/javascript-passing-by-value-vs- reference-explained-in-plain-english-8d00fd06a47c).
|
||||
|
||||
##  Basic Code Solution:
|
||||
|
||||
## Solution 1
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
function add (arr, bookName) {
|
||||
let newArr = [...arr]; // Copy the bookList array to a new array.
|
||||
newArr.push(bookName); // Add bookName parameter to the end of the new array.
|
||||
function add(arr, bookName) {
|
||||
let newArr = [...arr]; // Copy the bookList array to a new array.
|
||||
newArr.push(bookName); // Add bookName parameter to the end of the new array.
|
||||
return newArr; // Return the new array.
|
||||
}
|
||||
|
||||
function remove (arr, bookName) {
|
||||
let newArr = [...arr]; // Copy the bookList array to a new array.
|
||||
if (newArr.indexOf(bookName) >= 0) { // Check whether the bookName parameter is in new array.
|
||||
/.
|
||||
function remove(arr, bookName) {
|
||||
let newArr = [...arr]; // Copy the bookList array to a new array.
|
||||
if (newArr.indexOf(bookName) >= 0) {
|
||||
// Check whether the bookName parameter is in new array.
|
||||
//.
|
||||
newArr.splice(newArr.indexOf(bookName), 1); // Remove the given paramater from the new array.
|
||||
return newArr; // Return the new array.
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
</details>
|
||||
|
||||
<details><summary>Solution 2 (Click to Show/Hide)</summary>
|
||||
|
||||
## Solution 2
|
||||
```javascript
|
||||
function add (list,bookName) {
|
||||
function add(list, bookName) {
|
||||
return [...list, bookName];
|
||||
}
|
||||
|
||||
function remove (list,bookName) {
|
||||
function remove(list, bookName) {
|
||||
if (list.indexOf(bookName) >= 0) {
|
||||
return list.filter((item) => item !== bookName);
|
||||
}
|
||||
return list.filter(item => item !== bookName);
|
||||
}
|
||||
}
|
||||
```
|
||||
</details>
|
||||
|
@@ -1,21 +1,31 @@
|
||||
---
|
||||
title: Remove Elements from an Array Using slice Instead of splice
|
||||
---
|
||||
## Remove Elements from an Array Using slice Instead of splice
|
||||
|
||||
# Remove Elements from an Array Using slice Instead of splice
|
||||
|
||||
---
|
||||
## Problem Explanation
|
||||
- The difference between splice and slice method is that the slice method does not mutate the original array, but returns a new one.
|
||||
- The slice method takes 2 two arguments for the indices to begin and end the slice (the end is non-inclusive).
|
||||
- If you do not want to mutate the original array, you can use the slice method.
|
||||
|
||||
## Solution
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
function nonMutatingSplice(cities) {
|
||||
// Add your code below this line
|
||||
|
||||
return cities.slice(0, 3);
|
||||
|
||||
|
||||
return cities.slice(0, 3);
|
||||
|
||||
// Add your code above this line
|
||||
}
|
||||
var inputCities = ["Chicago", "Delhi", "Islamabad", "London", "Berlin"];
|
||||
nonMutatingSplice(inputCities);
|
||||
```
|
||||
|
||||
</details>
|
@@ -1,16 +1,15 @@
|
||||
---
|
||||
title: Return a Sorted Array Without Changing the Original Array
|
||||
---
|
||||
## Return a Sorted Array Without Changing the Original Array
|
||||
# Return a Sorted Array Without Changing the Original Array
|
||||
|
||||
### Method
|
||||
|
||||
Firstly concatenate the array taken in as a parameter to a new empty array. Then Use the `sort()` method as seen in the last challenge and create a function to sort the new array in ascending order.
|
||||
---
|
||||
## Solutions
|
||||
|
||||
### Solution
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
|
||||
var globalArray = [5, 6, 3, 2, 9];
|
||||
function nonMutatingSort(arr) {
|
||||
// Add your code below this line
|
||||
@@ -20,5 +19,10 @@ function nonMutatingSort(arr) {
|
||||
// Add your code above this line
|
||||
}
|
||||
nonMutatingSort(globalArray);
|
||||
|
||||
```
|
||||
|
||||
#### Code Explanation
|
||||
|
||||
- Firstly concatenate the array taken in as a parameter to a new empty array.
|
||||
- Then Use the `sort()` method as seen in the last challenge and create a function to sort the new array in ascending order.
|
||||
</details>
|
@@ -1,25 +1,24 @@
|
||||
---
|
||||
title: Return Part of an Array Using the slice Method
|
||||
---
|
||||
## Return Part of an Array Using the slice Method
|
||||
# Return Part of an Array Using the slice Method
|
||||
|
||||
### Problem Explanation
|
||||
---
|
||||
## Problem Explanation
|
||||
|
||||
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.
|
||||
|
||||
### Method
|
||||
|
||||
The function can be written by simply writing one line of code - a return statement. Just like in the example given, slice the array which the function takes as a parameter using the `beginSlice` and `endSlice` parameters as parameters for the `slice()` method.
|
||||
Remember the structure of the `slice()` method:
|
||||
|
||||
```javascript
|
||||
|
||||
var arr = ["Cat", "Dog", "Tiger", "Zebra", "Ant"];
|
||||
arr.slice([index-to-begin-slice] , [index-to-end-slice]);
|
||||
|
||||
arr.slice([index - to - begin - slice], [index - to - end - slice]);
|
||||
```
|
||||
|
||||
### Solution
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
function sliceArray(anim, beginSlice, endSlice) {
|
||||
@@ -31,5 +30,11 @@ var inputAnim = ["Cat", "Dog", "Tiger", "Zebra", "Ant"];
|
||||
sliceArray(inputAnim, 1, 3);
|
||||
```
|
||||
|
||||
#### Relevant Links:
|
||||
#### Code Explanation
|
||||
|
||||
The function can be written by simply writing one line of code - a return statement. Just like in the example given, slice the array which the function takes as a parameter using the `beginSlice` and `endSlice` parameters as parameters for the `slice()` method.
|
||||
Remember the structure of the `slice()` method:
|
||||
|
||||
#### Relevant Links
|
||||
- [Array.prototype.slice()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice)
|
||||
</details>
|
||||
|
@@ -1,7 +1,11 @@
|
||||
---
|
||||
title: Sort an Array Alphabetically using the sort Method
|
||||
---
|
||||
## Sort an Array Alphabetically using the sort Method
|
||||
# Sort an Array Alphabetically using the sort Method
|
||||
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
Hint #1
|
||||
|
||||
@@ -12,29 +16,29 @@ For example, the following is how you would sort an array in reverse alphabetica
|
||||
```js
|
||||
function reverseAlphabeticalOrder(arr) {
|
||||
// Add your code below this line
|
||||
return arr.sort(function(a,b) {
|
||||
return arr.sort(function(a, b) {
|
||||
return a === b ? 0 : a < b ? 1 : -1;
|
||||
});
|
||||
// Add your code above this line
|
||||
}
|
||||
reverseAlphabeticalOrder(['l', 'h', 'z', 'b', 's']);
|
||||
reverseAlphabeticalOrder(["l", "h", "z", "b", "s"]);
|
||||
// Returns ['z', 's', 'l', 'h', 'b']
|
||||
```
|
||||
|
||||
### Solution #1
|
||||
|
||||
<details>
|
||||
<summary>Spoiler Alert - Only click here to see the solution</summary>
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```js
|
||||
function alphabeticalOrder(arr) {
|
||||
// Add your code below this line
|
||||
return arr.sort(function(a,b) {
|
||||
return arr.sort(function(a, b) {
|
||||
return a === b ? 0 : a < b ? -1 : 1;
|
||||
});
|
||||
// Add your code above this line
|
||||
}
|
||||
alphabeticalOrder(["a", "d", "c", "a", "z", "g"]);
|
||||
```
|
||||
|
||||
</details>
|
||||
</details>
|
@@ -1,15 +1,23 @@
|
||||
---
|
||||
title: Split a String into an Array Using the split Method
|
||||
---
|
||||
## Split a String into an Array Using the split Method
|
||||
### Method
|
||||
# Split a String into an Array Using the split Method
|
||||
|
||||
---
|
||||
## Problem Explanation
|
||||
|
||||
Simply split the string to create a new array of words.
|
||||
|
||||
A simple regular expression can be used to achieve this result.
|
||||
|
||||
`/\W/` Matches any non-word character. This includes spaces and punctuation, but not underscores. It's equivalent to `/[^A-Za-z0-9_]/`. For more information about Regular Expressions, see the official [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions).
|
||||
|
||||
### Solution
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
function splitify(str) {
|
||||
// Add your code below this line
|
||||
@@ -18,3 +26,4 @@ function splitify(str) {
|
||||
}
|
||||
splitify("Hello World,I-am code");
|
||||
```
|
||||
</details>
|
||||
|
@@ -1,13 +1,18 @@
|
||||
---
|
||||
title: Understand Functional Programming Terminology
|
||||
---
|
||||
## Understand Functional Programming Terminology
|
||||
# Understand Functional Programming Terminology
|
||||
|
||||
### Method
|
||||
---
|
||||
## Problem Explanation
|
||||
|
||||
Just as in the last challenge, you must call the `getTea` method and store it in a variable. Only this time, you have 2 variables to store 2 seperate sets of data in. You will see that the `getTea()` function is the same as before, only now it takes in 2 seperate parameters. The first parameter is a function, so we will need to pass in either the `prepareGreenTea()` function or the `prepareBlackTea()` function, followed by the second parameter `numOfCups` which can be input as an integer.
|
||||
|
||||
### Solution
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
|
||||
In this exercise we are assigning the result of a higher order function to variables. To do this we call a function with a callback function as a parameter.
|
||||
|
||||
@@ -16,15 +21,18 @@ In this exercise we are assigning the result of a higher order function to varia
|
||||
const basketOne = makeBasket(addFruit, 10)
|
||||
```
|
||||
|
||||
## Solution:
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
```javascript
|
||||
|
||||
/**
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
/**
|
||||
* A long process to prepare green tea.
|
||||
* @return {string} A cup of green tea.
|
||||
**/
|
||||
const prepareGreenTea = () => 'greenTea';
|
||||
const prepareGreenTea = () => "greenTea";
|
||||
|
||||
/**
|
||||
* Get given number of cups of tea.
|
||||
@@ -35,7 +43,7 @@ const prepareGreenTea = () => 'greenTea';
|
||||
const getTea = (prepareTea, numOfCups) => {
|
||||
const teaCups = [];
|
||||
|
||||
for(let cups = 1; cups <= numOfCups; cups += 1) {
|
||||
for (let cups = 1; cups <= numOfCups; cups += 1) {
|
||||
const teaCup = prepareTea();
|
||||
teaCups.push(teaCup);
|
||||
}
|
||||
@@ -43,19 +51,17 @@ const getTea = (prepareTea, numOfCups) => {
|
||||
return teaCups;
|
||||
};
|
||||
|
||||
// Add your code below this line
|
||||
const tea4GreenTeamFCC = getTea(prepareGreenTea,27); // :)
|
||||
const tea4BlackTeamFCC = getTea(prepareBlackTea,13); // :)
|
||||
// Add your code above this line
|
||||
|
||||
console.log(
|
||||
tea4GreenTeamFCC,
|
||||
tea4BlackTeamFCC
|
||||
);
|
||||
|
||||
```
|
||||
## Code explanation:
|
||||
// Add your code below this line
|
||||
const tea4GreenTeamFCC = getTea(prepareGreenTea, 27); // :)
|
||||
const tea4BlackTeamFCC = getTea(prepareBlackTea, 13); // :)
|
||||
// Add your code above this line
|
||||
|
||||
console.log(tea4GreenTeamFCC, tea4BlackTeamFCC);
|
||||
```
|
||||
#### Code Explanation
|
||||
|
||||
In the solution above we passed in the functions ``` prepareGreenTea() ``` and ``` prepareBlackTea() ``` as parameters or callback functions for the ``` getTea() ``` functions being assigned to our two constant variables ``` tea4BlackTeamFCC ```
|
||||
and ``` tea4GreenTeamFCC ```.
|
||||
This way no global variables are changed, and we have the option to add an unlimited number of different choices of ``` prepareTea() ``` methods since it is a callback function being passed to the higher order function of ``` getTea() ```.
|
||||
|
||||
</details>
|
@@ -1,15 +1,11 @@
|
||||
---
|
||||
title: Understand the Hazards of Using Imperative Code
|
||||
---
|
||||
## Understand the Hazards of Using Imperative Code
|
||||
# Understand the Hazards of Using Imperative Code
|
||||
|
||||
### Disclaimer
|
||||
|
||||
In this text, there will two possible solutions for this challenge. First part is code analysis, so if you want to skip to the solutions, please scroll to bottom.
|
||||
|
||||
____
|
||||
|
||||
Note: If you run the tests, they will pass even though you didn't make a necessary change.
|
||||
---
|
||||
## Problem Explanation
|
||||
|
||||
What you should notice is the fact that output is not as suggested in instructions, which should be the following array:
|
||||
**['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium', 'new tab', 'Netflix', 'YouTube', 'Vine', 'GMail', 'Work mail', 'Docs', 'freeCodeCamp', 'new tab']**.
|
||||
@@ -17,29 +13,29 @@ What you should notice is the fact that output is not as suggested in instructio
|
||||
Instead, you will recieve this array:
|
||||
**['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium', 'new tab', 'Netflix', 'YouTube', 'GMail', 'Work mail', 'Docs', 'freeCodeCamp', 'new tab']**.
|
||||
|
||||
### Code analysis
|
||||
|
||||
Take a look at the last part of code to make a conclusion where is the issue.
|
||||
|
||||
#### Part 1
|
||||
**Part 1**
|
||||
```
|
||||
var finalTabs = socialWindow
|
||||
```
|
||||
|
||||
After this part of the code is executed, our array is **['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium']**
|
||||
|
||||
#### Part 2
|
||||
**Part 2**
|
||||
```
|
||||
.tabOpen() // Open a new tab for cat memes
|
||||
```
|
||||
```
|
||||
|
||||
#### Part 3
|
||||
**Part 3**
|
||||
After adding a 'new tab' to the array, our array is now **['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium' , 'new tab']**
|
||||
```
|
||||
.join(videoWindow.tabClose(2)) // Close third tab in video window, and join
|
||||
```
|
||||
This part of the code should supposedly close the third window (index 2 as the count starts from 0) and return video window without the third window which is 'Vimeo' in this case. So, returned array should look like **['Netflix', 'YouTube', 'Vine']** and after adding it to the main array, our array should be **['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium' , 'new tab', 'Netflix', 'YouTube', 'Vine']**
|
||||
|
||||
#### Part 4
|
||||
**Part 4**
|
||||
```
|
||||
.join(workWindow.tabClose(1).tabOpen());
|
||||
```
|
||||
@@ -47,7 +43,6 @@ This part would close second tab (index 1) in the workWindow **['GMail', 'Inbox'
|
||||
**['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium', 'new tab', 'Netflix', 'YouTube', 'Vine', 'GMail', 'Work mail', 'Docs', 'freeCodeCamp', 'new tab']**, as mentioned in the instructions.
|
||||
|
||||
|
||||
|
||||
If we compare the requested array and the one we recieved after running the initial code, we can see that value 'Vine' is omitted. Therefore, we need to see what is the cause of this. We can see that value 'Vine' is a value of an array **videoWindow**.
|
||||
Now that we know this, we will check the operations done on that array. This is done in Part 3:
|
||||
```
|
||||
@@ -63,7 +58,7 @@ tabClose = function (index) {
|
||||
3. this.tabs = tabsBeforeIndex.concat(tabsAfterIndex); // join them together
|
||||
4. return this;
|
||||
};
|
||||
```
|
||||
```
|
||||
For an index, we will take 2 - as done in the challenge.
|
||||
1. At the beginning, our array **videoWindow** looks like this:
|
||||
**['Netflix', 'YouTube', 'Vimeo', 'Vine']**.
|
||||
@@ -81,41 +76,50 @@ tabClose = function (index) {
|
||||
|
||||
After the third line and concatenation the returned array is the same as tabsBeforeIndex, which results in both 'Vimeo' and 'Vine' values not being in the array.
|
||||
|
||||
### Solution 1, using splice(). This creates side effects(changes to the original array) and should be avoided in practice.
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
using splice(). This creates side effects(changes to the original array) and should be avoided in practice.
|
||||
|
||||
In order for the method tabClose to work properly,
|
||||
```
|
||||
```
|
||||
var tabsAfterIndex = this.tabs.splice(index);
|
||||
```
|
||||
```
|
||||
should be replaced with
|
||||
```
|
||||
```
|
||||
var tabsAfterIndex = this.tabs.splice(1);
|
||||
```
|
||||
```
|
||||
|
||||
This way, after second line is executed on the current array **['Vimeo', 'Vine']**, it will always omit the first value (index 0) and the one with index 1 until the end, resulting in the proper array returned.
|
||||
</details>
|
||||
|
||||
|
||||
### Solution 2,using slice(). This does not create side effects and should be preferred over splice().
|
||||
<details><summary>Solution 2 (Click to Show/Hide)</summary>
|
||||
|
||||
using slice(). This does not create side effects and should be preferred over splice().
|
||||
|
||||
This part of the code:
|
||||
```
|
||||
```
|
||||
var tabsBeforeIndex = this.tabs.splice(0, index); // get the tabs before the tab
|
||||
var tabsAfterIndex = this.tabs.splice(index); // get the tabs after the tab
|
||||
```
|
||||
```
|
||||
|
||||
should be replaced with:
|
||||
|
||||
|
||||
```
|
||||
```
|
||||
var tabsBeforeIndex = this.tabs.slice(0, index); // get the tabs before the tab
|
||||
var tabsAfterIndex = this.tabs.slice(index+1);
|
||||
```
|
||||
### Word of caution
|
||||
|
||||
splice() should be always used carefully as it modifies the contents it is working on. For documentation and differences between splice and slice please take a look at:
|
||||
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
|
||||
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
|
||||
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
|
||||
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
|
||||
|
||||
</details>
|
||||
|
||||
|
||||
|
||||
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
|
||||
|
@@ -1,50 +1,52 @@
|
||||
---
|
||||
title: Use Higher-Order Functions map, filter, or reduce to Solve a Complex Problem
|
||||
---
|
||||
 Remember to use <a>**`Read-Search-Ask`**</a> if you get stuck. Try to pair program  and write your own code 
|
||||
|
||||
### Problem explanation:
|
||||
# Use Higher-Order Functions map, filter, or reduce to Solve a Complex Problem
|
||||
|
||||
---
|
||||
## Problem Explanation
|
||||
|
||||
We need to compute and square values from the `realNumberArray` and store them in the variable `squaredIntegers` using the `map()`, `filter()`, and/or `reduce()` functions.
|
||||
|
||||
##  Hint: 1
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
|
||||
* You will need to `filter()` the `realNumberArray` for positive integers (decimals are not integers).
|
||||
|
||||
> _try to solve the problem now_
|
||||
|
||||
##  Hint: 2
|
||||
### Hint 2
|
||||
|
||||
* You will need to `map()` the values from your `filter()` function to the variable `squaredIntegers`.
|
||||
|
||||
> _try to solve the problem now_
|
||||
|
||||
##  Hint: 3
|
||||
### Hint 3
|
||||
|
||||
* Remember the magic of chaining functions.
|
||||
|
||||
> _try to solve the problem now_
|
||||
|
||||
## Spoiler alert!
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||

|
||||
|
||||
**Solution ahead!**
|
||||
|
||||
##  Code solution:
|
||||
```js
|
||||
const squareList = (arr) => arr
|
||||
.filter((num) => num > 0 && num % parseInt(num) === 0)
|
||||
.map((num) => Math.pow(num, 2));
|
||||
const squareList = arr =>
|
||||
arr
|
||||
.filter(num => num > 0 && num % parseInt(num) === 0)
|
||||
.map(num => Math.pow(num, 2));
|
||||
```
|
||||
|
||||
### Code explanation:
|
||||
#### Code Explanation
|
||||
|
||||
Uses the operator `filter()` and `map()` functions to square all positive integers in a given array.
|
||||
</details>
|
||||
|
||||
<details><summary>Solution 2 (Click to Show/Hide)</summary>
|
||||
|
||||
|
||||
## Alternative code solution:
|
||||
```js
|
||||
const squareList = (arr) => {
|
||||
const squareList = arr => {
|
||||
return arr.reduce((sqrIntegers, num) => {
|
||||
return Number.isInteger(num) && num > 0
|
||||
? sqrIntegers.concat(num * num)
|
||||
@@ -53,14 +55,15 @@ const squareList = (arr) => {
|
||||
};
|
||||
```
|
||||
|
||||
### Code explanation
|
||||
#### Code Explanation
|
||||
This does basically the same but uses the `isInteger()` method to check the numbers.
|
||||
|
||||
|
||||
### Resources
|
||||
#### Relevant Links
|
||||
|
||||
- ["Array.prototype.map()" - *MDN JavaScript reference*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)
|
||||
- ["Array.prototype.filter()" - *MDN JavaScript reference*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)
|
||||
- ["Array.prototype.reduce()" - *MDN JavaScript reference*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce)
|
||||
- ["Number.isInteger()" - *MDN JavaScript reference*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger)
|
||||
- ["Math.pow()" - *MDN JavaScript reference*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/pow)
|
||||
</details>
|
||||
|
@@ -1,44 +1,49 @@
|
||||
---
|
||||
title: Use the every Method to Check that Every Element in an Array Meets a Criteria
|
||||
---
|
||||
## Use the every Method to Check that Every Element in an Array Meets a Criteria
|
||||
# Use the every Method to Check that Every Element in an Array Meets a Criteria
|
||||
|
||||
<!-- This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/javascript-algorithms-and-data-structures/functional-programming/use-the-every-method-to-check-that-every-element-in-an-array-meets-a-criteria/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>. -->
|
||||
|
||||
<!-- <a href='https://github.com/freecodecamp/guides/blob/master/README.md' target='_blank' rel='nofollow'>This quick style guide will help ensure your pull request gets accepted</a>. -->
|
||||
|
||||
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
|
||||
### Problem Explanation:
|
||||
---
|
||||
## Problem Explanation
|
||||
Use the `every` method inside the `checkPositive` function to check if every element in `arr` is positive. The function should return a Boolean value.
|
||||
|
||||
#### Relevant Links:
|
||||
#### Relevant Links
|
||||
- [Array.prototype.every()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every)
|
||||
|
||||
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint
|
||||
Don't forget `return`.
|
||||
|
||||
## Solution
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
function checkPositive(arr) {
|
||||
// Add your code below this line
|
||||
|
||||
|
||||
return arr.every(val => val > 0);
|
||||
// Add your code above this line
|
||||
}
|
||||
checkPositive([1, 2, 3, -4, 5]);
|
||||
```
|
||||
</details>
|
||||
|
||||
## Alternative Solution
|
||||
<details><summary>Solution 2 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
|
||||
function checkPositive(arr) {
|
||||
// Add your code below this line
|
||||
return arr.every(function(value) {
|
||||
return value > 0;
|
||||
});
|
||||
return arr.every(function(value) {
|
||||
return value > 0;
|
||||
});
|
||||
// Add your code above this line
|
||||
}
|
||||
checkPositive([1, 2, 3, -4, 5]);
|
||||
|
||||
```
|
||||
</details>
|
||||
|
@@ -1,44 +1,57 @@
|
||||
---
|
||||
title: Use the filter method to extract data from an array
|
||||
---
|
||||
## Use the filter method to extract data from an array
|
||||
# Use the filter method to extract data from an array
|
||||
|
||||
##  Hint: 1
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
This challange is solved in 2 steps.
|
||||
First, Array.prototype.filter is used to filter the array so it's left with elements that have imdbRating > 8.0.
|
||||
After that, Array.prototype.map can be used to shape the output to the desired format.
|
||||
|
||||
### Beginner Solution
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
|
||||
// Add your code below this line
|
||||
|
||||
var filteredList = watchList.map((movie) => {
|
||||
return {
|
||||
title: movie.Title,
|
||||
rating: movie.imdbRating
|
||||
}
|
||||
}).filter((movie) => {
|
||||
// return true it will keep the item
|
||||
// return false it will reject the item
|
||||
return parseFloat(movie.rating) >= 8.0
|
||||
});
|
||||
var filteredList = watchList
|
||||
.map(movie => {
|
||||
return {
|
||||
title: movie.Title,
|
||||
rating: movie.imdbRating
|
||||
};
|
||||
})
|
||||
.filter(movie => {
|
||||
// return true it will keep the item
|
||||
// return false it will reject the item
|
||||
return parseFloat(movie.rating) >= 8.0;
|
||||
});
|
||||
```
|
||||
|
||||
#### Code Explanation
|
||||
In the beginnner solution we're mapping over the watchList array to reduce the amount of data we have to work with and only returning the two items we need. Once we've reduced the items to what we're interested in (Title and imdbRating) we're filtering through and only returning the remaining items that meet the criteria. In this case it's having an imdbRating of 8.0 or higher.
|
||||
</details>
|
||||
|
||||
|
||||
### Intermediate Solution
|
||||
<details><summary>Solution 2 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
|
||||
// Add your code below this line
|
||||
|
||||
var filteredList = watchList.map(function(e) {
|
||||
return {title: e["Title"], rating: e["imdbRating"]}
|
||||
}).filter((e) => e.rating >= 8);
|
||||
|
||||
console.log(filteredList);
|
||||
var filteredList = watchList
|
||||
.map(function(e) {
|
||||
return { title: e["Title"], rating: e["imdbRating"] };
|
||||
})
|
||||
.filter(e => e.rating >= 8);
|
||||
|
||||
console.log(filteredList);
|
||||
```
|
||||
|
||||
</details>
|
@@ -1,25 +1,32 @@
|
||||
---
|
||||
title: Use the map Method to Extract Data from an Array
|
||||
---
|
||||
## Use the map Method to Extract Data from an Array
|
||||
# Use the map Method to Extract Data from an Array
|
||||
|
||||
##  Hint: 1
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
array.prototype.map takes a function as in input and returns an array. The returned array includes elements that is processed by the function. This function takes individual elements as input.
|
||||
|
||||
## Spoiler Alert!
|
||||
|
||||

|
||||
---
|
||||
## Solutions
|
||||
|
||||
**Solution ahead!**
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
##  Intermediate Code Solution:
|
||||
```javascript
|
||||
const rating = watchList.map(item => ({title: item["Title"], rating: item["imdbRating"]}));
|
||||
const rating = watchList.map(item => ({
|
||||
title: item["Title"],
|
||||
rating: item["imdbRating"]
|
||||
}));
|
||||
```
|
||||
### Code Explanation:
|
||||
#### Code Explanation
|
||||
Using ES6 notation, each item in array is processed to extract title and rating.
|
||||
Parantheses are needed to return an object.
|
||||
|
||||
#### Relevant Links
|
||||
|
||||
* <a href='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions' target='_blank' rel='nofollow'>Arrow Functions</a>
|
||||
</details>
|
||||
|
@@ -1,137 +1,156 @@
|
||||
---
|
||||
title: Use the reduce Method to Analyze Data
|
||||
---
|
||||
## Use the reduce Method to Analyze Data
|
||||
# Use the reduce Method to Analyze Data
|
||||
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
|
||||
// the global variable
|
||||
var watchList = [
|
||||
{
|
||||
"Title": "Inception",
|
||||
"Year": "2010",
|
||||
"Rated": "PG-13",
|
||||
"Released": "16 Jul 2010",
|
||||
"Runtime": "148 min",
|
||||
"Genre": "Action, Adventure, Crime",
|
||||
"Director": "Christopher Nolan",
|
||||
"Writer": "Christopher Nolan",
|
||||
"Actors": "Leonardo DiCaprio, Joseph Gordon-Levitt, Ellen Page, Tom Hardy",
|
||||
"Plot": "A thief, who steals corporate secrets through use of dream-sharing technology, is given the inverse task of planting an idea into the mind of a CEO.",
|
||||
"Language": "English, Japanese, French",
|
||||
"Country": "USA, UK",
|
||||
"Awards": "Won 4 Oscars. Another 143 wins & 198 nominations.",
|
||||
"Poster": "http://ia.media-imdb.com/images/M/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw@@._V1_SX300.jpg",
|
||||
"Metascore": "74",
|
||||
"imdbRating": "8.8",
|
||||
"imdbVotes": "1,446,708",
|
||||
"imdbID": "tt1375666",
|
||||
"Type": "movie",
|
||||
"Response": "True"
|
||||
},
|
||||
{
|
||||
"Title": "Interstellar",
|
||||
"Year": "2014",
|
||||
"Rated": "PG-13",
|
||||
"Released": "07 Nov 2014",
|
||||
"Runtime": "169 min",
|
||||
"Genre": "Adventure, Drama, Sci-Fi",
|
||||
"Director": "Christopher Nolan",
|
||||
"Writer": "Jonathan Nolan, Christopher Nolan",
|
||||
"Actors": "Ellen Burstyn, Matthew McConaughey, Mackenzie Foy, John Lithgow",
|
||||
"Plot": "A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival.",
|
||||
"Language": "English",
|
||||
"Country": "USA, UK",
|
||||
"Awards": "Won 1 Oscar. Another 39 wins & 132 nominations.",
|
||||
"Poster": "http://ia.media-imdb.com/images/M/MV5BMjIxNTU4MzY4MF5BMl5BanBnXkFtZTgwMzM4ODI3MjE@._V1_SX300.jpg",
|
||||
"Metascore": "74",
|
||||
"imdbRating": "8.6",
|
||||
"imdbVotes": "910,366",
|
||||
"imdbID": "tt0816692",
|
||||
"Type": "movie",
|
||||
"Response": "True"
|
||||
},
|
||||
{
|
||||
"Title": "The Dark Knight",
|
||||
"Year": "2008",
|
||||
"Rated": "PG-13",
|
||||
"Released": "18 Jul 2008",
|
||||
"Runtime": "152 min",
|
||||
"Genre": "Action, Adventure, Crime",
|
||||
"Director": "Christopher Nolan",
|
||||
"Writer": "Jonathan Nolan (screenplay), Christopher Nolan (screenplay), Christopher Nolan (story), David S. Goyer (story), Bob Kane (characters)",
|
||||
"Actors": "Christian Bale, Heath Ledger, Aaron Eckhart, Michael Caine",
|
||||
"Plot": "When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, the caped crusader must come to terms with one of the greatest psychological tests of his ability to fight injustice.",
|
||||
"Language": "English, Mandarin",
|
||||
"Country": "USA, UK",
|
||||
"Awards": "Won 2 Oscars. Another 146 wins & 142 nominations.",
|
||||
"Poster": "http://ia.media-imdb.com/images/M/MV5BMTMxNTMwODM0NF5BMl5BanBnXkFtZTcwODAyMTk2Mw@@._V1_SX300.jpg",
|
||||
"Metascore": "82",
|
||||
"imdbRating": "9.0",
|
||||
"imdbVotes": "1,652,832",
|
||||
"imdbID": "tt0468569",
|
||||
"Type": "movie",
|
||||
"Response": "True"
|
||||
},
|
||||
{
|
||||
"Title": "Batman Begins",
|
||||
"Year": "2005",
|
||||
"Rated": "PG-13",
|
||||
"Released": "15 Jun 2005",
|
||||
"Runtime": "140 min",
|
||||
"Genre": "Action, Adventure",
|
||||
"Director": "Christopher Nolan",
|
||||
"Writer": "Bob Kane (characters), David S. Goyer (story), Christopher Nolan (screenplay), David S. Goyer (screenplay)",
|
||||
"Actors": "Christian Bale, Michael Caine, Liam Neeson, Katie Holmes",
|
||||
"Plot": "After training with his mentor, Batman begins his fight to free crime-ridden Gotham City from the corruption that Scarecrow and the League of Shadows have cast upon it.",
|
||||
"Language": "English, Urdu, Mandarin",
|
||||
"Country": "USA, UK",
|
||||
"Awards": "Nominated for 1 Oscar. Another 15 wins & 66 nominations.",
|
||||
"Poster": "http://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3._V1_SX300.jpg",
|
||||
"Metascore": "70",
|
||||
"imdbRating": "8.3",
|
||||
"imdbVotes": "972,584",
|
||||
"imdbID": "tt0372784",
|
||||
"Type": "movie",
|
||||
"Response": "True"
|
||||
},
|
||||
{
|
||||
"Title": "Avatar",
|
||||
"Year": "2009",
|
||||
"Rated": "PG-13",
|
||||
"Released": "18 Dec 2009",
|
||||
"Runtime": "162 min",
|
||||
"Genre": "Action, Adventure, Fantasy",
|
||||
"Director": "James Cameron",
|
||||
"Writer": "James Cameron",
|
||||
"Actors": "Sam Worthington, Zoe Saldana, Sigourney Weaver, Stephen Lang",
|
||||
"Plot": "A paraplegic marine dispatched to the moon Pandora on a unique mission becomes torn between following his orders and protecting the world he feels is his home.",
|
||||
"Language": "English, Spanish",
|
||||
"Country": "USA, UK",
|
||||
"Awards": "Won 3 Oscars. Another 80 wins & 121 nominations.",
|
||||
"Poster": "http://ia.media-imdb.com/images/M/MV5BMTYwOTEwNjAzMl5BMl5BanBnXkFtZTcwODc5MTUwMw@@._V1_SX300.jpg",
|
||||
"Metascore": "83",
|
||||
"imdbRating": "7.9",
|
||||
"imdbVotes": "876,575",
|
||||
"imdbID": "tt0499549",
|
||||
"Type": "movie",
|
||||
"Response": "True"
|
||||
}
|
||||
{
|
||||
Title: "Inception",
|
||||
Year: "2010",
|
||||
Rated: "PG-13",
|
||||
Released: "16 Jul 2010",
|
||||
Runtime: "148 min",
|
||||
Genre: "Action, Adventure, Crime",
|
||||
Director: "Christopher Nolan",
|
||||
Writer: "Christopher Nolan",
|
||||
Actors: "Leonardo DiCaprio, Joseph Gordon-Levitt, Ellen Page, Tom Hardy",
|
||||
Plot:
|
||||
"A thief, who steals corporate secrets through use of dream-sharing technology, is given the inverse task of planting an idea into the mind of a CEO.",
|
||||
Language: "English, Japanese, French",
|
||||
Country: "USA, UK",
|
||||
Awards: "Won 4 Oscars. Another 143 wins & 198 nominations.",
|
||||
Poster:
|
||||
"http://ia.media-imdb.com/images/M/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw@@._V1_SX300.jpg",
|
||||
Metascore: "74",
|
||||
imdbRating: "8.8",
|
||||
imdbVotes: "1,446,708",
|
||||
imdbID: "tt1375666",
|
||||
Type: "movie",
|
||||
Response: "True"
|
||||
},
|
||||
{
|
||||
Title: "Interstellar",
|
||||
Year: "2014",
|
||||
Rated: "PG-13",
|
||||
Released: "07 Nov 2014",
|
||||
Runtime: "169 min",
|
||||
Genre: "Adventure, Drama, Sci-Fi",
|
||||
Director: "Christopher Nolan",
|
||||
Writer: "Jonathan Nolan, Christopher Nolan",
|
||||
Actors: "Ellen Burstyn, Matthew McConaughey, Mackenzie Foy, John Lithgow",
|
||||
Plot:
|
||||
"A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival.",
|
||||
Language: "English",
|
||||
Country: "USA, UK",
|
||||
Awards: "Won 1 Oscar. Another 39 wins & 132 nominations.",
|
||||
Poster:
|
||||
"http://ia.media-imdb.com/images/M/MV5BMjIxNTU4MzY4MF5BMl5BanBnXkFtZTgwMzM4ODI3MjE@._V1_SX300.jpg",
|
||||
Metascore: "74",
|
||||
imdbRating: "8.6",
|
||||
imdbVotes: "910,366",
|
||||
imdbID: "tt0816692",
|
||||
Type: "movie",
|
||||
Response: "True"
|
||||
},
|
||||
{
|
||||
Title: "The Dark Knight",
|
||||
Year: "2008",
|
||||
Rated: "PG-13",
|
||||
Released: "18 Jul 2008",
|
||||
Runtime: "152 min",
|
||||
Genre: "Action, Adventure, Crime",
|
||||
Director: "Christopher Nolan",
|
||||
Writer:
|
||||
"Jonathan Nolan (screenplay), Christopher Nolan (screenplay), Christopher Nolan (story), David S. Goyer (story), Bob Kane (characters)",
|
||||
Actors: "Christian Bale, Heath Ledger, Aaron Eckhart, Michael Caine",
|
||||
Plot:
|
||||
"When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, the caped crusader must come to terms with one of the greatest psychological tests of his ability to fight injustice.",
|
||||
Language: "English, Mandarin",
|
||||
Country: "USA, UK",
|
||||
Awards: "Won 2 Oscars. Another 146 wins & 142 nominations.",
|
||||
Poster:
|
||||
"http://ia.media-imdb.com/images/M/MV5BMTMxNTMwODM0NF5BMl5BanBnXkFtZTcwODAyMTk2Mw@@._V1_SX300.jpg",
|
||||
Metascore: "82",
|
||||
imdbRating: "9.0",
|
||||
imdbVotes: "1,652,832",
|
||||
imdbID: "tt0468569",
|
||||
Type: "movie",
|
||||
Response: "True"
|
||||
},
|
||||
{
|
||||
Title: "Batman Begins",
|
||||
Year: "2005",
|
||||
Rated: "PG-13",
|
||||
Released: "15 Jun 2005",
|
||||
Runtime: "140 min",
|
||||
Genre: "Action, Adventure",
|
||||
Director: "Christopher Nolan",
|
||||
Writer:
|
||||
"Bob Kane (characters), David S. Goyer (story), Christopher Nolan (screenplay), David S. Goyer (screenplay)",
|
||||
Actors: "Christian Bale, Michael Caine, Liam Neeson, Katie Holmes",
|
||||
Plot:
|
||||
"After training with his mentor, Batman begins his fight to free crime-ridden Gotham City from the corruption that Scarecrow and the League of Shadows have cast upon it.",
|
||||
Language: "English, Urdu, Mandarin",
|
||||
Country: "USA, UK",
|
||||
Awards: "Nominated for 1 Oscar. Another 15 wins & 66 nominations.",
|
||||
Poster:
|
||||
"http://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3._V1_SX300.jpg",
|
||||
Metascore: "70",
|
||||
imdbRating: "8.3",
|
||||
imdbVotes: "972,584",
|
||||
imdbID: "tt0372784",
|
||||
Type: "movie",
|
||||
Response: "True"
|
||||
},
|
||||
{
|
||||
Title: "Avatar",
|
||||
Year: "2009",
|
||||
Rated: "PG-13",
|
||||
Released: "18 Dec 2009",
|
||||
Runtime: "162 min",
|
||||
Genre: "Action, Adventure, Fantasy",
|
||||
Director: "James Cameron",
|
||||
Writer: "James Cameron",
|
||||
Actors: "Sam Worthington, Zoe Saldana, Sigourney Weaver, Stephen Lang",
|
||||
Plot:
|
||||
"A paraplegic marine dispatched to the moon Pandora on a unique mission becomes torn between following his orders and protecting the world he feels is his home.",
|
||||
Language: "English, Spanish",
|
||||
Country: "USA, UK",
|
||||
Awards: "Won 3 Oscars. Another 80 wins & 121 nominations.",
|
||||
Poster:
|
||||
"http://ia.media-imdb.com/images/M/MV5BMTYwOTEwNjAzMl5BMl5BanBnXkFtZTcwODc5MTUwMw@@._V1_SX300.jpg",
|
||||
Metascore: "83",
|
||||
imdbRating: "7.9",
|
||||
imdbVotes: "876,575",
|
||||
imdbID: "tt0499549",
|
||||
Type: "movie",
|
||||
Response: "True"
|
||||
}
|
||||
];
|
||||
|
||||
// Add your code below this line
|
||||
var averageRating = watchList
|
||||
// Use filter to find films directed by Christopher Nolan
|
||||
.filter(film => film.Director === "Christopher Nolan")
|
||||
// Use map to convert their ratings from strings to numbers
|
||||
.map(film => Number(film.imdbRating))
|
||||
// Use reduce to add together their ratings
|
||||
.reduce((sumOfRatings, rating) => sumOfRatings + rating)
|
||||
// Divide by the number of Nolan films to get the average rating
|
||||
/ watchList.filter(film => film.Director === "Christopher Nolan").length;
|
||||
var averageRating =
|
||||
watchList
|
||||
// Use filter to find films directed by Christopher Nolan
|
||||
.filter(film => film.Director === "Christopher Nolan")
|
||||
// Use map to convert their ratings from strings to numbers
|
||||
.map(film => Number(film.imdbRating))
|
||||
// Use reduce to add together their ratings
|
||||
.reduce((sumOfRatings, rating) => sumOfRatings + rating) /
|
||||
// Divide by the number of Nolan films to get the average rating
|
||||
watchList.filter(film => film.Director === "Christopher Nolan").length;
|
||||
|
||||
// Add your code above this line
|
||||
|
||||
console.log(averageRating);
|
||||
|
||||
```
|
||||
</details>
|
||||
|
@@ -1,20 +1,27 @@
|
||||
---
|
||||
title: Use the some Method to Check that Any Elements in an Array Meet a Criteria
|
||||
---
|
||||
## Use the some Method to Check that Any Elements in an Array Meet a Criteria
|
||||
# Use the some Method to Check that Any Elements in an Array Meet a Criteria
|
||||
|
||||
### Problem Explanation
|
||||
---
|
||||
## Problem Explanation
|
||||
|
||||
Use the some method inside the checkPositive function to check if any element in arr is positive. The `checkPositive` function should return a Boolean value.
|
||||
|
||||
#### Relevant Links:
|
||||
#### Relevant Links
|
||||
- [Array.prototype.some()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some)
|
||||
|
||||
|
||||
### Solution:
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
function checkPositive(arr) {
|
||||
return arr.some((elem) => elem > 0);
|
||||
return arr.some(elem => elem > 0);
|
||||
}
|
||||
checkPositive([1, 2, 3, -4, 5]);
|
||||
```
|
||||
</details>
|
||||
|
Reference in New Issue
Block a user