-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.js
More file actions
68 lines (55 loc) · 1.99 KB
/
helper.js
File metadata and controls
68 lines (55 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//MATH
console.log(Math.floor(5.8)); //Aredonda o numero para Baixo
console.log(Math.ceil(5.8)); //Aredonda o numero para cima
console.log(Math.round(2.3)); //Arredonda o numero para o mais proximo inteiro
//Array
let A = [1, 3, 5, 2, 9];
let B = [0, 1, 0, 2, 4, 8, 10];
let Aduplicados = [1, 2, 2, 3, 4, 3, 4];
const similar = A.filter((value) => B.includes(value));
console.log(`Similar: ${similar}`);
console.log(A.slice(2)); //REMOVE DO ARRAY ITENS ATE O INDEX [2]
A = A.concat(B); //juntar 2 ou mais Arrays
A.sort((a, b) => a - b);
console.log(A); //ordenar em ordem crescente
let semduplas = Array.from(new Set(Aduplicados)); // tirar duplicados do array
console.log(`${semduplas} = Sem estar Duplicados`);
//Add Element in Array
A.push(10); //add at the end
A.unshift(0); //Add at the start
A = [-1, ...A, 12]; //Add at the start and at the end
//NESTED ARRAY
const nested = [['A', 'B'], ['C']];
const flattened = nested.flat();
console.log(flattened);
//REDUCE
var total = A.reduce((total, numero) => total + numero, 0); //Somando tudo
console.log(total);
//MAP
var numbers = [4, 9, 16, 25];
var x = numbers.map(Math.sqrt);
console.log(x);
var newarray = numbers.map(myFunction); //Agora todos elementos do array vao passar na funcao, item por item
function myFunction(num) {
return num * 10;
}
console.log(newarray);
//The average of a nested Array
/*We will use recursion to solve this problem.
For each element in an array call the callback
If an element is a number then add the value to sum
Else if an element is an array then call the callback recursively.*/
let arr = [[1, 2, 3], 4, 5];
const s = arr
.flat()
.reduce((accumulator, currentValue) => accumulator + currentValue);
console.log(s);
//SPREAD OPERATOR
//Copying Arrays, also works with Objects, but does not perform deep-copy
let fruits = ['A', 'B', 'C', 'D'];
let fruitsCopy = [...fruits];
console.log(...fruitsCopy);
//Merging
let fruitsM = ['A', 'B', 'C', 'D'];
let vegetables = ['T', 'V', 'R'];
let fruitsAndVeg = [...fruits, ...vegetables];