fix(guide): simplify directory structure

This commit is contained in:
Mrugesh Mohapatra
2018-10-16 21:26:13 +05:30
parent f989c28c52
commit da0df12ab7
35752 changed files with 0 additions and 317652 deletions

View File

@ -0,0 +1,71 @@
---
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
Method:
- 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:
```javascript
for (let user in obj) {
if(obj.user.online === true) {
//code
}
}
```
- 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) {
//code
}
}
```
### Solution:
```javascript
let users = {
Alan: {
age: 27,
online: false
},
Jeff: {
age: 32,
online: true
},
Sarah: {
age: 48,
online: false
},
Ryan: {
age: 19,
online: true
}
};
function countOnline(obj) {
// change code below this line
let result = 0;
for (let user in obj) {
if(obj[user].online === true) {
result++;
}
}
return result;
// change code above this line
}
console.log(countOnline(users));
```

View File

@ -0,0 +1,11 @@
---
title: Access an Array's Contents Using Bracket Notation
---
## Access an Array's Contents Using Bracket Notation
- Remember the arrays index begins at 0 so the postion of b will be located in `myArray[1]`.
## Solution
```javascript
myArray[1] = "anything we want";
```

View File

@ -0,0 +1,43 @@
---
title: Access Property Names with Bracket Notation
---
## Access Property Names with Bracket Notation
Method:
- 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
};
function checkInventory(scannedItem) {
return juice[scannedItem];
}
```
## Solution:
```javascript
let foods = {
apples: 25,
oranges: 32,
plums: 28,
bananas: 13,
grapes: 35,
strawberries: 27
};
// do not change code above this line
function checkInventory(scannedItem) {
// change code below this line
return foods[scannedItem];
}
// change code below this line to test different cases:
console.log(checkInventory("apples"));
```

View File

@ -0,0 +1,20 @@
---
title: Add Items to an Array with push() and unshift()
---
## Add Items to an Array with push() and unshift()
- 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:
```javascript
function mixedNumbers(arr) {
// change code below this line
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']));
```

View File

@ -0,0 +1,29 @@
---
title: Add Items Using splice()
---
## Add Items Using splice()
- 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:
```javascript
arr.splice(0, 1, "Two");
/* The first two paramemters are the same as they were in the previous challenge.
`0` will start the `splice()` function off at index 0.
The second parameter `1` will remove only 1 variable from the array.
The final variable "Two" will replace the variable in arr[0].
Note: The final parameter can take more than 1 arguement.
*/
```
## Solution:
```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']));
```

View File

@ -0,0 +1,27 @@
---
title: Add Key-Value Pairs to JavaScript Objects
---
## Add Key-Value Pairs to JavaScript Objects
- 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}
```
- The above code will create a ney `key-value` within the object.
## Solution
```javascript
let foods = {
apples: 25,
oranges: 32,
plums: 28
};
// change code below this line
foods['bananas'] = 13;
foods['grapes'] = 35;
foods['strawberries'] = 27;
// change code above this line
console.log(foods);
```

View File

@ -0,0 +1,28 @@
---
title: 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:
```javascript
function quickCheck(arr, elem) {
if(arr.indexOf(elem)>=0) {
return true;
}
return false;
}
console.log(quickCheck(['squash', 'onions', 'shallots'], 'mushrooms'));
```
- `Solution-2` demonstrates how the problem can be solved using the `? : (conditional)` operator.
## Solution-2:
```javascript
function quickCheck(arr, elem) {
return arr.indexOf(elem) >= 0 ? true : false;
}
console.log(quickCheck(['squash', 'onions', 'shallots'], 'mushrooms'));
```

View File

@ -0,0 +1,54 @@
---
title: Check if an Object has a Property
---
## Check if an Object has a Property
Method:
- The simplest way to complete this challenge is to create an `ìf-statement` to check wether or not the object contains all useres, then to return a true or false statement. The first solution does just this.
- The second solution works in exactly the same way, only it uses 1 line of code - `Conditional(ternary)-Operator` - within the function.
[developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator) provides a more in depth analysis of the ternary operator.
### Solution-1:
```javascript
let users = {
Alan: {
age: 27,
online: true
},
Jeff: {
age: 32,
online: true
},
Sarah: {
age: 48,
online: true
},
Ryan: {
age: 19,
online: true
}
};
function isEveryoneHere(obj) {
// change code below this line
if(users.hasOwnProperty('Alan','Jeff','Sarah','Ryan')) {
return true;
}
return false;
// change code above this line
}
console.log(isEveryoneHere(users));
```
### Solution-2:
```javascript
function isEveryoneHere(obj) {
return (users.hasOwnProperty('Alan','Jeff','Sarah','Ryan')) ? true : false;
}
```

View File

@ -0,0 +1,18 @@
---
title: 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:
```javascript
function spreadOut() {
let fragment = ['to', 'code'];
let sentence = ["learning", ...fragment, "is", "fun"]; // change this line
return sentence;
}
// do not change code below this line
console.log(spreadOut());
```

View File

@ -0,0 +1,34 @@
---
title: Copy an Array with the Spread Operator
---
## Copy an Array with the Spread Operator
- The final hint in the example tells you to use a recently learned method.
- The spread operator copies all elements into a new empty object.
```javascript
while (num >= 1) {
newArr = [...arr]
num--;
}
```
- 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:
```javascript
function copyMachine(arr, num) {
let newArr = [];
while (num >= 1) {
// change code below this line
newArr.push([...arr]);
// change code above this line
num--;
}
return newArr;
}
// change code here to test different cases:
console.log(copyMachine([true, false, true], 2));
```

View File

@ -0,0 +1,24 @@
---
title: 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.
```
## Solution:
```javascript
function forecast(arr) {
// change code below this line
return arr.slice(2,4);
}
// do not change code below this line
console.log(forecast(['cold', 'rainy', 'warm', 'sunny', 'cool', 'thunderstorms']));
```

