allTag = (node) =>{
let tag = [],
allEle = {};
_explore = (node) => {
for (let elt of node.children) {
tag.push(elt.nodeName);
_explore(elt);
}
}
_explore(node);
_getWordCnt = (arr) => {
return arr.reduce(function(prev,next){
prev[next] = (prev[next] + 1) || 1;
return prev;
},{});
}
allEle = _getWordCnt(tag);
//allEle {div:2,p:10}
console.log(Object.keys(allEle).map(key => `${key}*${allEle[key]} `));
console.log(`La balise la plus utilisée est : ${Object.keys(allEle).reduce((keya, keyb) =>allEle[keya] > allEle[keyb] ? keya : keyb)}`);
let all = Object.keys(allEle).sort((keya, keyb) => allEle[keyb] - allEle[keya]);
console.log("par ordre de présence ** ");
for (var b of all) {
console.log(` ${b} ${allEle[b]} ` );
}
return allEle;
}
let a = allTag(document.body);
for (var b in a) {
if (a.hasOwnProperty(b)) {
console.log(`la balise ${b} apparaît ${a[b]} fois ` );
}
}