Skip to content

Instantly share code, notes, and snippets.

@geomago
Created June 13, 2020 14:30
Show Gist options
  • Save geomago/0193de584970749880ebb5f747c21827 to your computer and use it in GitHub Desktop.
Save geomago/0193de584970749880ebb5f747c21827 to your computer and use it in GitHub Desktop.
function findCheapest(orders) {
var cheapest = 999999999;
for (i=0; i<orders.length; i++) {
cheapest = Math.min(cheapest, findCheapestItem(orders[i].items) );
}
return cheapest;
}
function findCheapestItem(items) {
var cheapest = 999999999;
for (i=0; i<items.length; i++) {
cheapest = Math.min(cheapest,items[i].cost);
}
return cheapest;
}
var orders = [{id:1, items: [{cost:100.00},{cost: 95.00}]},
{id:2, items: [{cost:90.00},{cost: 120.00}]}
];
alert( findCheapest(orders) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment