Skip to content

Instantly share code, notes, and snippets.

@DavertMik
Created March 9, 2022 20:59
Show Gist options
  • Save DavertMik/4b1fbdbd2ee7ad4ea489854c59aa6b63 to your computer and use it in GitHub Desktop.
Save DavertMik/4b1fbdbd2ee7ad4ea489854c59aa6b63 to your computer and use it in GitHub Desktop.
const inventory = [
{ name: 'sword', weight: 3},
{ name: 'pickaxe', weight: 2},
{ name: 'bow', weight: 1},
]
var inventoryWeight = 0;
function addToInventory(item) {
// if weight of items in inventory
// less than 10
inventoryWeight = 0;
for (let i = 0; i < inventory.length; i++) {
inventoryWeight += inventory[i].weight;
}
if (inventoryWeight + item.weight > 10) {
console.log('Мені важко!')
return;
}
inventory.push(item)
}
console.log('weight', inventoryWeight, inventory);
addToInventory({ name: 'axe', weight: 1000 })
console.log('weight', inventoryWeight, inventory);
addToInventory({ name: 'knife', weight: 1 })
console.log('weight', inventoryWeight, inventory);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment