Exercices

Pour réaliser ces exercices, vous utiliserez l'interface HTML fournie (index.html) reliée au fichier JavaScript (script.js). N'ajoutez pas de balises HTML statiques : appuyez-vous sur les identifiants (id) et événements déjà présents dans le document. Pour observer les résultats dans la console, utilisez la touche F12 ou Ctrl+Maj+I.

1
<!doctype html>
2
<html lang="fr">
3
  <head>
4
    <meta charset="UTF-8" />
5
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
    <title>Exercices JavaScript - Partie 3</title>
7
    <script src="https://cdn.tailwindcss.com"></script>
8
    <script src="script.js" defer></script>
9
  </head>
10
  <body class="bg-slate-100 font-sans p-6 text-slate-800">
11
    <header class="max-w-4xl mb-4 mx-auto space-y-6">
12
      <h1 class="text-3xl font-bold text-slate-900 mb-2">
13
        Exercices JavaScript — Partie 3
14
      </h1>
15
      <p class="text-slate-600 text-sm">
16
        Fonctions fléchées, Tableaux, Rest/Spread et Manipulation de chaînes.
17
      </p>
18
    </header>
19
    <main class="max-w-4xl mx-auto space-y-6">
20
      <!-- Ex 1 -->
21
      <section
22
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
23
      >
24
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
25
          Exercice 1 : Fonction fléchée d'accueil
26
        </h2>
27
        <form
28
          onsubmit="return false;"
29
          class="flex flex-wrap gap-3 items-center"
30
        >
31
          <input
32
            type="text"
33
            id="prenomUtilisateur"
34
            placeholder="Prénom"
35
            class="border border-slate-300 rounded-lg px-3 py-2 w-48 focus:outline-none focus:ring-2 focus:ring-blue-500"
36
          />
37
          <button
38
            type="button"
39
            onclick="afficherAccueil()"
40
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
41
          >
42
            Saluer
43
          </button>
44
        </form>
45
        <output
46
          id="resultatAccueil"
47
          class="block mt-3 font-semibold text-slate-700 min-h-[1.5rem]"
48
        ></output>
49
      </section>
50
51
      <!-- Ex 2 -->
52
      <section
53
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
54
      >
55
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
56
          Exercice 2 : Calculateur de surface (Retour explicite)
57
        </h2>
58
        <form
59
          onsubmit="return false;"
60
          class="flex flex-wrap gap-3 items-center"
61
        >
62
          <input
63
            type="number"
64
            id="largeur"
65
            placeholder="Largeur (m)"
66
            class="border border-slate-300 rounded-lg px-3 py-2 w-36 focus:outline-none focus:ring-2 focus:ring-blue-500"
67
          />
68
          <input
69
            type="number"
70
            id="longueur"
71
            placeholder="Longueur (m)"
72
            class="border border-slate-300 rounded-lg px-3 py-2 w-36 focus:outline-none focus:ring-2 focus:ring-blue-500"
73
          />
74
          <button
75
            type="button"
76
            onclick="afficherSurface()"
77
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
78
          >
79
            Calculer
80
          </button>
81
        </form>
82
        <output
83
          id="resultatSurface"
84
          class="block mt-3 font-semibold text-slate-700 min-h-[1.5rem]"
85
        ></output>
86
      </section>
87
88
      <!-- Ex 3 -->
89
      <section
90
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
91
      >
92
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
93
          Exercice 3 : Conversion de température (Retour implicite)
94
        </h2>
95
        <form
96
          onsubmit="return false;"
97
          class="flex flex-wrap gap-3 items-center"
98
        >
99
          <input
100
            type="number"
101
            id="tempCelsius"
102
            placeholder="Température °C"
103
            class="border border-slate-300 rounded-lg px-3 py-2 w-40 focus:outline-none focus:ring-2 focus:ring-blue-500"
104
          />
105
          <button
106
            type="button"
107
            onclick="convertirTemperature()"
108
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
109
          >
110
            Convertir en °F
111
          </button>
112
        </form>
113
        <output
114
          id="resultatTemperature"
115
          class="block mt-3 font-semibold text-slate-700 min-h-[1.5rem]"
116
        ></output>
117
      </section>
118
119
      <!-- Ex 4 -->
