Advertisement
vladovip

09.Catalogue_JS Objects Ex

Feb 26th, 2022
1,069
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function orderedCatalogue(array){
  2.  
  3.     let catalogue = [];
  4.     for (let i = 0; i < array.length; i++) {
  5.         let elements = array[i].split(' : ');
  6.         // console.log(elements);
  7.         let product = {
  8.             name: elements[0],
  9.             price: parseFloat(elements[1])
  10.         };
  11.        
  12.         catalogue.push(product);
  13.     }
  14.  
  15.    
  16.     catalogue.sort((a,b) => a.name.localeCompare(b.name));
  17.    
  18.    
  19.     let currentLetter = '';
  20.     for (const product of catalogue) {
  21.         if (product.name.charAt(0).toUpperCase() === currentLetter){
  22.             console.log(`  ${product.name}: ${product.price}`)
  23.         }else {
  24.             currentLetter = product.name.charAt(0).toUpperCase();
  25.             console.log(currentLetter);
  26.             console.log(`  ${product.name}: ${product.price}`)
  27.  
  28.         }
  29.     }
  30. }
  31. orderedCatalogue (
  32.     [
  33.     'Appricot : 20.4',
  34.     'Fridge : 1500',
  35.     'TV : 1499',
  36.     'Deodorant : 10',
  37.     'Boiler : 300',
  38.     'Apple : 1.25',
  39.     'Anti-Bug Spray : 15',
  40.     'T-Shirt : 10',
  41. ]);
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement