A partir d'un tableau d'objets (voir ci-dessous) écrire les fonctions permettant
de calculer :
- le nombre d'hommes
- afficher les âges des hommes
- calculer l'âge moyen des hommes.
Étudier la flexibilité de votre code (même questions pour les homme nés avant 1900, les femmes ... )
Base de noms : un tableau d'objets !
[
{"name": "Carolus Haverbeke", "sex": "m", "born": 1832, "died": 1905, "father": "Carel Haverbeke", "mother": "Maria van Brussel"},
{"name": "Emma de Milliano", "sex": "f", "born": 1876, "died": 1956, "father": "Petrus de Milliano", "mother": "Sophia van Damme"},
{"name": "Maria de Rycke", "sex": "f", "born": 1683, "died": 1724, "father": "Frederik de Rycke", "mother": "Laurentia van Vlaenderen"},
{"name": "Jan van Brussel", "sex": "m", "born": 1714, "died": 1748, "father": "Jacobus van Brussel", "mother": "Joanna van Rooten"},
{"name": "Philibert Haverbeke", "sex": "m", "born": 1907, "died": 1997, "father": "Emile Haverbeke", "mother": "Emma de Milliano"},
{"name": "Jan Frans van Brussel", "sex": "m", "born": 1761, "died": 1833, "father": "Jacobus Bernardus van Brussel", "mother":null},
{"name": "Pauwels van Haverbeke", "sex": "m", "born": 1535, "died": 1582, "father": "N. van Haverbeke", "mother":null},
{"name": "Clara Aernoudts", "sex": "f", "born": 1918, "died": 2012, "father": "Henry Aernoudts", "mother": "Sidonie Coene"},
{"name": "Emile Haverbeke", "sex": "m", "born": 1877, "died": 1968, "father": "Carolus Haverbeke", "mother": "Maria Sturm"},
{"name": "Lieven de Causmaecker", "sex": "m", "born": 1696, "died": 1724, "father": "Carel de Causmaecker", "mother": "Joanna Claes"},
{"name": "Pieter Haverbeke", "sex": "m", "born": 1602, "died": 1642, "father": "Lieven van Haverbeke", "mother":null},
{"name": "Livina Haverbeke", "sex": "f", "born": 1692, "died": 1743, "father": "Daniel Haverbeke", "mother": "Joanna de Pape"},
{"name": "Pieter Bernard Haverbeke", "sex": "m", "born": 1695, "died": 1762, "father": "Willem Haverbeke", "mother": "Petronella Wauters"},
{"name": "Lieven van Haverbeke", "sex": "m", "born": 1570, "died": 1636, "father": "Pauwels van Haverbeke", "mother": "Lievijne Jans"},
{"name": "Joanna de Causmaecker", "sex": "f", "born": 1762, "died": 1807, "father": "Bernardus de Causmaecker", "mother":null},
{"name": "Willem Haverbeke", "sex": "m", "born": 1668, "died": 1731, "father": "Lieven Haverbeke", "mother": "Elisabeth Hercke"},
{"name": "Pieter Antone Haverbeke", "sex": "m", "born": 1753, "died": 1798, "father": "Jan Francies Haverbeke", "mother": "Petronella de Decker"},
{"name": "Maria van Brussel", "sex": "f", "born": 1801, "died": 1834, "father": "Jan Frans van Brussel", "mother": "Joanna de Causmaecker"},
{"name": "Angela Haverbeke", "sex": "f", "born": 1728, "died": 1734, "father": "Pieter Bernard Haverbeke", "mother": "Livina de Vrieze"},
{"name": "Elisabeth Haverbeke", "sex": "f", "born": 1711, "died": 1754, "father": "Jan Haverbeke", "mother": "Maria de Rycke"},
{"name": "Lievijne Jans", "sex": "f", "born": 1542, "died": 1582, "father":null, "mother":null},
{"name": "Bernardus de Causmaecker", "sex": "m", "born": 1721, "died": 1789, "father": "Lieven de Causmaecker", "mother": "Livina Haverbeke"},
{"name": "Jacoba Lammens", "sex": "f", "born": 1699, "died": 1740, "father": "Lieven Lammens", "mother": "Livina de Vrieze"},
{"name": "Pieter de Decker", "sex": "m", "born": 1705, "died": 1780, "father": "Joos de Decker", "mother": "Petronella van de Steene"},
{"name": "Joanna de Pape", "sex": "f", "born": 1654, "died": 1723, "father": "Vincent de Pape", "mother": "Petronella Wauters"},
{"name": "Daniel Haverbeke", "sex": "m", "born": 1652, "died": 1723, "father": "Lieven Haverbeke", "mother": "Elisabeth Hercke"},
{"name": "Lieven Haverbeke", "sex": "m", "born": 1631, "died": 1676, "father": "Pieter Haverbeke", "mother": "Anna van Hecke"},
{"name": "Martina de Pape", "sex": "f", "born": 1666, "died": 1727, "father": "Vincent de Pape", "mother": "Petronella Wauters"},
{"name": "Jan Francies Haverbeke", "sex": "m", "born": 1725, "died": 1779, "father": "Pieter Bernard Haverbeke", "mother": "Livina de Vrieze"},
{"name": "Maria Haverbeke", "sex": "m", "born": 1905, "died": 1997, "father": "Emile Haverbeke", "mother": "Emma de Milliano"},
{"name": "Petronella de Decker", "sex": "f", "born": 1731, "died": 1781, "father": "Pieter de Decker", "mother": "Livina Haverbeke"},
{"name": "Livina Sierens", "sex": "f", "born": 1761, "died": 1826, "father": "Jan Sierens", "mother": "Maria van Waes"},
{"name": "Laurentia Haverbeke", "sex": "f", "born": 1710, "died": 1786, "father": "Jan Haverbeke", "mother": "Maria de Rycke"},
{"name": "Carel Haverbeke", "sex": "m", "born": 1796, "died": 1837, "father": "Pieter Antone Haverbeke", "mother": "Livina Sierens"},
{"name": "Elisabeth Hercke", "sex": "f", "born": 1632, "died": 1674, "father": "Willem Hercke", "mother": "Margriet de Brabander"},
{"name": "Jan Haverbeke", "sex": "m", "born": 1671, "died": 1731, "father": "Lieven Haverbeke", "mother": "Elisabeth Hercke"},
{"name": "Anna van Hecke", "sex": "f", "born": 1607, "died": 1670, "father": "Paschasius van Hecke", "mother": "Martijntken Beelaert"},
{"name": "Maria Sturm", "sex": "f", "born": 1835, "died": 1917, "father": "Charles Sturm", "mother": "Seraphina Spelier"},
{"name": "Jacobus Bernardus van Brussel", "sex": "m", "born": 1736, "died": 1809, "father": "Jan van Brussel", "mother": "Elisabeth Haverbeke"}
]
Correction :
function fetchPersonnes() {
// Fetch
$.ajax({type: "GET",
url: "http://output.jsbin.com/fokuyi/1.js",
dataType: "json",
success: handleData
});
}
function handleData (pers) {
var homT = filter(pers,isMen);
console.log(` le nb homme = ${homT.length}`);
var menAge = map(homT,age);
console.log(` Age des hommes = ${menAge}`);
var averageMen = reduce(menAge,sum,0)/homT.length;
console.log(` moyenne age ${Math.ceil(averageMen)}`);
// le min
console.log("Année de naissance min =" + reduce(homT,function(val,persI){
return (val < persI.born) ? val : persI.born;
},homT[0]));
//le max
console.log("Année de naissance max =" +reduce(homT,function(val,persI){
return (val > persI.born) ? val : persI.born;
},homT[0]));
//moyenne
console.log("Année de naissance moyenne=" +Math.ceil(reduce(homT,function(val,persI){
return (val + persI.born);
},0) / homT.length));
}
function reduce(array, combine, start) {
var current = start;
for (var i = 0; i < array.length; i++)
current = combine(current, array[i]);
return current;
}
function sortByBorn(a, b){
return (a.born - a.born);
}
function isMen(data){
//console.log(data.sex);
return (data.sex === "m");
}
function age(data){
return (data.died - data.born);
}
function sum(a,b){
return a+b;q
}
function filter(array, test) {
var passed = [];
for (var i = 0; i < array.length; i++) {
if (test(array[i]))
passed.push(array[i]);
}
return passed;
}
function map(array, transform) {
var mapped = [];
for (var i = 0; i < array.length; i++)
mapped.push(transform(array[i]));
return mapped;
}
$(document).ready(function() {
fetchPersonnes();
});
Solution en action :
http://jsbin.com/jebixe/2/edit?html,js,console,output
---------------------------------------------------------- version 1
function fetchPersonnes() {
// Fetch
$.ajax({type: "GET",
url: "http://output.jsbin.com/fokuyi/1.js",
dataType: "json",
success: handleData
});
}
function handleData (pers) {
var homT = filter(pers,isMen);
console.log(` le nb homme = ${homT.length}`);
var menAge = map(homT,age);
console.log(` Age des hommes = ${menAge}`);
var averageMen = reduce(menAge,sum,0)/homT.length;
console.log(` moyenne age ${Math.ceil(averageMen)}`);
// le min
console.log("Année de naissance min =" + reduce(homT,ageMin,homT[0]));
//le max
console.log("Année de naissance max =" +reduce(homT,ageMax,homT[0]));
//moyenne
console.log("Année de naissance moyenne=" +Math.ceil(reduce(homT,sumAge,0) / homT.length));
}
function sortByBorn(a, b){
return (a.born - a.born);
}
function isMen(data){
//console.log(data.sex);
return (data.sex === "m");
}
function age(data){
return (data.died - data.born);
}
function ageMin(val,persI){
return (val < persI.born) ? val : persI.born;
}
function ageMax(val,persI){
return (val > persI.born) ? val : persI.born;
}
function sumAge(val,persI){
return (val + persI.born);
}
function sum(a,b){
return a+b;
}
function forEach(array, fx) {
for (var i = 0; i < array.length; i++)
fx(array[i]);
}
/*
function filter(array, test) {
var passed = [];
for (var i = 0; i < array.length; i++) {
if (test(array[i]))
passed.push(array[i]);
}
return passed;
}
*/
function filter(array, test) {
var passed = [];
forEach(array, function(v){
if (test(v))
passed.push(v);
});
return passed;
}
/*
function map(array, transform) {
var mapped = [];
for (var i = 0; i < array.length; i++)
mapped.push(transform(array[i]));
return mapped;
}
*/
function map(array, transform) {
var mapped = [];
forEach(array, function(v){
mapped.push(transform(v));
});
return mapped;
}
/*
function reduce(array, combine, start) {
var current = start;
for (var i = 0; i < array.length; i++)
current = combine(current, array[i]);
return current;
}
*/
function reduce(array, combine, start) {
var current = start;
forEach(array, function(v){
current = combine(current,v);
})
return current;
}
$(document).ready(function() {
fetchPersonnes();
});
http://jsbin.com/jebixe/6/edit?html,js,console,output