-1
let fruits = [mango,banana,avocado,apple,orange,lychee];
let prices = [50,90,65,300,600,900]; // not constant value;
//Solution with If else

if(prices > 0 && prices <= 50) console.log("Mango@0-50")
if(prices > 51 && prices <= 65)console.log("Mango@0-50<br>Banana@51-65")
//So on

Is there any way to short it with loop?

This is how the result should look like

Mango@0-50

Banana@51-65

avocado@65-90

apple@91-300

orange@301-600

lychee@601-900

rest@>901

Note: I do not want to use If else;

ANOL GHOSH
  • 1
  • 1
  • 9
  • 2
    What is the question? What have you tried? Where did you get stuck? SO expects a clear question along with some demonstration of effort. – Codebling Jan 19 '21 at 21:11
  • I have not tried anything at all as all my idea are not even close to it. this will work with if statement but I want to do it with Loop. If you understand my question, it will be very helpful. – ANOL GHOSH Jan 19 '21 at 21:14
  • That is what I am getting at. You should try to work on this problem before you ask a question on Stackoverflow. See [What topics can I ask about here?](https://stackoverflow.com/help/on-topic) – Codebling Jan 19 '21 at 21:15
  • @Codebling updated the question with the alter solution. but I need to do it with loop. – ANOL GHOSH Jan 19 '21 at 21:21
  • I gave a solution that loops through the values. Prices is presented in the question as a constant but with a comment that it is not constant, which is unclear. Perhaps your question is "How do I use a loop" ? – Codebling Jan 19 '21 at 21:26
  • For reference, have a look at [this question about loops](https://stackoverflow.com/questions/3010840/loop-through-an-array-in-javascript) – Codebling Jan 19 '21 at 21:34
  • 1
    let me do the work; your ans to my question did the half the work. thanks anyway. – ANOL GHOSH Jan 19 '21 at 21:35
  • what format do you want as result? – Nina Scholz Jan 19 '21 at 21:35
  • @NinaScholz Array; eg.['Mango@0-100','Banana@101-200',...]; – ANOL GHOSH Jan 19 '21 at 21:41
  • 1
    @Codebling I have updated question, please take a look. – ANOL GHOSH Jan 19 '21 at 21:50
  • @ANOL thanks for the update, I understand the problem a bit better now! Perhaps you can give it another try and if you get stuck post the code you've written and a specific question about why it's not working. This will get you both more attention and better help. :) Good luck! – Codebling Jan 19 '21 at 22:14

2 Answers2

1
let i = 1
fruits.map(fruit => `${fruit.name}@${i}-${i+=100}`);
Codebling
  • 10,764
  • 2
  • 38
  • 66
1

You could map the fruits with their price range and slice the array by the wanted length and return a joined string.

function getValues(price) {
    return temp
        .slice(0, (prices.findIndex(p => price <= p) + 1) || prices.length + 1)
        .join('<br>');
}

const
    fruits = ['mango', 'banana', 'avocado', 'apple', 'orange', 'lychee'],
    prices = [50, 90, 65, 300, 600, 900].sort((a, b) => a - b),
    temp = [...fruits.map((f, i, { length }) => `${f}@${prices[i - 1] + 1 || 0}-${prices[i]}`), `rest@>${prices[prices.length - 1] + 1}`];

console.log(getValues(100));
console.log(getValues(300));
console.log(getValues(301));
console.log(getValues(1000));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392