Created
March 9, 2022 20:59
-
-
Save DavertMik/4b1fbdbd2ee7ad4ea489854c59aa6b63 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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