Skip to content

Instantly share code, notes, and snippets.

@sambatlim
Created April 19, 2020 08:12
Show Gist options
  • Save sambatlim/4b2b93f98a2428fa10215381ec2cbb94 to your computer and use it in GitHub Desktop.
Save sambatlim/4b2b93f98a2428fa10215381ec2cbb94 to your computer and use it in GitHub Desktop.
var utility = ['pink bag','pink book','pink pen','pink shirt','pink shoe'];
var shop = [{'shopname':'aa shop','sell':'pink bag','price':30000,'distance':2},{'shopname':'bb shop','sell':'pink shirt','price':10000,'distance':3},{'shopname':'cc shop','sell':'pink shoe','price':15000,'distance':1},{'shopname':'dd shop','sell':'pink skirt','price':25000,'distance':0.5}]
var how ={'grab':5000,'own motor':2000};
var moneyihave = 50000;
var findperfectgift = async(moneyihave)=>{
findshop(moneyihave,(whatshop,whattobuy,moneyileft)=>{
findhow(moneyileft,(moneytopay,howigo)=>{
moneyfinallytopay = moneyihave - moneytopay;
bestoption = 'buy: '+whattobuy+' ,at: '+whatshop+' ,go by: '+ howigo+' all you need to pay: '+moneyfinallytopay;
return bestoption;
})
});
}
var findshop=(moneyihave,callback)=>{
var objectofchoice = new Object;
shop.forEach(element => {
if(element.price<moneyihave){
objectofchoice[element.shopname]=element.price * element.distance;
}
});
console.log(objectofchoice);
var recentlowest = objectofchoice[Object.keys(objectofchoice)[0]];
var shoplowest;
for( const i in objectofchoice){
if (recentlowest>objectofchoice[i]){
recentlowest = objectofchoice[i];
shoplowest = i;
}
};
var whattobuy, moneythatleft;
shop.forEach(element=>{
if(element.shopname==shoplowest){
whattobuy = element.sell;
moneythatleft = moneyihave - element.price;
}
});
callback(shoplowest,whattobuy,moneythatleft);
};
var findhow=(moneyileft,callback)=>{
var recentlowest = how[Object.keys(how)[0]];
var howlowest,moneythatifinallyleft;
for(const i in how){
if(recentlowest>how[i]){
recentlowest = how[i];
howlowest = i;
moneythatifinallyleft = moneyileft - recentlowest;
}
}
console.log(moneythatifinallyleft);
callback(moneythatifinallyleft,howlowest);
};
findperfectgift(moneyihave).then(console.log(bestoption));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment