Skip to content

Commit 941fd23

Browse files
author
DouglasHdezT
committed
Add: Arrays Examples
1 parent 022de21 commit 941fd23

6 files changed

Lines changed: 232 additions & 0 deletions

File tree

5-Arrays/Example1.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use-strict'
2+
3+
/**
4+
* Push, pop, unshift, shift
5+
*/
6+
7+
let array1 = [9, 5, 6, 3];
8+
let array2 = [1, 2, 3, 4, 5];
9+
10+
/**
11+
* Push and pop, adds and remove items at the end of the array respectively
12+
*/
13+
14+
const manageArrayAtTheEnd = () => {
15+
console.log("Manage Array at end ");
16+
17+
console.log("Pushing one element");
18+
19+
array1.push(3);
20+
console.log(array1);
21+
22+
console.log("Pushing several elements");
23+
24+
array1.push(2,4,3,5,2);
25+
console.log(array1);
26+
27+
console.log("Poping one element");
28+
29+
console.log(array1.pop());
30+
console.log(array1);
31+
}
32+
33+
/**
34+
* Shift and unshift adds and remove items at the begin of the array respectively
35+
*/
36+
37+
const manageArrayAtTheBegin = () => {
38+
console.log("manage Array at the begin");
39+
40+
console.log("Unshift one element");
41+
42+
array2.unshift(4)
43+
console.log(array2);
44+
45+
console.log("Unshifting several elements");
46+
47+
array2.unshift(6,4,3,5,2);
48+
console.log(array2);
49+
50+
console.log("shift one element");
51+
52+
console.log(array2.shift());
53+
console.log(array2);
54+
}
55+
56+
manageArrayAtTheEnd();
57+
manageArrayAtTheBegin();

5-Arrays/Example2.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use-strict'
2+
3+
/**
4+
* Concat, slice and splice
5+
*/
6+
7+
let array1 = [1, 2, 3, 4];
8+
let array2 = [4, 3, 2, 1];
9+
10+
/**
11+
* Concat join two arrays, and return a new instace of Array with the result. Dont alter both originals arrays
12+
*/
13+
14+
const concatArrays = () => {
15+
let array3 = array1.concat(array2);
16+
console.log(array3);
17+
}
18+
19+
/**
20+
* Slice return a copy of part of an array in a new instace of Array. Dont alter original array.
21+
*/
22+
23+
const sliceArrays = () => {
24+
//The begin position is included, and the end position is excluded
25+
let arraySliced = array1.slice(0, 2);
26+
console.log(arraySliced);
27+
}
28+
29+
/**
30+
* Splice returns a part of an array, deleting it from the original array.
31+
*/
32+
33+
const spliceArrays = () => {
34+
//The begin position is included, and the end position is excluded
35+
let arraySpliced = array1.splice(0, 2);
36+
console.log(`Subarray: ${arraySpliced}`);
37+
console.log(`New array: ${array1}`);
38+
39+
}
40+
41+
concatArrays();
42+
sliceArrays();
43+
spliceArrays();

5-Arrays/Example3.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use-strict'
2+
3+
/**
4+
* For each; it's used to go through an array; you can know every item with its position in every iteration
5+
*/
6+
7+
let array = [1, 9, 8, 2, 7, 3, 6 ,4, 5];
8+
9+
const oddEven = () =>{
10+
11+
array.forEach(element => {
12+
13+
element % 2 == 0 ? console.log("Even"): console.log("Odd");
14+
15+
});
16+
17+
}
18+
19+
const listAllElements = () => {
20+
21+
array.forEach((element, index) => {
22+
23+
console.log(`${index}: ${element}`);
24+
25+
});
26+
27+
}
28+
29+
oddEven();
30+
listAllElements();

5-Arrays/Example4.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Filter; it's used to filter array items that fullfill a condition. Returns a new instance of Array.
3+
*/
4+
5+
let books = [
6+
"Cien años de soledad",
7+
"Rayuela",
8+
"El padrino",
9+
"Simbolo perdido",
10+
"El Tunel",
11+
"El retrato de Dorian Grey"
12+
]
13+
14+
const filterEBooks = () => {
15+
let ebooks = books.filter(element =>{
16+
return element.charAt(0) == "E";
17+
});
18+
19+
console.log(ebooks);
20+
21+
}
22+
23+
filterEBooks();

5-Arrays/Example5.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Map; Apply a function to every item in an array, returning a new array with the items modified; Returns a new instance of Array
3+
*/
4+
5+
let numbers = [1, 2, 3, 4, 5];
6+
let persons = [
7+
{name: "Douglas", lastname: "Hernandez"},
8+
{name: "Pedro", lastname: "Gomez"},
9+
]
10+
11+
const arrayTimesX = x => {
12+
let newArray = numbers.map(element => {
13+
return element * x;
14+
});
15+
16+
console.log(newArray);
17+
}
18+
19+
const listFullnames = () => {
20+
let fullnames = persons.map(element => {
21+
let fullname = `${element.name} ${element.lastname}`;
22+
return fullname;
23+
});
24+
25+
console.log(fullnames);
26+
27+
}
28+
29+
arrayTimesX(2);
30+
arrayTimesX(3);
31+
arrayTimesX(4);
32+
arrayTimesX(5);
33+
34+
listFullnames();

5-Arrays/Example6.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Reduce; It reduce the complete array into a single result; it use every element in a formula and the result is joined with an acumulator.
3+
* Returns the acumulator
4+
*/
5+
6+
let books = [
7+
"Cien años de soledad",
8+
"Rayuela",
9+
"El padrino",
10+
"Simbolo perdido",
11+
"El Tunel",
12+
"El retrato de Dorian Grey"
13+
]
14+
15+
let numbers = [1, 2, 3, 4, 5];
16+
17+
let addAllNumbers = () => {
18+
let acumulator = numbers.reduce ((curResult, element) => {
19+
return curResult + element;
20+
})
21+
22+
console.log(acumulator);
23+
}
24+
25+
let subAllNumbers = () => {
26+
let acumulator = numbers.reduce ((curResult, element) => {
27+
return curResult - element;
28+
});
29+
30+
console.log(acumulator);
31+
}
32+
33+
let showBookshelf = () => {
34+
let myBookshelf = books.reduce((result, book) =>{
35+
return result + `${book}, `;
36+
}, "My Bookshelf is: ");
37+
38+
myBookshelf = myBookshelf.slice(0, -1);
39+
console.log(myBookshelf);
40+
41+
}
42+
43+
addAllNumbers();
44+
subAllNumbers();
45+
showBookshelf();

0 commit comments

Comments
 (0)