Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// canShop([
// { product: "Milk", quantity: 1, price: 1.50 },
// { product: "Cereals", quantity: 1, price: 2.50 }
// ], 5) ➞ true // because we have sufficient amount to purchase all the grocery items.
// canShop([
// { product: "Milk", quantity: 1, price: 1.50 },
// { product: "Eggs", quantity: 12, price: 0.10 },
// { product: "Bread", quantity: 2, price: 1.60 },
// { product: "Cheese", quantity: 1, price: 4.50 }
// ], 10) ➞ false // As we donot have sufficient amount to purchase all the grocery items.
function canShop(arr, x){
let total = 0;
for(let i=0 ; i < arr.length; i++ ){
total+=arr[i].price;
}
if (total <= x){
return 'true'
}else{
return 'false'
}
}
console.log(canShop([
{ product: "Milk", quantity: 1, price: 1.50 },
{ product: "Eggs", quantity: 12, price: 0.10 },
{ product: "Bread", quantity: 2, price: 1.60 },
{ product: "Cheese", quantity: 1, price: 4.50 }
], 3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment