maymunskoa

09. Catalogue (Objects&Classes Exercise)

Mar 3rd, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2. let output = [];
  3.     for (const line of input) {
  4.         let[product, price] = line.split(' : ');
  5.         let singleObj = {product,price};
  6.        
  7.         output.push(singleObj);
  8.     }
  9.    
  10.     output.sort((a,b)=>a.product.localeCompare(b.product));
  11.     let firstLetter = output.map(item=>{
  12.         return item.product[0];
  13.  
  14.     });
  15.  
  16.     let uniqFirstLetter = [];
  17.     for (const letter of firstLetter) {
  18.             if(!uniqFirstLetter.includes(letter)){
  19.                 uniqFirstLetter.push(letter);
  20.             }
  21.     }
  22.  
  23.     for (const object of output) {
  24.         let first = object.product[0];
  25.         if(first === uniqFirstLetter[0]){
  26.             console.log(uniqFirstLetter.shift());
  27.         }
  28.         console.log(`  ${object.product}: ${object.price}`);
  29.     }
  30.  
  31. }
Add Comment
Please, Sign In to add comment