Strategy Game Example
let playerResources = 50;
function gatherResources(gathered) {
playerResources += gathered;
console.log(`You have gathered ${gathered} resources. Your total is now ${playerResources}.`);
}
gatherResources(20); // Outputs "You have gathered 20 resources. Your total is now 70."
gatherResources(30); // Outputs "You have gathered 30 resources. Your total is now 100."
let unitCost = 10;
let unitsToCreate = 3;
if (playerResources >= unitCost * unitsToCreate) {
playerResources -= unitCost * unitsToCreate;
console.log(`You have created ${unitsToCreate} units. Your resources are now ${playerResources}.`);
} else {
console.log(`You do not have enough resources to create ${unitsToCreate} units.`);
}Last updated