Advertisement
elena1234

Objects in JavaScript - print

Aug 31st, 2021
1,081
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(array) {
  2.     let juices = [];
  3.     let bottlesOfJuices = [];
  4.    
  5.     for (const line of array) {
  6.         let [juiceName, quantity] = line.split(' => ');
  7.         quantity = Number(quantity);
  8.         if (!juices[juiceName]) {
  9.             juices[juiceName] = quantity;
  10.         } else {
  11.             juices[juiceName] += quantity;
  12.         }
  13.  
  14.         if (juices[juiceName] >= 1000) {
  15.             let quantity = juices[juiceName];
  16.             let bottles = Math.floor(quantity / 1000);
  17.             juices[juiceName] = quantity % 1000;
  18.             if (!bottlesOfJuices[juiceName]) {
  19.                 bottlesOfJuices[juiceName] = bottles;
  20.             } else {
  21.                 bottlesOfJuices[juiceName] += bottles;
  22.             }
  23.         }
  24.     }
  25.  
  26.     for (const [key, value] of Object.entries(bottlesOfJuices)) {
  27.       value > 0 ?  console.log(`${key} => ${value}`) : null;
  28.       }
  29. }
  30.  
  31. solve(['Kiwi => 234',
  32. 'Pear => 2345',
  33. 'Watermelon => 3456',
  34. 'Kiwi => 4567',
  35. 'Pear => 5678',
  36. 'Watermelon => 6789']
  37. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement