I am working with an array
var prices = ['100','200','300','500'];
In my code, I am trying to insert a price if certain conditions are met.
var index = prices.indexOf(400);
if(index){
prices.splice(1, 0, '150');
};
var index2 = prices.indexOf(200);
if(index2){
prices.splice(3, 0, '600');
};
One challenge I am facing is that I need to insert the value '600' after '300', but the index changes if the first condition is met. How can I ensure '600' gets added after '300' in this scenario?
Update
If the first condition occurs, the updated array should be:
// ['100','150','200','300','600','500']
If not,
// ['100','200','300','600','500']