120
      <section
121
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
122
      >
123
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
124
          Exercice 4 : Liste de courses (Copie et ajout via Spread `...`)
125
        </h2>
126
        <p class="text-sm text-slate-500 mb-3">
127
          Tableau initial :
128
          <code class="bg-slate-100 px-2 py-1 rounded">['Pain', 'Lait']</code>
129
        </p>
130
        <form
131
          onsubmit="return false;"
132
          class="flex flex-wrap gap-3 items-center"
133
        >
134
          <input
135
            type="text"
136
            id="articleSaisi"
137
            placeholder="Article (ex: Pommes)"
138
            class="border border-slate-300 rounded-lg px-3 py-2 w-48 focus:outline-none focus:ring-2 focus:ring-blue-500"
139
          />
140
          <button
141
            type="button"
142
            onclick="ajouterArticle()"
143
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
144
          >
145
            Ajouter via Spread
146
          </button>
147
        </form>
148
        <output
149
          id="affichageCourses"
150
          class="block mt-3 min-h-[1.5rem]"
151
        ></output>
152
      </section>
153
154
      <!-- Ex 5 -->
155
      <section
156
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
157
      >
158
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
159
          Exercice 5 : Premier et dernier élément
160
        </h2>
161
        <p class="text-sm text-slate-500 mb-3">
162
          Tableau :
163
          <code class="bg-slate-100 px-2 py-1 rounded"
164
            >['Alice', 'Bob', 'Charlie', 'Diana']</code
165
          >
166
        </p>
167
        <button
168
          type="button"
169
          onclick="afficherExtremes()"
170
          class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
171
        >
172
          Afficher 1er et Dernier
173
        </button>
174
        <output
175
          id="resultatExtremes"
176
          class="block mt-3 min-h-[1.5rem]"
177
        ></output>
178
      </section>
179
180
      <!-- Ex 6 -->
181
      <section
182
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
183
      >
184
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
185
          Exercice 6 : Parcours de tableau (Boucle `for` & `innerHTML`)
186
        </h2>
187
        <p class="text-sm text-slate-500 mb-3">
188
          Notes :
189
          <code class="bg-slate-100 px-2 py-1 rounded"
190
            >[12, 15, 8, 19, 10]</code
191
          >
192
        </p>
193
        <button
194
          type="button"
195
          onclick="afficherNotes()"
196
          class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
197
        >
198
          Afficher les notes
199
        </button>
200
        <output id="affichageNotes" class="block mt-3 min-h-[2.5rem]"></output>
201
      </section>
202
203
      <!-- Ex 7 -->
204
      <section
205
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
206
      >
207
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
208
          Exercice 7 : Somme variable de valeurs (Paramètre Rest `...`)
209
        </h2>
210
        <p class="text-sm text-slate-500 mb-3">
211
          Envoie d'un nombre variable de notes à une fonction via `...notes`.
212
        </p>
213
        <button
214
          type="button"
215
          onclick="calculerSommeRest()"
216
          class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
217
        >
218
          Calculer la somme (12, 15, 8, 19, 10)
219
        </button>
220
        <output
221
          id="resultatMoyenneTableau"
222
          class="block mt-3 min-h-[1.5rem]"
223
        ></output>
224
      </section>
225
226
      <!-- Ex 8 -->
227
      <section
228
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
229
      >
230
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
231
          Exercice 8 : Recherche de la valeur maximale (Boucle `for`)
232
        </h2>
233
        <p class="text-sm text-slate-500 mb-3">
234
          Valeurs :
235
          <code class="bg-slate-100 px-2 py-1 rounded"
236
            >[45, 12, 89, 34, 67, 2]</code
237
          >
238
        </p>
239
        <button
240
          type="button"
241
          onclick="chercherMaximum()"
242
          class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
243
        >
244
          Trouver le max
245
        </button>
246
        <output id="resultatMax" class="block mt-3 min-h-[1.5rem]"></output>
247
      </section>
248
249
      <!-- Ex 9 -->
250
      <section
251
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
252
      >
253
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
254
          Exercice 9 : Mise en majuscules (.toUpperCase)
255
        </h2>
256
        <form
257
          onsubmit="return false;"
