A list of values
Une liste de valeurs
An array holds several values, in order, between square brackets. Each value has a number, its index, and you count from 0!
Un tableau range plusieurs valeurs, dans l’ordre, entre crochets. Chaque valeur a un numéro, son indice, et on compte à partir de 0!
Going through an array
Parcourir un tableau
A loop can visit every value: with the index i, from 0 to length - 1, or more simply with for (const … of …), which hands over each value in turn.
Une boucle peut visiter chaque valeur : avec l’indice i, de 0 à length - 1, ou plus simplement avec for (const … of …), qui donne chaque valeur à tour de rôle.
You can also fill an array in a loop: start with an empty array, [], and push a value at each turn.
On peut aussi remplir un tableau dans une boucle : on part d’un tableau vide, [], et on ajoute une valeur à chaque tour avec push.
Objects
Les objets
An object holds several values too, but each one has a name, called a property, instead of a number. You read a property with a dot: pupil.name.
Un objet range lui aussi plusieurs valeurs, mais chacune a un nom, appelé propriété, au lieu d’un numéro. On lit une propriété avec un point : eleve.prenom.
When a variable already has the name of the property, { x } is enough for { x: x }. The code of Euclid’s algorithm uses it to record each step.
Quand une variable porte déjà le nom de la propriété, { x } suffit pour { x: x }. Le code de l’algorithme d’Euclide s’en sert pour garder chaque étape.
Exercises
Exercices
As in the last lesson, write functions that return their answer. Each editor is separate: to reuse a function from another exercise, copy it.
Comme à la leçon précédente, écris des fonctions qui renvoient leur réponse. Chaque éditeur est indépendant : pour réutiliser une fonction d’un autre exercice, recopie-la.
The sum
La somme
Write a function sum(numbers) that returns the sum of the numbers in the array (0 for an empty array).
Écris une fonction somme(tableau) qui renvoie la somme des nombres du tableau (0 pour un tableau vide).
The largest
Le maximum
Write a function largest(numbers) that returns the largest number in the array. The array is never empty.
Écris une fonction maximum(tableau) qui renvoie le plus grand nombre du tableau. Le tableau n’est jamais vide.
The divisors
Les diviseurs
Write a function divisors(n) that returns the array of the divisors of n, from the smallest to the biggest. For example, divisors(12) is [1, 2, 3, 4, 6, 12].
Écris une fonction diviseurs(n) qui renvoie le tableau des diviseurs de n, du plus petit au plus grand. Par exemple, diviseurs(12) vaut [1, 2, 3, 4, 6, 12].
Common divisors
Les diviseurs communs
Write a function common(a, b) that returns the array of the numbers that divide both a and b, from the smallest to the biggest. The last one is… the greatest common divisor!
Écris une fonction communs(a, b) qui renvoie le tableau des nombres qui divisent à la fois a et b, du plus petit au plus grand. Le dernier est… le plus grand commun diviseur!