forked from Zelechos/Pragmatic_JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise38.js
More file actions
72 lines (57 loc) · 2.18 KB
/
Copy pathExercise38.js
File metadata and controls
72 lines (57 loc) · 2.18 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
// 38) Programa una función que dado un array de números devuelva un objeto con 2 arreglos en el primero almacena los números pares y en el segundo los impares, pe. miFuncion([1,2,3,4,5,6,7,8,9,0]) devolverá {pares: [2,4,6,8,0], impares: [1,3,5,7,9]}.
import { validateNumber, validateArray } from "./validations.js"
'use strict'
const arrayRipper = array => {
if (validateArray(array)){
if (array.length > 1){
if(validateStatus(array)){
let response = ripper(array);
console.warn("---- With my methods ---");
console.info({pairs : response[0], odd : response[1]});
console.warn("---- With filter method ---");
console.info(effectiveRipper(array));
}
}else{
console.warn(`the length of the array must be greater than 1!!!`);
}
}
}
// de esta manera tambien se podia realizar el llenado de array usando filter
const effectiveRipper = array => new Object({pairs: array.filter(element => element % 2 === 0), odds: array.filter(element => element % 2 !== 0)})
const ripper = array =>{
let arrayPair = [] , arrayOdd = [];
for (let index = 0; index < array.length; index++) {
if(array[index] % 2 == 0){
arrayPair.push(array[index]);
}else{
arrayOdd.push(array[index]);
}
}
return [arrayPair, arrayOdd];
}
const validateStatus = array =>{
let validateState = true, index_capture;
for (let index = 0; index < array.length; index++) {
if(!(validateNumber(array[index]))){
validateState = false;
index_capture = index;
break;
}
}
if(!validateState){
console.error(`wrong element ${index_capture} of array => element [${typeof(array[index_capture])}] \n was expected => element [number]`);
}
return validateState;
}
// Testing
arrayRipper([1,2,3,4,5,6,7,8,9,0]);
arrayRipper([12,13,14,15,16,17,18,19,20]);
arrayRipper([1,2,3,4,5,6,false,0]);
arrayRipper([]);
arrayRipper([1]);
arrayRipper([1 , "ether"]);
arrayRipper("[1,2,3,4,5,6,false,0]");
arrayRipper({});
arrayRipper(false);
arrayRipper(2341324);
arrayRipper();