The function calculates the mean of a class with any number of students. At first this was difficult to me cause I'm a novice in the use of arrays. Even when my score was 10/10, I had to re-read the program to understand what I did :p.
- var class1 = [75, 53, 92, 100, 24, 55, 86,
- 85, 43, 55, 84, 82, 99, 51];
- var class2 = [54, 92, 72, 65, 66,
- 83, 44, 81, 88, 78];
- function sum(array) {
- var sum = 0;
- for (i = 0; i < array.length; i++) {
- sum += array[i]
- }
- return sum;
- }
- function mean(array) {
- return sum(array) / array.length;
- }
- var mean1 = mean(class1);
- var mean2 = mean(class2);
- console.log('Class 1 mean: ' + Math.round(mean1));
- console.log('Class 2 mean: ' + Math.round(mean2));
Class 1 mean 70
Class 2 mean 72
The program above was made in codeavengers.com
Español: El programa arriba escrito calcula la media de n estudiantes en una clase cualquiera. La amplitud no es fija, sólo depende del array. Fue hecho en codeavengers en el curso de javascript nivel 2.
No hay comentarios:
Publicar un comentario