Advertisement
vinissh

imperativoVsDeclarativo.js

Jan 18th, 2021
616
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const alunos   = [
  2.     {nome:'Joao', nota:7.9},
  3.     {nome:'Maria', nota:9.2}
  4. ]
  5.  
  6. //Imperativo
  7.  
  8. let total1 = 0;
  9. for(let i=0; i < alunos.length; i++){
  10.     total1 += alunos[i].nota;
  11. }
  12.  
  13. console.log(total1 / alunos.length);
  14.  
  15. //Declarativo
  16. const getNota = alunos => alunos.nota
  17. const soma = (total, atual) => total + atual
  18. const total2 = alunos.map(getNota).reduce(soma)
  19. console.log(total2 / alunos.length)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement