Lists (JavaScript)

Lists

> Tutorial

A list is used to store several values in a specific order.

Creating and using a list

You can create a list with the following syntax:

The first position in a list is position 0. You access a specific element of the list with:

Many operations are available for handling lists. The most notable are:

Iterating over a list with the for of loop

You can iterate over each of the values in a list using a simple for loop, like this:

But there is a variant of the for loop which makes it possible to iterate over each of the values of a list more simply, the for of loop. The following code is equivalent to the previous one but is more concise:

Weapon damage rating and finisher condition

Our current AI has a problem: when the enemy is almost dead, our leek will use its boosts, heals and shields before using its weapon, whereas it could finish the enemy!

To improve this situation at the end of the fight, we will estimate the damage that we can inflict on the enemy to know if we can finish it.

We start by using me.weapon.features to get the list of the features of our weapon:

Each feature has properties: .type (the effect type), .minValue (minimum value), .maxValue (maximum value)... We calculate the final damage when the feature is damage:

Add the damage to the total:

We end with a condition: if the total damage is greater than the life of the enemy, we will kill him: we attack! Otherwise, we wait with chips:

> Tip: you can extract a range of a list with list.slice(2, 8).

> Tip: you can nest lists: a list in a list [ [1, 2, 3] ], and access a list in a list: list[0][0].

❓ Quiz

What is a list for?

Store multiple values Sort values Execute instructions in parallel Perform multiple calculations

Which codes are valid?

let a = [1, 2, 3] a[0] + a[2] let [1, 2, 3] [1, 2, 3].length [ [1, 2], [3, 4], [5, 6] ] list[5][10]

Full AI