View File

@ -0,0 +1,24 @@
---
title: Create complex multi-dimensional arrays
---
## Create complex multi-dimensional arrays
- 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"]]];
```
- Using this logic insert strings `deep` , `deeper` and `deepest` in the matrix three levels deep, four levels deep and five levels deep respectively.
## Solution:
```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"]]] ]
// change code above this line
];
```

View File

@ -0,0 +1,42 @@
---
title: Generate an Array of All Object Keys with Object.keys()
---
## Generate an Array of All Object Keys with Object.keys()
### Method:
- 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:
```javascript
let users = {
Alan: {
age: 27,
online: false
},
Jeff: {
age: 32,
online: true
},
Sarah: {
age: 48,
online: false
},
Ryan: {
age: 19,
online: true
}
};
function getArrayOfUsers(obj) {
// change code below this line
return Object.keys(obj);
// change code above this line
}
console.log(getArrayOfUsers(users));
```

View File

@ -0,0 +1,13 @@
---
title: 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 -->

View File

@ -0,0 +1,40 @@
---
title: 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.
```javascript
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){
```
## 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:
```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
};
};
// 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));
```

View File

@ -0,0 +1,45 @@
---
title: Modify an Array Stored in an Object
---
## Modify an Array Stored in an Object
### Method:
- 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:
```javascript
let user = {
name: 'Kenneth',
age: 28,
data: {
username: 'kennethCodesAllDay',
joinDate: 'March 26, 2016',
organization: 'freeCodeCamp',
friends: [
'Sam',
'Kira',
'Tomo'
],
location: {
city: 'San Francisco',
state: 'CA',
country: 'USA'
}
}
};
function addFriend(userObj, friend) {
// change code below this line
userObj.data.friends.push(friend);
return userObj.data.friends;
// change code above this line
}
console.log(addFriend(user, 'Pete'));
```

View File

@ -0,0 +1,40 @@
---
title: Modify an Object Nested Within an Object
---
## Modify an Object Nested Within an Object
Method:
- 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',
first_level_object: {
level_2: '2 levels deep',
second_level_object: {
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';
```
## Solution:
```javascript
let userActivity = {
id: 23894201352,
date: 'January 1, 2017',
data: {
totalUsers: 51,
online: 42
}
};
// change code below this line
userActivity.data.online = 45;
// change code above this line
console.log(userActivity);
```

View File

@ -0,0 +1,18 @@
---
title: Remove Items from an Array with pop() and shift()
---
## Remove Items from an Array with pop() and shift()
- 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:
```javascript
function popShift(arr) {
let popped = arr.pop();
let shifted = arr.shift();
return [shifted, popped];
}
// do not change code below this line
console.log(popShift(['challenge', 'is', 'not', 'complete']));
```

View File

@ -0,0 +1,20 @@
---
title: Remove Items Using splice()
---
## Remove Items Using splice()
- 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:
```javascript
function sumOfTen(arr) {
// change code below this line
arr.splice(1,2);
// change code above this line
return arr.reduce((a, b) => a + b);
}
// do not change code below this line
console.log(sumOfTen([2, 5, 1, 5, 2, 1]));
```

View File

@ -0,0 +1,33 @@
---
title: Use an Array to Store a Collection of Data
---
## Use an Array to Store a Collection of Data
### Method:
- 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:
1. Array literals
2. Array constructors
- 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"]
```
- We can also initialize the values in the array when we declare it, example:
```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:
```js
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).

View File

@ -0,0 +1,24 @@
---
title: 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:
```javascript
let foods = {
apples: 25,
oranges: 32,
plums: 28,
bananas: 13,
grapes: 35,
strawberries: 27
};
// change code below this line
delete foods.oranges;
delete foods.plums;
delete foods.strawberries;
// change code above this line
console.log(foods);
```