Spocoman

Football Results

Feb 22nd, 2022 (edited)
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function footballResults(input) {
  2.     let won = 0;
  3.     let lost = 0;
  4.     let drawn = 0;
  5.     for(let i = 0; i < 3; i++) {
  6.         let game = input[i];
  7.         if (game[0] > game[2]) {
  8.             won++;
  9.         } else if (game[0] < game[2]) {
  10.             lost++;
  11.         } else {
  12.             drawn++;
  13.         }
  14.     }
  15.     console.log(`Team won ${won} games.`);
  16.     console.log(`Team lost ${lost} games.`);
  17.     console.log(`Drawn games: ${drawn}`);
  18. }
  19.  
  20.  
  21. Fundamentals solution with collection and ternary operator:
  22.  
  23. function footballResults(input) {
  24.     let result = {'won' : 0, 'lost' : 0, 'drawn' : 0};
  25.  
  26.     for(let i = 0; i < 3; i++) {
  27.         let game = input[i];
  28.         game[0] > game[2] ? result['won']++ : game[0] < game[2] ? result['lost']++ : result['drawn']++;
  29.     }
  30.     console.log(`Team won ${result['won']} games.\nTeam lost ${result['lost']} games.\nDrawn games: ${result['drawn']}`);
  31. }
Add Comment
Please, Sign In to add comment