258
          class="flex flex-wrap gap-3 items-center"
259
        >
260
          <input
261
            type="text"
262
            id="texteMinuscule"
263
            placeholder="Saisir un texte"
264
            class="border border-slate-300 rounded-lg px-3 py-2 w-64 focus:outline-none focus:ring-2 focus:ring-blue-500"
265
          />
266
          <button
267
            type="button"
268
            onclick="convertirMajuscules()"
269
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
270
          >
271
            Transformer
272
          </button>
273
        </form>
274
        <output
275
          id="resultatMajuscules"
276
          class="block mt-3 min-h-[1.5rem]"
277
        ></output>
278
      </section>
279
280
      <!-- Ex 10 -->
281
      <section
282
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
283
      >
284
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
285
          Exercice 10 : Comptage de voyelles
286
        </h2>
287
        <form
288
          onsubmit="return false;"
289
          class="flex flex-wrap gap-3 items-center"
290
        >
291
          <input
292
            type="text"
293
            id="motVoyelles"
294
            placeholder="Mot ou phrase"
295
            class="border border-slate-300 rounded-lg px-3 py-2 w-64 focus:outline-none focus:ring-2 focus:ring-blue-500"
296
          />
297
          <button
298
            type="button"
299
            onclick="compterVoyelles()"
300
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
301
          >
302
            Compter voyelles
303
          </button>
304
        </form>
305
        <output
306
          id="resultatVoyelles"
307
          class="block mt-3 min-h-[1.5rem]"
308
        ></output>
309
      </section>
310
311
      <!-- Ex 11 -->
312
      <section
313
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
314
      >
