Additional exercises – Sharpening the axe
- Write a function,
limit_inventory()
, that takes an array representing an inventory and an integer. The function checks if the array is longer than the provided integer; if it is, it should remove all items that are too much. Lastly, the function returns the resulting array:var inventory: Array = ["Boots", "Sword", "Grapes", "Cuffs", "Potion"] var limited_inventory: Array = limit_inventory(inventory, 2) print(limited_inventory)
This example should print out
["
Boots", "Sword"]
. - Rewrite the previous function so that the integer it takes has a default value of 3 for the following code to work:
var inventory: Array = ["Boots", "Sword", "Grapes", "Cuffs", "Potion"] var limited_inventory: Array = limit_inventory(inventory) print(limited_inventory)
This should print out
["Boots", "
Sword", "Grapes...