forked from Zelechos/Pragmatic_JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise41.js
More file actions
74 lines (64 loc) · 2.13 KB
/
Copy pathExercise41.js
File metadata and controls
74 lines (64 loc) · 2.13 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
69
70
71
72
73
74
// 41) Programa una función que dado un arreglo de números obtenga el promedio, pe. promedio([9,8,7,6,5,4,3,2,1,0]) devolverá 4.5.
import { validateNumber , validateArray } from "./validations.js";
'use strict'
const averageNumbers = array =>{
if (validateState(array)){
console.warn("------- solucion anticuada ------");
average(array);
console.warn("------- solucion ECMASCRIPT-6 ------");
averageECMA6(array);
}
}
// Solucion ECMASCRIPT 6 actual
const averageECMA6 = array => array.reduce((sum, element, index, array)=>{
sum += element;
if (index === array.length-1)
return console.log(`the array is [${array}]\nthe average ECMA-6 of the numbers is => ${sum/array.length}`);
else
return sum;
});
// Esta es una solucion anticuadad de ECMASCRIPT 5 para abajo
const average = array =>{
let sum = 0;
for (const iterator of array) {
sum += iterator;
}
return console.log(`the array is [${array}]\nthe average of the numbers is => ${sum/array.length}`);
}
const validateState = array =>{
if (validateArray(array)){
if (array.length > 1){
if(validateStatus(array)){
return true;
}
}else{
return console.warn(`the length of the array must be greater than 1!!!`);
}
}
}
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
averageNumbers([9,8,7,6,5,4,3,2,1,0]);
averageNumbers([5,5,5,5,5]);
averageNumbers([1,2,3,4,5,6,false,0]);
averageNumbers([]);
averageNumbers([1]);
averageNumbers([1 , "ether"]);
averageNumbers("[1,2,3,4,5,6,false,0]");
averageNumbers({});
averageNumbers(false);
averageNumbers(2341324);
averageNumbers();