315
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
316
          Exercice 11 : Inversion de chaîne (Boucle `for` à l'envers)
317
        </h2>
318
        <form
319
          onsubmit="return false;"
320
          class="flex flex-wrap gap-3 items-center"
321
        >
322
          <input
323
            type="text"
324
            id="motAInverser"
325
            placeholder="Mot à inverser"
326
            class="border border-slate-300 rounded-lg px-3 py-2 w-48 focus:outline-none focus:ring-2 focus:ring-blue-500"
327
          />
328
          <button
329
            type="button"
330
            onclick="inverserChaine()"
331
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
332
          >
333
            Inverser
334
          </button>
335
        </form>
336
        <output
337
          id="resultatInversion"
338
          class="block mt-3 min-h-[1.5rem]"
339
        ></output>
340
      </section>
341
342
      <!-- Ex 12 -->
343
      <section
344
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
345
      >
346
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
347
          Exercice 12 : Recherche dans un tableau (.includes)
348
        </h2>
349
        <p class="text-sm text-slate-500 mb-3">
350
          Villes :
351
          <code class="bg-slate-100 px-2 py-1 rounded"
352
            >['Paris', 'Lyon', 'Marseille', 'Lille']</code
353
          >
354
        </p>
355
        <form
356
          onsubmit="return false;"
357
          class="flex flex-wrap gap-3 items-center"
358
        >
359
          <input
360
            type="text"
361
            id="villeCherchee"
362
            placeholder="Nom de ville"
363
            class="border border-slate-300 rounded-lg px-3 py-2 w-48 focus:outline-none focus:ring-2 focus:ring-blue-500"
364
          />
365
          <button
366
            type="button"
367
            onclick="rechercherVille()"
368
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
369
          >
370
            Rechercher
371
          </button>
372
        </form>
373
        <output
374
          id="resultatRechercheVille"
375
          class="block mt-3 min-h-[1.5rem]"
376
        ></output>
377
      </section>
378
379
      <!-- Ex 13 -->
380
      <section
381
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
382
      >
383
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
384
          Exercice 13 : Suppression du dernier élément (.pop)
385
        </h2>
386
        <p class="text-sm text-slate-500 mb-3">
387
          Supprime le dernier article de la liste de l'exercice 4.
388
        </p>
389
        <button
390
          type="button"
391
          onclick="retirerDernierArticle()"
392
          class="bg-rose-600 hover:bg-rose-700 text-white font-medium px-4 py-2 rounded-lg transition"
393
        >
394
          Retirer le dernier article
395
        </button>
396
        <output id="resultatPop" class="block mt-3 min-h-[1.5rem]"></output>
397
      </section>
398
399
      <!-- Ex 14 -->
400
      <section
401
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
402
      >
403
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
404
          Exercice 14 : Filtrage manuel de tableau (`for` & `if`)
405
        </h2>
406
        <p class="text-sm text-slate-500 mb-3">
407
          Source :
408
          <code class="bg-slate-100 px-2 py-1 rounded"
409
            >[5, 12, 8, 130, 44]</code
410
          >
411
        </p>
412
        <button
413
          type="button"
414
          onclick="filtrerGrandNombres()"
415
          class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
416
        >
417
          Filtrer (> 10)
418
        </button>
419
        <output id="resultatFiltre" class="block mt-3 min-h-[1.5rem]"></output>
420
      </section>
421
422
      <!-- Ex 15 -->
423
      <section
424
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
425
      >
426
        <h2 class="text-lg font-semibold text-slate-800 mb-4">
427
          Exercice 15 : Synthèse — Annuaire (Rest, Spread & `innerHTML`)
428
        </h2>
429
        <form
430
          onsubmit="return false;"
431
          class="flex flex-wrap gap-3 items-center mb-3"
432
        >
433
          <input
434
            type="text"
435
            id="nomContact"
436
            placeholder="Nom principal"
437
            class="border border-slate-300 rounded-lg px-3 py-2 w-48 focus:outline-none focus:ring-2 focus:ring-blue-500"
438
          />
439
          <button
440
            type="button"
441
            onclick="ajouterPlusieursContacts('Lucie', 'Marc')"
442
            class="bg-emerald-600 hover:bg-emerald-700 text-white font-medium px-4 py-2 rounded-lg transition"
443
          >
444
            Ajouter (Saisi + Lucie & Marc)
445
          </button>
446
          <button
447
            type="button"
448
            onclick="reinitialiserAnnuaire()"
449
            class="bg-slate-600 hover:bg-slate-700 text-white font-medium px-4 py-2 rounded-lg transition"
450
          >
451
            Réinitialiser
452
          </button>
453
        </form>
454
        <output id="affichageAnnuaire" class="block mt-3 min-h-[3rem]"></output>
455
      </section>
456
    </main>
457
  </body>
458
</html>
459

SyntaxeExercice 1 : Fonction fléchée d'accueil

  • Créez une fonction fléchée afficherAccueil.

  • Récupérez la saisie du champ d'identifiant prenomUtilisateur.

  • Affichez le message d'accueil personnalisé dans l'élément resultatAccueil à l'aide de innerHTML.

SyntaxeExercice 2 : Calculateur de surface (Retour explicite)

  • Créez une fonction fléchée calculerSurface prenant deux paramètres (largeur et longueur) et retournant leur produit avec l'instruction return.

  • Récupérez les valeurs des champs largeur et longueur.

  • Affichez le résultat mis en forme via innerHTML dans l'élément resultatSurface.

SyntaxeExercice 3 : Conversion de température (Retour implicite)

  • Créez une fonction fléchée concise celsiusVersFahrenheit sur une seule ligne (retour implicite) avec la formule : (C * 9/5) + 32.

  • Lisez la valeur du champ tempCelsius.

  • Affichez la valeur convertie dans l'élément resultatTemperature.

SyntaxeExercice 4 : Liste de courses (Opérateur Spread ...)

  • Déclarez un tableau listeCourses initialisé avec des articles de base.

  • Ajoutez l'article saisi dans articleSaisi en créant un nouveau tableau à l'aide de l'opérateur spread ([...listeCourses, nouvelArticle]).

  • Générez une liste HTML (<ul>) affichée dans affichageCourses avec innerHTML.

SyntaxeExercice 5 : Premier et dernier élément (Indices de tableau)

  • Soit le tableau : ['Alice', 'Bob', 'Charlie', 'Diana'].

  • Accédez au premier élément via l'indice [0] et au dernier via [length - 1].

  • Affichez ces deux valeurs dans l'élément d'identifiant resultatExtremes.

SyntaxeExercice 6 : Parcours de tableau (Boucle for & innerHTML)

  • Soit le tableau de notes : [12, 15, 8, 19, 10].

  • Parcourez le tableau à l'aide d'une boucle for classique.

  • Construisez une structure HTML rendue dans le conteneur affichageNotes.

SyntaxeExercice 7 : Somme variable de valeurs (Paramètre Rest ...)

  • Créez une fonction fléchée calculerSomme acceptant un nombre indéfini d'arguments grâce au paramètre rest (...valeurs).

  • Additionnez toutes les valeurs à l'aide d'une boucle for.

  • Passez les éléments d'un tableau à la fonction grâce à l'opérateur spread et affichez le résultat dans resultatMoyenneTableau.

SyntaxeExercice 8 : Recherche de la valeur maximale (Boucle for)

  • Considérez le tableau de nombres : [45, 12, 89, 34, 67, 2].

  • Parcourez le tableau avec une boucle for et une condition if pour trouver la plus grande valeur.

  • Affichez le maximum trouvé dans l'élément resultatMax.

SyntaxeExercice 9 : Mise en majuscules (.toUpperCase)

  • Récupérez le texte saisi dans le champ texteMinuscule.

  • Transformez la chaîne en majuscules avec la méthode .toUpperCase().

  • Affichez la chaîne modifiée dans l'élément resultatMajuscules.

SyntaxeExercice 10 : Comptage de voyelles

  • Lisez la chaîne saisie dans le champ motVoyelles.

  • Parcourez les caractères et comptez les voyelles en vérifiant leur présence à l'aide de .includes().

  • Affichez le total obtenu dans l'élément resultatVoyelles.

SyntaxeExercice 11 : Inversion de chaîne de caractères (Boucle for décrémentale)

  • Récupérez la chaîne saisie dans le champ motAInverser.

  • À l'aide d'une boucle for décrémentale (partant de l'indice texte.length - 1 jusqu'à 0), reconstruisez la chaîne à l'envers.

  • Affichez la chaîne inversée dans l'élément resultatInversion.

SyntaxeExercice 12 : Recherche dans un tableau (.includes)

  • Soit le tableau de villes : ['Paris', 'Lyon', 'Marseille', 'Lille'].

  • Vérifiez la présence de la ville saisie dans villeCherchee à l'aide de la méthode .includes().

  • Affichez un message adapté dans l'élément resultatRechercheVille.

SyntaxeExercice 13 : Suppression du dernier élément (.pop)

  • Utilisez le tableau listeCourses de l'exercice 4.

  • Retirez le dernier élément du tableau à l'aide de .pop().

  • Mettez à jour le message d'information dans resultatPop ainsi que la liste HTML affichée.

SyntaxeExercice 14 : Filtrage manuel de tableau (for & if)

  • Considérez le tableau source : [5, 12, 8, 130, 44].

  • Parcourez-le avec une boucle for et construisez un nouveau tableau contenant uniquement les nombres strictement supérieurs à 10 via .push().

  • Affichez le résultat filtré dans l'élément resultatFiltre.

1
<!doctype html>
2
<html lang="fr">
3
  <head>
4
    <meta charset="UTF-8" />
5
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
    <title>Exercices JavaScript - Partie 3</title>
7
    <script src="https://cdn.tailwindcss.com"></script>
8
    <!-- Inclusion du script unique regroupant tous les exercices -->
9
    <script src="script.js" defer></script>
10
  </head>
11
  <body class="bg-slate-100 text-slate-800 antialiased py-10 px-4 sm:px-8">
12
    <div class="max-w-4xl mx-auto space-y-6">
13
      <header class="text-center mb-8">
14
        <h1 class="text-3xl font-bold text-slate-900">
15
          Exercices JavaScript — Partie 3
16
        </h1>
17
        <p class="text-slate-600 mt-2">
18
          Fonctions fléchées, Tableaux, Rest/Spread et Manipulation de chaînes.
19
        </p>
20
      </header>
21
22
      <!-- Ex 1 -->
23
      <section
24
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
25
      >
26
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
27
          Exercice 1 : Fonction fléchée d'accueil
28
        </h2>
29
        <div class="flex flex-wrap gap-3 items-center">
30
          <input
31
            type="text"
32
            id="prenomUtilisateur"
33
            placeholder="Prénom"
34
            class="border border-slate-300 rounded-lg px-3 py-2 w-48 focus:outline-none focus:ring-2 focus:ring-blue-500"
35
          />
36
          <button
37
            onclick="afficherAccueil()"
38
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
39
          >
40
            Saluer
41
          </button>
42
        </div>
43
        <div
44
          id="resultatAccueil"
45
          class="mt-3 font-semibold text-slate-700 min-h-[1.5rem]"
46
        ></div>
47
      </section>
48
49
      <!-- Ex 2 -->
50
      <section
51
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
52
      >
53
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
54
          Exercice 2 : Calculateur de surface (Retour explicite)
55
        </h2>
56
        <div class="flex flex-wrap gap-3 items-center">
57
          <input
58
            type="number"
59
            id="largeur"
60
            placeholder="Largeur (m)"
61
            class="border border-slate-300 rounded-lg px-3 py-2 w-36 focus:outline-none focus:ring-2 focus:ring-blue-500"
62
          />
63
          <input
64
            type="number"
65
            id="longueur"
66
            placeholder="Longueur (m)"
67
            class="border border-slate-300 rounded-lg px-3 py-2 w-36 focus:outline-none focus:ring-2 focus:ring-blue-500"
68
          />
69
          <button
70
            onclick="afficherSurface()"
71
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
72
          >
73
            Calculer
74
          </button>
75
        </div>
76
        <div
77
          id="resultatSurface"
78
          class="mt-3 font-semibold text-slate-700 min-h-[1.5rem]"
79
        ></div>
80
      </section>
81
82
      <!-- Ex 3 -->
83
      <section
84
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
85
      >
86
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
87
          Exercice 3 : Conversion de température (Retour implicite)
88
        </h2>
89
        <div class="flex flex-wrap gap-3 items-center">
90
          <input
91
            type="number"
92
            id="tempCelsius"
93
            placeholder="Température °C"
94
            class="border border-slate-300 rounded-lg px-3 py-2 w-40 focus:outline-none focus:ring-2 focus:ring-blue-500"
95
          />
96
          <button
97
            onclick="convertirTemperature()"
98
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
99
          >
100
            Convertir en °F
101
          </button>
102
        </div>
103
        <div
104
          id="resultatTemperature"
105
          class="mt-3 font-semibold text-slate-700 min-h-[1.5rem]"
106
        ></div>
107
      </section>
108
109
      <!-- Ex 4 -->
110
      <section
111
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
112
      >
113
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
114
          Exercice 4 : Liste de courses (Copie et ajout via Spread `...`)
115
        </h2>
116
        <p class="text-sm text-slate-500 mb-3">
117
          Tableau initial :
118
          <code class="bg-slate-100 px-2 py-1 rounded">['Pain', 'Lait']</code>
119
        </p>
120
        <div class="flex flex-wrap gap-3 items-center">
121
          <input
122
            type="text"
123
            id="articleSaisi"
124
            placeholder="Article (ex: Pommes)"
125
            class="border border-slate-300 rounded-lg px-3 py-2 w-48 focus:outline-none focus:ring-2 focus:ring-blue-500"
126
          />
127
          <button
128
            onclick="ajouterArticle()"
129
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
130
          >
131
            Ajouter via Spread
132
          </button>
133
        </div>
134
        <div id="affichageCourses" class="mt-3 min-h-[1.5rem]"></div>
135
      </section>
136
137
      <!-- Ex 5 -->
138
      <section
139
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
140
      >
141
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
142
          Exercice 5 : Premier et dernier élément
143
        </h2>
144
        <p class="text-sm text-slate-500 mb-3">
145
          Tableau :
146
          <code class="bg-slate-100 px-2 py-1 rounded"
147
            >['Alice', 'Bob', 'Charlie', 'Diana']</code
148
          >
149
        </p>
150
        <button
151
          onclick="afficherExtremes()"
152
          class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
153
        >
154
          Afficher 1er et Dernier
155
        </button>
156
        <div id="resultatExtremes" class="mt-3 min-h-[1.5rem]"></div>
157
      </section>
158
159
      <!-- Ex 6 -->
160
      <section
161
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
162
      >
163
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
164
          Exercice 6 : Parcours de tableau (Boucle `for` & `innerHTML`)
165
        </h2>
166
        <p class="text-sm text-slate-500 mb-3">
167
          Notes :
168
          <code class="bg-slate-100 px-2 py-1 rounded"
169
            >[12, 15, 8, 19, 10]</code
170
          >
171
        </p>
172
        <button
173
          onclick="afficherNotes()"
174
          class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
175
        >
176
          Afficher les notes
177
        </button>
178
        <div id="affichageNotes" class="mt-3 min-h-[2.5rem]"></div>
179
      </section>
180
181
      <!-- Ex 7 -->
182
      <section
183
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
184
      >
185
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
186
          Exercice 7 : Somme variable de valeurs (Paramètre Rest `...`)
187
        </h2>
188
        <p class="text-sm text-slate-500 mb-3">
189
          Envoie d'un nombre variable de notes à une fonction via `...notes`.
190
        </p>
191
        <button
192
          onclick="calculerSommeRest()"
193
          class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
194
        >
195
          Calculer la somme (12, 15, 8, 19, 10)
196
        </button>
197
        <div id="resultatMoyenneTableau" class="mt-3 min-h-[1.5rem]"></div>
198
      </section>
199
200
      <!-- Ex 8 -->
201
      <section
202
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
203
      >
204
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
205
          Exercice 8 : Recherche de la valeur maximale (Boucle `for`)
206
        </h2>
207
        <p class="text-sm text-slate-500 mb-3">
208
          Valeurs :
209
          <code class="bg-slate-100 px-2 py-1 rounded"
210
            >[45, 12, 89, 34, 67, 2]</code
211
          >
212
        </p>
213
        <button
214
          onclick="chercherMaximum()"
215
          class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
216
        >
217
          Trouver le max
218
        </button>
219
        <div id="resultatMax" class="mt-3 min-h-[1.5rem]"></div>
220
      </section>
221
222
      <!-- Ex 9 -->
223
      <section
224
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
225
      >
226
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
227
          Exercice 9 : Mise en majuscules (.toUpperCase)
228
        </h2>
229
        <div class="flex flex-wrap gap-3 items-center">
230
          <input
231
            type="text"
232
            id="texteMinuscule"
233
            placeholder="Saisir un texte"
234
            class="border border-slate-300 rounded-lg px-3 py-2 w-64 focus:outline-none focus:ring-2 focus:ring-blue-500"
235
          />
236
          <button
237
            onclick="convertirMajuscules()"
238
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
239
          >
240
            Transformer
241
          </button>
242
        </div>
243
        <div id="resultatMajuscules" class="mt-3 min-h-[1.5rem]"></div>
244
      </section>
245
246
      <!-- Ex 10 -->
247
      <section
248
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
249
      >
250
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
251
          Exercice 10 : Comptage de voyelles
252
        </h2>
253
        <div class="flex flex-wrap gap-3 items-center">
254
          <input
255
            type="text"
256
            id="motVoyelles"
257
            placeholder="Mot ou phrase"
258
            class="border border-slate-300 rounded-lg px-3 py-2 w-64 focus:outline-none focus:ring-2 focus:ring-blue-500"
259
          />
260
          <button
261
            onclick="compterVoyelles()"
262
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
263
          >
264
            Compter voyelles
265
          </button>
266
        </div>
267
        <div id="resultatVoyelles" class="mt-3 min-h-[1.5rem]"></div>
268
      </section>
269
270
      <!-- Ex 11 -->
271
      <section
272
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
273
      >
274
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
275
          Exercice 11 : Inversion de chaîne (Boucle `for` à l'envers)
276
        </h2>
277
        <div class="flex flex-wrap gap-3 items-center">
278
          <input
279
            type="text"
280
            id="motAInverser"
281
            placeholder="Mot à inverser"
282
            class="border border-slate-300 rounded-lg px-3 py-2 w-48 focus:outline-none focus:ring-2 focus:ring-blue-500"
283
          />
284
          <button
285
            onclick="inverserChaine()"
286
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
287
          >
288
            Inverser
289
          </button>
290
        </div>
291
        <div id="resultatInversion" class="mt-3 min-h-[1.5rem]"></div>
292
      </section>
293
294
      <!-- Ex 12 -->
295
      <section
296
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
297
      >
298
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
299
          Exercice 12 : Recherche dans un tableau (.includes)
300
        </h2>
301
        <p class="text-sm text-slate-500 mb-3">
302
          Villes :
303
          <code class="bg-slate-100 px-2 py-1 rounded"
304
            >['Paris', 'Lyon', 'Marseille', 'Lille']</code
305
          >
306
        </p>
307
        <div class="flex flex-wrap gap-3 items-center">
308
          <input
309
            type="text"
310
            id="villeCherchee"
311
            placeholder="Nom de ville"
312
            class="border border-slate-300 rounded-lg px-3 py-2 w-48 focus:outline-none focus:ring-2 focus:ring-blue-500"
313
          />
314
          <button
315
            onclick="rechercherVille()"
316
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
317
          >
318
            Rechercher
319
          </button>
320
        </div>
321
        <div id="resultatRechercheVille" class="mt-3 min-h-[1.5rem]"></div>
322
      </section>
323
324
      <!-- Ex 13 -->
325
      <section
326
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
327
      >
328
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
329
          Exercice 13 : Suppression du dernier élément (.pop)
330
        </h2>
331
        <p class="text-sm text-slate-500 mb-3">
332
          Supprime le dernier article de la liste de l'exercice 4.
333
        </p>
334
        <button
335
          onclick="retirerDernierArticle()"
336
          class="bg-rose-600 hover:bg-rose-700 text-white font-medium px-4 py-2 rounded-lg transition"
337
        >
338
          Retirer le dernier article
339
        </button>
340
        <div id="resultatPop" class="mt-3 min-h-[1.5rem]"></div>
341
      </section>
342
343
      <!-- Ex 14 -->
344
      <section
345
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
346
      >
347
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
348
          Exercice 14 : Filtrage manuel de tableau (`for` & `if`)
349
        </h2>
350
        <p class="text-sm text-slate-500 mb-3">
351
          Source :
352
          <code class="bg-slate-100 px-2 py-1 rounded"
353
            >[5, 12, 8, 130, 44]</code
354
          >
355
        </p>
356
        <button
357
          onclick="filtrerGrandNombres()"
358
          class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
359
        >
360
          Filtrer (> 10)
361
        </button>
362
        <div id="resultatFiltre" class="mt-3 min-h-[1.5rem]"></div>
363
      </section>
364
365
      <!-- Ex 15 -->
366
      <section
367
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
368
      >
369
        <h2 class="text-lg font-semibold text-slate-800 mb-4">
370
          Exercice 15 : Synthèse — Annuaire (Rest, Spread & `innerHTML`)
371
        </h2>
372
        <div class="flex flex-wrap gap-3 items-center mb-3">
373
          <input
374
            type="text"
375
            id="nomContact"
376
            placeholder="Nom principal"
377
            class="border border-slate-300 rounded-lg px-3 py-2 w-48 focus:outline-none focus:ring-2 focus:ring-blue-500"
378
          />
379
          <button
380
            onclick="ajouterPlusieursContacts('Lucie', 'Marc')"
381
            class="bg-emerald-600 hover:bg-emerald-700 text-white font-medium px-4 py-2 rounded-lg transition"
382
          >
383
            Ajouter (Saisi + Lucie & Marc)
384
          </button>
385
          <button
386
            onclick="reinitialiserAnnuaire()"
387
            class="bg-slate-600 hover:bg-slate-700 text-white font-medium px-4 py-2 rounded-lg transition"
388
          >
389
            Réinitialiser
390
          </button>
391
        </div>
392
        <div id="affichageAnnuaire" class="mt-3 min-h-[3rem]"></div>
393
      </section>
394
    </div>
395
  </body>
396
</html>
397

SyntaxeExercice 15 : Synthèse - Annuaire de contacts (Rest, Spread & innerHTML)

Pour la gestion globale de l'annuaire de contacts :

  • Créez une fonction recevant un nombre variable de contacts supplémentaires avec le paramètre rest (...contactsSuplementaires).

  • Fusionnez les données saisies et reçues dans le tableau principal grâce à l'opérateur spread ([...annuaire, ...nouveaux]).

  • Générez et injectez le rendu HTML complet des contacts enregistrés dans le conteneur affichageAnnuaire.