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:
@@ -1,11 +1,21 @@
|
||||
---
|
||||
title: Access an Array's Contents Using Bracket Notation
|
||||
---
|
||||
## Access an Array's Contents Using Bracket Notation
|
||||
# Access an Array's Contents Using Bracket Notation
|
||||
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
- Remember the arrays index begins at 0 so the postion of b will be located in `myArray[1]`.
|
||||
|
||||
## Solution
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
```javascript
|
||||
myArray[1] = "anything we want";
|
||||
```
|
||||
</details>
|
||||
|
@@ -1,15 +1,15 @@
|
||||
---
|
||||
title: Access Property Names with Bracket Notation
|
||||
---
|
||||
## Access Property Names with Bracket Notation
|
||||
# Access Property Names with Bracket Notation
|
||||
|
||||
Method:
|
||||
---
|
||||
## Problem Explanation
|
||||
- Using bracket notation simply write the return statement in the `checkInventory()` function.
|
||||
- The following code block demonstrates the required syntax.
|
||||
|
||||
## Example:
|
||||
```javascript
|
||||
|
||||
let juice = {
|
||||
apple: 1.15,
|
||||
orange: 1.45
|
||||
@@ -17,11 +17,14 @@ let juice = {
|
||||
function checkInventory(scannedItem) {
|
||||
return juice[scannedItem];
|
||||
}
|
||||
|
||||
```
|
||||
## Solution:
|
||||
```javascript
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
let foods = {
|
||||
apples: 25,
|
||||
oranges: 32,
|
||||
@@ -39,5 +42,6 @@ function checkInventory(scannedItem) {
|
||||
|
||||
// change code below this line to test different cases:
|
||||
console.log(checkInventory("apples"));
|
||||
|
||||
```
|
||||
|
||||
</details>
|
@@ -1,20 +1,31 @@
|
||||
---
|
||||
title: Add Items to an Array with push() and unshift()
|
||||
---
|
||||
## Add Items to an Array with push() and unshift()
|
||||
# Add Items to an Array with push() and unshift()
|
||||
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
- Just like the example given, use the `.unshift()` method on the array to add elements to the start of the array and use the `.push()` method to add elements to the end of the array.
|
||||
|
||||
## Solution:
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
function mixedNumbers(arr) {
|
||||
// change code below this line
|
||||
arr.unshift('I',2,'three');
|
||||
arr.push(7,'VIII', 9);
|
||||
arr.unshift("I", 2, "three");
|
||||
arr.push(7, "VIII", 9);
|
||||
// change code above this line
|
||||
return arr;
|
||||
}
|
||||
|
||||
// do not change code below this line
|
||||
console.log(mixedNumbers(['IV', 5, 'six']));
|
||||
console.log(mixedNumbers(["IV", 5, "six"]));
|
||||
```
|
||||
</details>
|
@@ -1,10 +1,16 @@
|
||||
---
|
||||
title: Add Items Using splice()
|
||||
---
|
||||
## Add Items Using splice()
|
||||
# Add Items Using splice()
|
||||
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
- Using the splice() function, you must remove the first 2 elements from array `arr` and replace them with `DarkSalmon` and `BlanchedAlmond`.
|
||||
- Remember the splice function can take up to three parameters.
|
||||
## Example:
|
||||
#### Example:
|
||||
```javascript
|
||||
arr.splice(0, 1, "Two");
|
||||
/* The first two paramemters are the same as they were in the previous challenge.
|
||||
@@ -15,15 +21,29 @@ arr.splice(0, 1, "Two");
|
||||
*/
|
||||
```
|
||||
|
||||
## Solution:
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
function htmlColorNames(arr) {
|
||||
// change code below this line
|
||||
arr.splice(0, 2, "DarkSalmon", "BlanchedAlmond");
|
||||
// change code above this line
|
||||
return arr;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// do not change code below this line
|
||||
console.log(htmlColorNames(['DarkGoldenRod', 'WhiteSmoke', 'LavenderBlush', 'PaleTurqoise', 'FireBrick']));
|
||||
console.log(
|
||||
htmlColorNames([
|
||||
"DarkGoldenRod",
|
||||
"WhiteSmoke",
|
||||
"LavenderBlush",
|
||||
"PaleTurqoise",
|
||||
"FireBrick"
|
||||
])
|
||||
);
|
||||
```
|
||||
</details>
|
@@ -1,17 +1,26 @@
|
||||
---
|
||||
title: Add Key-Value Pairs to JavaScript Objects
|
||||
---
|
||||
## Add Key-Value Pairs to JavaScript Objects
|
||||
# Add Key-Value Pairs to JavaScript Objects
|
||||
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
- The foods object has already been declared. All that is left to be done is to add three new `key-values`.
|
||||
|
||||
```javascript
|
||||
OBJECT[{KEY}] = {VALUE}
|
||||
OBJECT[{ KEY }] = { VALUE };
|
||||
```
|
||||
|
||||
- The above code will create a ney `key-value` within the object.
|
||||
|
||||
## Solution
|
||||
|
||||
---
|
||||
## Solutions
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
let foods = {
|
||||
apples: 25,
|
||||
@@ -19,9 +28,10 @@ let foods = {
|
||||
plums: 28
|
||||
};
|
||||
// change code below this line
|
||||
foods['bananas'] = 13;
|
||||
foods['grapes'] = 35;
|
||||
foods['strawberries'] = 27;
|
||||
foods["bananas"] = 13;
|
||||
foods["grapes"] = 35;
|
||||
foods["strawberries"] = 27;
|
||||
// change code above this line
|
||||
console.log(foods);
|
||||
```
|
||||
</details>
|
@@ -1,37 +1,53 @@
|
||||
---
|
||||
title: Check For The Presence of an Element With indexOf()
|
||||
---
|
||||
## Check For The Presence of an Element With indexOf()
|
||||
# Check For The Presence of an Element With indexOf()
|
||||
|
||||
- A simple `if-statement` can be used to check whether or not the value returned by the `indexOf()` function is less than 0.
|
||||
- Once the value is discovered then you can return either `true` or `false`.
|
||||
- `Solution-1` demonstrates how a simple `if-statement` can return the correct result.
|
||||
|
||||
## Solution-1:
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
function quickCheck(arr, elem) {
|
||||
if(arr.indexOf(elem)>=0) {
|
||||
if (arr.indexOf(elem) >= 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
console.log(quickCheck(['squash', 'onions', 'shallots'], 'mushrooms'));
|
||||
console.log(quickCheck(["squash", "onions", "shallots"], "mushrooms"));
|
||||
```
|
||||
- `Solution-2` demonstrates how the problem can be solved using the `? : (conditional)` operator.
|
||||
|
||||
## Solution-2:
|
||||
#### Code Explanation
|
||||
- A simple `if-statement` can be used to check whether or not the value returned by the `indexOf()` function is less than 0.
|
||||
- Once the value is discovered then you can return either `true` or `false`.
|
||||
- demonstrates how a simple `if-statement` can return the correct result.
|
||||
</details>
|
||||
|
||||
<details><summary>Solution 2 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
function quickCheck(arr, elem) {
|
||||
return arr.indexOf(elem) >= 0 ? true : false;
|
||||
return arr.indexOf(elem) >= 0 ? true : false;
|
||||
}
|
||||
console.log(quickCheck(['squash', 'onions', 'shallots'], 'mushrooms'));
|
||||
console.log(quickCheck(["squash", "onions", "shallots"], "mushrooms"));
|
||||
```
|
||||
- `Solution-3` demonstrates how the problem can be solved by directly returning result of the comparison.
|
||||
|
||||
## Solution-3:
|
||||
#### Code Explanation
|
||||
- demonstrates how the problem can be solved using the `? : (conditional)` operator.
|
||||
</details>
|
||||
|
||||
|
||||
<details><summary>Solution 3 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
function quickCheck(arr, elem) {
|
||||
return arr.indexOf(elem) != -1;
|
||||
}
|
||||
console.log(quickCheck(['squash', 'onions', 'shallots'], 'mushrooms'));
|
||||
console.log(quickCheck(["squash", "onions", "shallots"], "mushrooms"));
|
||||
```
|
||||
|
||||
#### Code Explanation
|
||||
- demonstrates how the problem can be solved by directly returning result of the comparison.
|
||||
</details>
|
||||
|
@@ -1,11 +1,15 @@
|
||||
---
|
||||
title: Check if an Object has a Property
|
||||
---
|
||||
## Check if an Object has a Property
|
||||
# Check if an Object has a Property
|
||||
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
### Basic Solution:
|
||||
```javascript
|
||||
|
||||
let users = {
|
||||
Alan: {
|
||||
age: 27,
|
||||
@@ -27,8 +31,10 @@ let users = {
|
||||
|
||||
function isEveryoneHere(obj) {
|
||||
if (
|
||||
obj.hasOwnProperty('Alan') && obj.hasOwnProperty('Jeff') &&
|
||||
obj.hasOwnProperty('Sarah') && obj.hasOwnProperty('Ryan')
|
||||
obj.hasOwnProperty("Alan") &&
|
||||
obj.hasOwnProperty("Jeff") &&
|
||||
obj.hasOwnProperty("Sarah") &&
|
||||
obj.hasOwnProperty("Ryan")
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
@@ -39,8 +45,10 @@ function isEveryoneHere(obj) {
|
||||
#### Code Explanation
|
||||
|
||||
* Checks whether object contains all users by using the `hasOwnProperty` method for each name with the `&&` operator to return a `true` or `false` value.
|
||||
</details>
|
||||
|
||||
<details><summary>Solution 2 (Click to Show/Hide)</summary>
|
||||
|
||||
### Advanced Solution:
|
||||
```javascript
|
||||
let users = {
|
||||
Alan: {
|
||||
@@ -62,12 +70,9 @@ let users = {
|
||||
};
|
||||
|
||||
function isEveryoneHere(obj) {
|
||||
return [
|
||||
'Alan',
|
||||
'Jeff',
|
||||
'Sarah',
|
||||
'Ryan'
|
||||
].every(name => obj.hasOwnProperty(name));
|
||||
return ["Alan", "Jeff", "Sarah", "Ryan"].every(name =>
|
||||
obj.hasOwnProperty(name)
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
@@ -80,3 +85,5 @@ function isEveryoneHere(obj) {
|
||||
#### Relevant Links
|
||||
|
||||
* [Array.prototype.every](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every)
|
||||
|
||||
</details>
|
||||
|
@@ -1,14 +1,17 @@
|
||||
---
|
||||
title: Combine Arrays with the Spread Operator
|
||||
---
|
||||
## Combine Arrays with the Spread Operator
|
||||
# Combine Arrays with the Spread Operator
|
||||
|
||||
- The solution is exactly like the example given. Simply insert the `fragment[]` array into the `sentence[]` array at the desired index.
|
||||
|
||||
## Solution:
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 2 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
function spreadOut() {
|
||||
let fragment = ['to', 'code'];
|
||||
let fragment = ["to", "code"];
|
||||
let sentence = ["learning", ...fragment, "is", "fun"]; // change this line
|
||||
return sentence;
|
||||
}
|
||||
@@ -16,3 +19,6 @@ function spreadOut() {
|
||||
// do not change code below this line
|
||||
console.log(spreadOut());
|
||||
```
|
||||
#### Code Explanation
|
||||
- The solution is exactly like the example given. Simply insert the `fragment[]` array into the `sentence[]` array at the desired index.
|
||||
</details>
|
@@ -1,9 +1,16 @@
|
||||
---
|
||||
title: Copy an Array with the Spread Operator
|
||||
---
|
||||
## Copy an Array with the Spread Operator
|
||||
# Copy an Array with the Spread Operator
|
||||
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
- The final hint in the example tells you to use a recently learned method.
|
||||
|
||||
### Hint 2
|
||||
- The spread operator copies all elements into a new empty object.
|
||||
|
||||
```javascript
|
||||
@@ -16,7 +23,12 @@ while (num >= 1) {
|
||||
- The code above will copy all of the elements into `newArr` but will also reinitialise `newArr` with every new iteration of the while loop.
|
||||
- A new variable should first be initialised using the spread operator - `let obj = [...arr];` - then this variable should be added to the `newArr` for every iteration of the while loop.
|
||||
|
||||
## Solution:
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
function copyMachine(arr, num) {
|
||||
let newArr = [];
|
||||
@@ -32,3 +44,4 @@ function copyMachine(arr, num) {
|
||||
// change code here to test different cases:
|
||||
console.log(copyMachine([true, false, true], 2));
|
||||
```
|
||||
</details>
|
@@ -1,24 +1,32 @@
|
||||
---
|
||||
title: Copy Array Items Using slice()
|
||||
---
|
||||
## Copy Array Items Using slice()
|
||||
# Copy Array Items Using slice()
|
||||
|
||||
- the `slice()` function must be used to return an array consisting of only `warm` and `sunny`.
|
||||
- Therefore, two parameters must be passed to the `slice()` function. The first parameter must be the index you would like the substring to start at. The second parameter must be the index at which the substring ends.
|
||||
- Note: The second parameter will end the substring at that exact index.
|
||||
## Example:
|
||||
```javascript
|
||||
return arr.slice(1,4);
|
||||
/* This will return a substring consisting of indexs [1,2,3]
|
||||
Note: arr[4] is NOT included.
|
||||
return arr.slice(1, 4);
|
||||
/* This will return a substring consisting of indexs [1,2,3]
|
||||
Note: arr[4] is NOT included. */
|
||||
```
|
||||
## Solution:
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
function forecast(arr) {
|
||||
// change code below this line
|
||||
return arr.slice(2,4);
|
||||
return arr.slice(2, 4);
|
||||
}
|
||||
|
||||
// do not change code below this line
|
||||
console.log(forecast(['cold', 'rainy', 'warm', 'sunny', 'cool', 'thunderstorms']));
|
||||
console.log(
|
||||
forecast(["cold", "rainy", "warm", "sunny", "cool", "thunderstorms"])
|
||||
);
|
||||
```
|
||||
</details>
|
@@ -1,24 +1,38 @@
|
||||
---
|
||||
title: Create complex multi-dimensional arrays
|
||||
---
|
||||
## Create complex multi-dimensional arrays
|
||||
# Create complex multi-dimensional arrays
|
||||
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1
|
||||
- The first string - `deep` - must be inserted three levels deep. This means within exactly threes sets of `[square-brackets]`.
|
||||
|
||||
```javascript
|
||||
let threeLevelArray = ["first level", ["Two levels deep", ["Three levels deep"]]];
|
||||
let threeLevelArray = [
|
||||
"first level",
|
||||
["Two levels deep", ["Three levels deep"]]
|
||||
];
|
||||
```
|
||||
- Using this logic insert strings `deep` , `deeper` and `deepest` in the matrix three levels deep, four levels deep and five levels deep respectively.
|
||||
|
||||
## Solution:
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
let myNestedArray = [
|
||||
// change code below this line
|
||||
['unshift', false, 1, 2, 3, 'complex', 'nested'],
|
||||
['loop', 'shift', 6, 7, 1000, 'method'],
|
||||
['concat', false, true, 'spread', 'array',["deep"]],
|
||||
['mutate', 1327.98, 'splice', 'slice', 'push', [["deeper"]]],
|
||||
['iterate', 1.3849, 7, '8.4876', 'arbitrary', 'depth', [[["deepest"]]] ]
|
||||
["unshift", false, 1, 2, 3, "complex", "nested"],
|
||||
["loop", "shift", 6, 7, 1000, "method"],
|
||||
["concat", false, true, "spread", "array", ["deep"]],
|
||||
["mutate", 1327.98, "splice", "slice", "push", [["deeper"]]],
|
||||
["iterate", 1.3849, 7, "8.4876", "arbitrary", "depth", [[["deepest"]]]]
|
||||
// change code above this line
|
||||
];
|
||||
```
|
||||
</details>
|
@@ -1,17 +1,21 @@
|
||||
---
|
||||
title: Generate an Array of All Object Keys with Object.keys()
|
||||
---
|
||||
## Generate an Array of All Object Keys with Object.keys()
|
||||
# Generate an Array of All Object Keys with Object.keys()
|
||||
|
||||
### Method:
|
||||
---
|
||||
## Problem Explanation
|
||||
|
||||
- To return the array of users the `Object.keys()` method must take an arguement.
|
||||
- This challenge can be solved using a single line return statement.
|
||||
|
||||
### Solution:
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
|
||||
let users = {
|
||||
Alan: {
|
||||
age: 27,
|
||||
@@ -38,5 +42,5 @@ function getArrayOfUsers(obj) {
|
||||
}
|
||||
|
||||
console.log(getArrayOfUsers(users));
|
||||
|
||||
```
|
||||
</details>
|
@@ -1,13 +1,11 @@
|
||||
---
|
||||
title: Basic Data Structures
|
||||
---
|
||||
## Basic Data Structures
|
||||
# Basic Data Structures
|
||||
|
||||
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,40 +1,50 @@
|
||||
---
|
||||
title: Iterate Through All an Array's Items Using For Loops
|
||||
---
|
||||
## Iterate Through All an Array's Items Using For Loops
|
||||
# Iterate Through All an Array's Items Using For Loops
|
||||
|
||||
### Hint 1
|
||||
|
||||
- A nested `for` loop must be used to search through every element in the array.
|
||||
|
||||
## Hint 1
|
||||
- A nested ``for`` loop must be used to search through every element in the array.
|
||||
```javascript
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
````
|
||||
## Hint 2
|
||||
for (let i = 0; i < arr.length; i++) {}
|
||||
```
|
||||
|
||||
### Hint 2
|
||||
- Every element of the array must then be compared to the `elem` parameter passed through the `filteredArray()` function.
|
||||
```javascript
|
||||
if (arr[i].indexOf(elem)==-1){
|
||||
if (arr[i].indexOf(elem) == -1) {
|
||||
}
|
||||
```
|
||||
## Hint 3
|
||||
### Hint 3
|
||||
- If a match is NOT found then `newArr` have that entire subarray added. The `push()` function is very useful here.
|
||||
```javascript
|
||||
newArr.push(arr[i]);
|
||||
```
|
||||
- Once that entire subarray is added to `newArr` the loop continue with the next element.
|
||||
|
||||
## Solution:
|
||||
|
||||
---
|
||||
## Solutions
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
function filteredArray(arr, elem) {
|
||||
let newArr = [];
|
||||
// change code below this line
|
||||
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
if (arr[i].indexOf(elem)==-1){ //Checks every parameter for the element and if is NOT there continues the code
|
||||
newArr.push(arr[i]); //Inserts the element of the array in the new filtered array
|
||||
};
|
||||
};
|
||||
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
if (arr[i].indexOf(elem) == -1) {
|
||||
//Checks every parameter for the element and if is NOT there continues the code
|
||||
newArr.push(arr[i]); //Inserts the element of the array in the new filtered array
|
||||
}
|
||||
}
|
||||
|
||||
// change code above this line
|
||||
return newArr;
|
||||
};
|
||||
}
|
||||
// change code here to test different cases:
|
||||
console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));
|
||||
```
|
||||
</details>
|
@@ -1,42 +1,45 @@
|
||||
---
|
||||
title: Iterate Through the Keys of an Object with a for...in Statement
|
||||
---
|
||||
## Iterate Through the Keys of an Object with a for...in Statement
|
||||
# Iterate Through the Keys of an Object with a for...in Statement
|
||||
|
||||
Method:
|
||||
---
|
||||
## Problem Explanation
|
||||
- Note: `dot-notation` will cause errors in this challenge.
|
||||
- `[square-bracket]` notation must be used to call a variable property name.
|
||||
- The following code will not work.
|
||||
|
||||
### Example 1:
|
||||
|
||||
---
|
||||
## Hints
|
||||
|
||||
### Hint 1:
|
||||
|
||||
```javascript
|
||||
|
||||
for (let user in obj) {
|
||||
if(obj.user.online === true) {
|
||||
if (obj.user.online === true) {
|
||||
//code
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### Hint 2
|
||||
- Example 2 demonstrates how using `[square-bracket]` notation the code will be executed.
|
||||
|
||||
### Example 2:
|
||||
|
||||
```javascript
|
||||
|
||||
for (let user in obj) {
|
||||
if(obj[user].online === true) {
|
||||
if (obj[user].online === true) {
|
||||
//code
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### Solution:
|
||||
```javascript
|
||||
|
||||
---
|
||||
## Solutions
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
let users = {
|
||||
Alan: {
|
||||
age: 27,
|
||||
@@ -59,7 +62,7 @@ function countOnline(obj) {
|
||||
// change code below this line
|
||||
let result = 0;
|
||||
for (let user in obj) {
|
||||
if(obj[user].online === true) {
|
||||
if (obj[user].online === true) {
|
||||
result++;
|
||||
}
|
||||
}
|
||||
@@ -67,5 +70,5 @@ function countOnline(obj) {
|
||||
// change code above this line
|
||||
}
|
||||
console.log(countOnline(users));
|
||||
|
||||
```
|
||||
</details>
|
@@ -1,45 +1,45 @@
|
||||
---
|
||||
title: Modify an Array Stored in an Object
|
||||
---
|
||||
## Modify an Array Stored in an Object
|
||||
# Modify an Array Stored in an Object
|
||||
|
||||
### Method:
|
||||
---
|
||||
## Problem Explanation
|
||||
|
||||
- The function can be written in just two lines of code.
|
||||
- The first line should just use the `push()` function to add the `friend`parameter to the array found in `user.data.friend`. The second line will return the modified array.
|
||||
- Remember that `user` mustb be referenced with the first parameter given to the `addFriend()` function.
|
||||
|
||||
### Solution:
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
|
||||
let user = {
|
||||
name: 'Kenneth',
|
||||
name: "Kenneth",
|
||||
age: 28,
|
||||
data: {
|
||||
username: 'kennethCodesAllDay',
|
||||
joinDate: 'March 26, 2016',
|
||||
organization: 'freeCodeCamp',
|
||||
friends: [
|
||||
'Sam',
|
||||
'Kira',
|
||||
'Tomo'
|
||||
],
|
||||
username: "kennethCodesAllDay",
|
||||
joinDate: "March 26, 2016",
|
||||
organization: "freeCodeCamp",
|
||||
friends: ["Sam", "Kira", "Tomo"],
|
||||
location: {
|
||||
city: 'San Francisco',
|
||||
state: 'CA',
|
||||
country: 'USA'
|
||||
city: "San Francisco",
|
||||
state: "CA",
|
||||
country: "USA"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
function addFriend(userObj, friend) {
|
||||
// change code below this line
|
||||
// change code below this line
|
||||
userObj.data.friends.push(friend);
|
||||
return userObj.data.friends;
|
||||
// change code above this line
|
||||
}
|
||||
|
||||
console.log(addFriend(user, 'Pete'));
|
||||
|
||||
console.log(addFriend(user, "Pete"));
|
||||
```
|
||||
</details>
|
@@ -1,31 +1,38 @@
|
||||
---
|
||||
title: Modify an Object Nested Within an Object
|
||||
---
|
||||
## Modify an Object Nested Within an Object
|
||||
Method:
|
||||
# Modify an Object Nested Within an Object
|
||||
|
||||
---
|
||||
## Problem Explanation
|
||||
- Remember the object you want to change is two levels deep, `dot-notation` is easier to use in this instance.
|
||||
- Simply define the object and then use `dot-notation` to access the second object and finally the final element you wish to modify.
|
||||
|
||||
## Example:
|
||||
```javascript
|
||||
let myObject = {
|
||||
level_1: 'outside',
|
||||
level_1: "outside",
|
||||
first_level_object: {
|
||||
level_2: '2 levels deep',
|
||||
level_2: "2 levels deep",
|
||||
second_level_object: {
|
||||
level_3: '3 levels deep'
|
||||
}
|
||||
}
|
||||
level_3: "3 levels deep"
|
||||
}
|
||||
}
|
||||
};
|
||||
//The following line of code will modify the data found in level_2.
|
||||
myObject.first_level_object.level_2 = 'level-2 has been reached';
|
||||
myObject.first_level_object.level_2 = "level-2 has been reached";
|
||||
```
|
||||
|
||||
## Solution:
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
let userActivity = {
|
||||
id: 23894201352,
|
||||
date: 'January 1, 2017',
|
||||
date: "January 1, 2017",
|
||||
data: {
|
||||
totalUsers: 51,
|
||||
online: 42
|
||||
@@ -38,3 +45,4 @@ userActivity.data.online = 45;
|
||||
|
||||
console.log(userActivity);
|
||||
```
|
||||
</details>
|
@@ -1,11 +1,18 @@
|
||||
---
|
||||
title: Remove Items from an Array with pop() and shift()
|
||||
---
|
||||
## Remove Items from an Array with pop() and shift()
|
||||
# Remove Items from an Array with pop() and shift()
|
||||
|
||||
---
|
||||
## Problem Explanation
|
||||
- The `.pop()` method and `.shift()` method must be called and initialised using the `popped` and `shifted` variables to return the correct answer from the function.
|
||||
|
||||
## Solution:
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
function popShift(arr) {
|
||||
let popped = arr.pop();
|
||||
@@ -14,5 +21,6 @@ function popShift(arr) {
|
||||
}
|
||||
|
||||
// do not change code below this line
|
||||
console.log(popShift(['challenge', 'is', 'not', 'complete']));
|
||||
console.log(popShift(["challenge", "is", "not", "complete"]));
|
||||
```
|
||||
</details>
|
@@ -1,16 +1,23 @@
|
||||
---
|
||||
title: Remove Items Using splice()
|
||||
---
|
||||
## Remove Items Using splice()
|
||||
# Remove Items Using splice()
|
||||
|
||||
---
|
||||
## Problem Explanation
|
||||
- The `splice()` function must be called on the `arr` array in order to remove 1 or more elements from the center of the array.
|
||||
- The array `arr` currently adds up to the value of 16. Simply remove as many variables neccessary to return 10.
|
||||
|
||||
## Solution:
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
function sumOfTen(arr) {
|
||||
// change code below this line
|
||||
arr.splice(1,2);
|
||||
arr.splice(1, 2);
|
||||
// change code above this line
|
||||
return arr.reduce((a, b) => a + b);
|
||||
}
|
||||
@@ -18,3 +25,4 @@ function sumOfTen(arr) {
|
||||
// do not change code below this line
|
||||
console.log(sumOfTen([2, 5, 1, 5, 2, 1]));
|
||||
```
|
||||
</details>
|
@@ -1,9 +1,9 @@
|
||||
---
|
||||
title: Use an Array to Store a Collection of Data
|
||||
---
|
||||
## Use an Array to Store a Collection of Data
|
||||
# Use an Array to Store a Collection of Data
|
||||
|
||||
### Method:
|
||||
Problem Explanation
|
||||
|
||||
- In JS, Arrays are one of the most commonly used data structure. Unlike other languages Arrays in JS can store different data types and can also change their size on runtime and hence are also referred as "Dynamic Arrays". They are also 0 indexed.
|
||||
- Arrays can be initialized in different ways:
|
||||
@@ -12,22 +12,26 @@ title: Use an Array to Store a Collection of Data
|
||||
|
||||
- In this challenge we'll focus on Array literals. To initialize an array we simply do `let arr = [];`
|
||||
- We can add values to this array by accessing its index, example:
|
||||
```javascript
|
||||
let arr = [];
|
||||
arr[0] = "hello";
|
||||
console.log(arr); // ["hello"]
|
||||
```
|
||||
```javascript
|
||||
let arr = [];
|
||||
arr[0] = "hello";
|
||||
console.log(arr); // ["hello"]
|
||||
```
|
||||
- We can also initialize the values in the array when we declare it, example:
|
||||
```javascript
|
||||
let arr = [1, 2, 3, "John"];
|
||||
```
|
||||
```javascript
|
||||
let arr = [1, 2, 3, "John"];
|
||||
```
|
||||
- In this challenge you need to create an array with at least 5 elements and at least one string, one number, and one boolean.
|
||||
|
||||
### Solution:
|
||||
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
```js
|
||||
let yourArray = ["a", 2, true, "c", null, {name: "john"}];
|
||||
let yourArray = ["a", 2, true, "c", null, { name: "john" }];
|
||||
```
|
||||
|
||||
### Resources
|
||||
Further reading about arrays at [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array).
|
||||
#### Relevant Links
|
||||
Further reading about arrays at [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array).
|
||||
</details>
|
||||
|
@@ -1,11 +1,14 @@
|
||||
---
|
||||
title: Use the delete Keyword to Remove Object Properties
|
||||
---
|
||||
## Use the delete Keyword to Remove Object Properties
|
||||
# Use the delete Keyword to Remove Object Properties
|
||||
|
||||
[Developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete) provides a comprehensive tutorial on the delete operator.
|
||||
|
||||
### Solution:
|
||||
---
|
||||
## Solutions
|
||||
|
||||
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||||
|
||||
```javascript
|
||||
let foods = {
|
||||
apples: 25,
|
||||
@@ -22,3 +25,7 @@ delete foods.strawberries;
|
||||
// change code above this line
|
||||
console.log(foods);
|
||||
```
|
||||
|
||||
#### Relevant Links
|
||||
* [Developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete) provides a comprehensive tutorial on the delete operator.
|
||||
</details>
|
Reference in New Issue
Block a user