1

I'm having a small project where I have to do a shopping list (for fruit salad) which allows user to add products and set prices for them. I've managed to do this so far that the "final" prompt shows all added products and prices for the user. Problem is that it should show all this from lowest to the highest by the price. I've tried to search information about this but haven't found anything relevant. Is there any help for me? Thank you in advance!

var count = 0;
var fruitSaladItems = [];
var ingredient=true;

 while (ingredient && count<22) {
      count++
  
      ingredient= prompt("Enter the fruit salad ingredients");
      if(ingredient){
        var price=prompt("Enter the price");
             fruitSaladItems.push(ingredient+" €"+price);
        
      }
}

alert(fruitSaladItems.join(','));

Here you can see the edited code:

var count = 0;
var fruitSaladItems = [];
var ingredient=true;

 while (ingredient && count<22) {
      count++
  
     ingredient = prompt("Enter the fruit salad ingredients");
     if (ingredient) {
      var price = parseFloat(prompt("Enter the price"));
      fruitSaladItems.push({ ingredient, price });
      
      }
}

alert(fruitSaladItems.join(','));
Micos15
  • 13
  • 3
  • 3
    Don't store items as `ingredient+" €"+price`. Store them as objects e.g. `{ingredient, price}` then you can easily [sort](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value) the list and can render/print the list in any custom way you want. – jarmod Jul 26 '22 at 16:23
  • I tried to store these things as objects and after that I tried to sort the answers by price but nothing happened. All the products and prices were not in order by price. Is there any solution for this? – Micos15 Jul 26 '22 at 19:56

1 Answers1

0

The key thing that you need to do to make this problem easier is to maintain a list of objects representing the ingredients rather than a list of interpolated strings such as "apple €1.50". When you store objects (containing the ingredient name and the price) you now have the source data in a form that is flexible and can be used for all kinds of things, such as sorting by ingredient name, sorting by price (ascending or descending), filtering on name or price, etc.

Generally, it's better to store and manipulate data in an elemental form and only transform that into something derived such as the description "apple €1.50" at the point you need to do that.

Here's an example that runs outside the browser and has pre-created data:

const fruitSaladItems = [];

fruitSaladItems.push({ ingredient: "grape", price: 5.2 });
fruitSaladItems.push({ ingredient: "apple", price: 3.99 });
fruitSaladItems.push({ ingredient: "orange", price: 1.3 });
fruitSaladItems.push({ ingredient: "plum", price: 1.5 });

fruitSaladItems.sort((a, b) => a.price - b.price);

const descriptions = fruitSaladItems.map(
  (item) => `${item.ingredient} €${item.price.toFixed(2)}`
);

console.log(descriptions.join(", "));

In your code, to populate the items array, you could use:

let count = 0;
const fruitSaladItems = [];
let ingredient=true;

while (ingredient && count<22) {
  count++
  ingredient = prompt("Enter the fruit salad ingredients");
  if (ingredient) {
    const price = parseFloat(prompt("Enter the price"));
    fruitSaladItems.push({ ingredient, price });
  }
}

fruitSaladItems.sort((a, b) => a.price - b.price);

const descriptions = fruitSaladItems.map(
  (item) => `${item.ingredient} €${item.price.toFixed(2)}`
);

console.log(descriptions.join(", "));
jarmod
  • 71,565
  • 16
  • 115
  • 122
  • I changed my code to the code above and now it gives (object Object) answers only. I already tried to see the answers by using JSON.stringify() method but answers still look the same. What I'm doing wrong with this? – Micos15 Jul 27 '22 at 16:36
  • Are you saying that your code now pushes objects of the form `{ingredient, price}` to the `fruitSaladItems` array and you're then using the `map` code I showed to construct and print a comma-separated list of ingredients with prices? Perhaps you could add to your original post above to show the current code and current problematic output. – jarmod Jul 27 '22 at 16:42
  • I edited the original post. I'm sorry for being such a newbie with this. – Micos15 Jul 27 '22 at 17:55
  • You didn't use the majority of the code from my posted solution, specifically the sort and the map steps (basically everything from the sort step downwards). Right now you're just joining stringized objects, which is why you see "(object Object)". – jarmod Jul 27 '22 at 18:03
  • Updated answer to make it clearer. – jarmod Jul 27 '22 at 18:14