Skip to content

Instantly share code, notes, and snippets.

@AsifITk
Created November 25, 2021 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AsifITk/3fa83a880a07f9705a8e8b3adb847315 to your computer and use it in GitHub Desktop.
Save AsifITk/3fa83a880a07f9705a8e8b3adb847315 to your computer and use it in GitHub Desktop.
// 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