> For the complete documentation index, see [llms.txt](https://demirels-organization.gitbook.io/javascript-tutorial/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://demirels-organization.gitbook.io/javascript-tutorial/plus-+-operator/strategy-game-example.md).

# Strategy Game Example

Here is an example of how the `+` and `+=` operators might be used in the context of a strategy game:

```javascript
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.`);
}
```

In this example, we have a variable called `playerResources` that stores the number of resources that the player has. We have a function called `gatherResources` that adds a given amount of resources to the player's total.

We also have a few variables called `unitCost` and `unitsToCreate` that store the cost and number of units that the player wants to create. We use the `+` operator to add the gathered resources to the player's total, and we use the `+=` operator to update the player's total after units are created.

Finally, we use an `if` statement to check if the player has enough resources to create the desired number of units. If they do, we use the `*` operator to calculate the total cost and the `-=` operator to subtract the cost from the player's resources. If they don't have enough resources, we display a message to the player


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://demirels-organization.gitbook.io/javascript-tutorial/plus-+-operator/strategy-game-example.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
