Exercices

Pour réaliser ces exercices, vous utiliserez l'interface HTML fournies (index.html) reliée au fichier JavaScript (script.js). N'ajoutez pas de balises HTML : 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 2</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 2
14
      </h1>
15
      <p class="text-slate-600 text-sm">
16
        Structures de contrôle, conditions et boucles avec manipulation du DOM.
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 : Test de majorité (if / else)
26
        </h2>
27
        <form
28
          onsubmit="return false;"
29
          class="flex flex-wrap gap-3 items-center"
30
        >
31
          <label for="ageUtilisateur" class="sr-only"
32
            >Âge de l'utilisateur</label
33
          >
34
          <input
35
            type="number"
36
            id="ageUtilisateur"
37
            placeholder="Âge"
38
            class="border border-slate-300 rounded-lg px-3 py-2 w-32 focus:outline-none focus:ring-2 focus:ring-blue-500"
39
          />
40
          <button
41
            type="button"
42
            onclick="verifierMajorite()"
43
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
44
          >
45
            Vérifier
46
          </button>
47
        </form>
48
        <output
49
          id="resultatMajorite"
50
          class="block mt-3 font-semibold text-slate-700 min-h-[1.5rem]"
51
        ></output>
52
      </section>
53
54
      <!-- Ex 2 -->
55
      <section
56
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
57
      >
58
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
59
          Exercice 2 : Évaluation d'une note minimale (Ternaire)
60
        </h2>
61
        <form
62
          onsubmit="return false;"
63
          class="flex flex-wrap gap-3 items-center"
64
        >
65
          <label for="noteExamen" class="sr-only"
66
            >Note de l'examen sur 20</label
67
          >
68
          <input
69
            type="number"
70
            id="noteExamen"
71
            placeholder="Note /20"
72
            class="border border-slate-300 rounded-lg px-3 py-2 w-32 focus:outline-none focus:ring-2 focus:ring-blue-500"
73
          />
74
          <button
75
            type="button"
76
            onclick="evaluerNote()"
77
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
78
          >
79
            Évaluer
80
          </button>
81
        </form>
82
        <output
83
          id="resultatAdmission"
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 : Comparaison Strictement Égal (== vs ===)
94
        </h2>
95
        <button
96
          type="button"
97
          onclick="comparerEgalite()"
98
          class="bg-slate-800 hover:bg-slate-900 text-white font-medium px-4 py-2 rounded-lg transition"
99
        >
100
          Lancer la comparaison (Console)
101
        </button>
102
        <p class="text-xs text-slate-500 mt-2">
103
          Ouvrez la console du navigateur (<kbd
104
            class="bg-slate-100 border px-1 rounded"
105
            >F12</kbd
106
          >) pour voir les logs.
107
        </p>
108
      </section>
109
110
      <!-- Ex 4 -->
111
      <section
112
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
113
      >
114
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
115
          Exercice 4 : Attribution de mention (if / else if / else)
116
        </h2>
117
        <form
118
          onsubmit="return false;"
119
          class="flex flex-wrap gap-3 items-center"
120
        >
121
          <label for="moyenneEtudiant" class="sr-only"
122
            >Moyenne de l'étudiant</label
123
          >
124
          <input
125
            type="number"
126
            id="moyenneEtudiant"
127
            placeholder="Moyenne"
128
            step="0.5"
129
            class="border border-slate-300 rounded-lg px-3 py-2 w-32 focus:outline-none focus:ring-2 focus:ring-blue-500"
130
          />
131
          <button
132
            type="button"
133
            onclick="obtenirMention()"
134
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
135
          >
136
            Obtenir la mention
137
          </button>
138
        </form>
139
        <output
140
          id="affichageMention"
141
          class="block mt-3 font-semibold text-slate-700 min-h-[1.5rem]"
142
        ></output>
143
      </section>
144
145
      <!-- Ex 5 -->
146
      <section
147
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
148
      >
149
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
150
          Exercice 5 : Vérification d'accès VIP (&amp;&amp; et ||)
151
        </h2>
152
        <form
153
          onsubmit="return false;"
154
          class="flex flex-wrap gap-4 items-center"
155
        >
156
          <label for="ageVip" class="sr-only">Âge du visiteur</label>
157
          <input
158
            type="number"
159
            id="ageVip"
160
            placeholder="Âge"
161
            class="border border-slate-300 rounded-lg px-3 py-2 w-32 focus:outline-none focus:ring-2 focus:ring-blue-500"
162
          />
163
          <label
164
            class="inline-flex items-center gap-2 cursor-pointer select-none text-sm font-medium text-slate-700"
165
          >
166
            <input
167
              type="checkbox"
168
              id="possedeInvitation"
169
              class="w-4 h-4 text-blue-600 rounded border-slate-300 focus:ring-blue-500"
170
            />
171
            Invitation ?
172
          </label>
173
          <button
174
            type="button"
175
            onclick="verifierVip()"
176
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
177
          >
178
            Vérifier l'accès
179
          </button>
180
        </form>
181
        <output
182
          id="resultatVip"
183
          class="block mt-3 font-semibold text-slate-700 min-h-[1.5rem]"
184
        ></output>
185
      </section>
186
187
      <!-- Ex 6 -->
188
      <section
189
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
190
      >
191
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
192
          Exercice 6 : Jours de la semaine (switch)
193
        </h2>
194
        <form
195
          onsubmit="return false;"
196
          class="flex flex-wrap gap-3 items-center"
197
        >
198
          <label for="numeroJour" class="sr-only">Numéro du jour</label>
199
          <input
200
            type="number"
201
            id="numeroJour"
202
            placeholder="Jour (1-7)"
203
            class="border border-slate-300 rounded-lg px-3 py-2 w-36 focus:outline-none focus:ring-2 focus:ring-blue-500"
204
          />
205
          <button
206
            type="button"
207
            onclick="convertirJour()"
208
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
209
          >
210
            Convertir
211
          </button>
212
        </form>
213
        <output
214
          id="nomJour"
215
          class="block mt-3 font-semibold text-slate-700 min-h-[1.5rem]"
216
        ></output>
217
      </section>
218
219
      <!-- Ex 7 -->
220
      <section
221
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
222
      >
223
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
224
          Exercice 7 : Table de multiplication (for)
225
        </h2>
226
        <form
227
          onsubmit="return false;"
228
          class="flex flex-wrap gap-3 items-center"
229
        >
230
          <label for="nombreTable" class="sr-only">Nombre pour la table</label>
231
          <input
232
            type="number"
233
            id="nombreTable"
234
            placeholder="Nombre"
235
            class="border border-slate-300 rounded-lg px-3 py-2 w-32 focus:outline-none focus:ring-2 focus:ring-blue-500"
236
          />
237
          <button
238
            type="button"
239
            onclick="genererTable()"
240
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
241
          >
242
            Générer
243
          </button>
244
        </form>
245
        <pre
246
          id="affichageTable"
247
          class="mt-3 bg-slate-50 p-3 rounded-lg border border-slate-200 font-mono text-sm text-slate-800 min-h-[2.5rem] whitespace-pre-wrap"
248
        ></pre>
249
      </section>
250
251
      <!-- Ex 8 -->
252
      <section
253
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
254
      >
255
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
256
          Exercice 8 : Décompte interactif (while)
257
        </h2>
258
        <form
259
          onsubmit="return false;"
260
          class="flex flex-wrap gap-3 items-center"
261
        >
262
          <label for="valeurDepart" class="sr-only"
263
            >Valeur de départ du décompte</label
264
          >
265
          <input
266
            type="number"
267
            id="valeurDepart"
268
            placeholder="Départ (ex: 5)"
269
            class="border border-slate-300 rounded-lg px-3 py-2 w-36 focus:outline-none focus:ring-2 focus:ring-blue-500"
270
          />
271
          <button
272
            type="button"
273
            onclick="lancerDecompte()"
274
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
275
          >
276
            Lancer
277
          </button>
278
        </form>
279
        <output
280
          id="resultatDecompte"
281
          class="block mt-3 font-semibold text-slate-700 min-h-[1.5rem]"
282
        ></output>
283
      </section>
284
285
      <!-- Ex 9 -->
286
      <section
287
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
288
      >
289
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
290
          Exercice 9 : Calcul de somme d'entiers (Accumulateur)
291
        </h2>
292
        <form
293
          onsubmit="return false;"
294
          class="flex flex-wrap gap-3 items-center"
295
        >
296
          <label for="nombreN" class="sr-only">Nombre N</label>
297
          <input
298
            type="number"
299
            id="nombreN"
300
            placeholder="N (ex: 10)"
301
            class="border border-slate-300 rounded-lg px-3 py-2 w-32 focus:outline-none focus:ring-2 focus:ring-blue-500"
302
          />
303
          <button
304
            type="button"
305
            onclick="calculerSomme()"
306
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
307
          >
308
            Calculer la somme
309
          </button>
310
        </form>
311
        <output
312
          id="resultatSomme"
313
          class="block mt-3 font-semibold text-slate-700 min-h-[1.5rem]"
314
        ></output>
315
      </section>
316
317
      <!-- Ex 10 -->
318
      <section
319
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
320
      >
321
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
322
          Exercice 10 : Filtre de nombres pairs
323
        </h2>
324
        <form
325
          onsubmit="return false;"
326
          class="flex flex-wrap gap-3 items-center"
327
        >
328
          <label for="limitePair" class="sr-only">Limite pour les pairs</label>
329
          <input
330
            type="number"
331
            id="limitePair"
332
            placeholder="Limite (ex: 20)"
333
            class="border border-slate-300 rounded-lg px-3 py-2 w-36 focus:outline-none focus:ring-2 focus:ring-blue-500"
334
          />
335
          <button
336
            type="button"
337
            onclick="filtrerPairs()"
338
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
339
          >
340
            Filtrer
341
          </button>
342
        </form>
343
        <output
344
          id="listePairs"
345
          class="block mt-3 font-semibold text-slate-700 min-h-[1.5rem] break-words"
346
        ></output>
347
      </section>
348
349
      <!-- Ex 11 -->
350
      <section
351
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
352
      >
353
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
354
          Exercice 11 : Validation de mot de passe
355
        </h2>
356
        <form
357
          onsubmit="return false;"
358
          class="flex flex-wrap gap-3 items-center"
359
        >
360
          <label for="motDePasse" class="sr-only">Mot de passe</label>
361
          <input
362
            type="password"
363
            id="motDePasse"
364
            placeholder="Mot de passe"
365
            class="border border-slate-300 rounded-lg px-3 py-2 w-48 focus:outline-none focus:ring-2 focus:ring-blue-500"
366
          />
367
          <button
368
            type="button"
369
            onclick="validerMotDePasse()"
370
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
371
          >
372
            Valider
373
          </button>
374
        </form>
375
        <output
376
          id="feedbackMotDePasse"
377
          class="block mt-3 font-semibold min-h-[1.5rem]"
378
        ></output>
379
      </section>
380
381
      <!-- Ex 12 -->
382
      <section
383
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
384
      >
385
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
386
          Exercice 12 : Calculateur de remises progressives
387
        </h2>
388
        <form
389
          onsubmit="return false;"
390
          class="flex flex-wrap gap-3 items-center"
391
        >
392
          <label for="montantTotal" class="sr-only"
393
            >Montant total en euros</label
394
          >
395
          <input
396
            type="number"
397
            id="montantTotal"
398
            placeholder="Montant €"
399
            class="border border-slate-300 rounded-lg px-3 py-2 w-36 focus:outline-none focus:ring-2 focus:ring-blue-500"
400
          />
401
          <button
402
            type="button"
403
            onclick="calculerRemise()"
404
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
405
          >
406
            Calculer le net
407
          </button>
408
        </form>
409
        <output
410
          id="resultatRemise"
411
          class="block mt-3 font-semibold text-slate-700 min-h-[1.5rem] whitespace-pre-line"
412
        ></output>
413
      </section>
414
415
      <!-- Ex 13 -->
416
      <section
417
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
418
      >
419
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
420
          Exercice 13 : Générateur de Motifs (Boucles imbriquées)
421
        </h2>
422
        <form
423
          onsubmit="return false;"
424
          class="flex flex-wrap gap-3 items-center"
425
        >
426
          <label for="nombreLignes" class="sr-only">Nombre de lignes</label>
427
          <input
428
            type="number"
429
            id="nombreLignes"
430
            placeholder="Lignes"
431
            class="border border-slate-300 rounded-lg px-3 py-2 w-32 focus:outline-none focus:ring-2 focus:ring-blue-500"
432
          />
433
          <button
434
            type="button"
435
            onclick="genererMotif()"
436
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
437
          >
438
            Générer
439
          </button>
440
        </form>
441
        <pre
442
          id="affichageMotif"
443
          class="mt-3 bg-slate-900 text-amber-400 p-3 rounded-lg font-mono text-sm leading-none min-h-[2.5rem] whitespace-pre"
444
        ></pre>
445
      </section>
446
447
      <!-- Ex 14 -->
448
      <section
449
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
450
      >
451
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
452
          Exercice 14 : Jeu du Nombre Mystère
453
        </h2>
454
        <form
455
          onsubmit="return false;"
456
          class="flex flex-wrap gap-3 items-center"
457
        >
458
          <label for="propositionNombre" class="sr-only"
459
            >Proposition de nombre</label
460
          >
461
          <input
462
            type="number"
463
            id="propositionNombre"
464
            placeholder="Nombre (1-100)"
465
            class="border border-slate-300 rounded-lg px-3 py-2 w-40 focus:outline-none focus:ring-2 focus:ring-blue-500"
466
          />
467
          <button
468
            type="button"
469
            onclick="jouerNombreMystere()"
470
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
471
          >
472
            Tester
473
          </button>
474
        </form>
475
        <output
476
          id="statutJeu"
477
          class="block mt-3 font-semibold text-slate-700 min-h-[1.5rem]"
478
        ></output>
479
      </section>
480
481
      <!-- Ex 15 -->
482
      <section
483
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
484
      >
485
        <h2 class="text-lg font-semibold text-slate-800 mb-4">
486
          Exercice 15 : Synthèse — Simulation DAB
487
        </h2>
488
        <form
489
          onsubmit="return false;"
490
          class="flex flex-wrap gap-3 items-center"
491
        >
492
          <label for="soldeCompte" class="sr-only">Solde initial</label>
493
          <input
494
            type="number"
495
            id="soldeCompte"
496
            placeholder="Solde initial €"
497
            value="500"
498
            class="border border-slate-300 rounded-lg px-3 py-2 w-36 focus:outline-none focus:ring-2 focus:ring-blue-500"
499
          />
500
          <label for="typeOperation" class="sr-only">Type d'opération</label>
501
          <select
502
            id="typeOperation"
503
            class="border border-slate-300 rounded-lg px-3 py-2 bg-white focus:outline-none focus:ring-2 focus:ring-blue-500"
504
          >
505
            <option value="retrait">Retrait</option>
506
            <option value="depot">Dépôt</option>
507
          </select>
508
          <label for="montantOperation" class="sr-only"
509
            >Montant de l'opération</label
510
          >
511
          <input
512
            type="number"
513
            id="montantOperation"
514
            placeholder="Montant €"
515
            class="border border-slate-300 rounded-lg px-3 py-2 w-32 focus:outline-none focus:ring-2 focus:ring-blue-500"
516
          />
517
          <button
518
            type="button"
519
            onclick="traiterDab()"
520
            class="bg-emerald-600 hover:bg-emerald-700 text-white font-medium px-4 py-2 rounded-lg transition"
521
          >
522
            Valider l'opération
523
          </button>
524
        </form>
525
        <output
526
          id="resultatDab"
527
          class="block mt-4 p-4 rounded-lg bg-slate-50 border border-slate-200 font-medium text-slate-700 min-h-[2rem] whitespace-pre-line"
528
        ></output>
529
      </section>
530
531
      <!-- Ex 16 -->
532
      <section
533
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
534
      >
535
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
536
          Exercice 16 : Affichage d'une alerte stylisée (if / else &amp;
537
          innerHTML)
538
        </h2>
539
        <form
540
          onsubmit="return false;"
541
          class="flex flex-wrap gap-3 items-center"
542
        >
543
          <label for="ageAlerte" class="sr-only">Âge de contrôle</label>
544
          <input
545
            type="number"
546
            id="ageAlerte"
547
            placeholder="Âge"
548
            class="border border-slate-300 rounded-lg px-3 py-2 w-32 focus:outline-none focus:ring-2 focus:ring-blue-500"
549
          />
550
          <button
551
            type="button"
552
            onclick="afficherAlerte()"
553
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
554
          >
555
            Tester l'accès
556
          </button>
557
        </form>
558
        <output id="conteneurAlerte" class="block mt-3 min-h-[2.5rem]"></output>
559
      </section>
560
561
      <!-- Ex 17 -->
562
      <section
563
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
564
      >
565
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
566
          Exercice 17 : Génération d'une liste à puces (for &amp; innerHTML)
567
        </h2>
568
        <form
569
          onsubmit="return false;"
570
          class="flex flex-wrap gap-3 items-center"
571
        >
572
          <label for="nombreListe" class="sr-only">Nombre d'éléments</label>
573
          <input
574
            type="number"
575
            id="nombreListe"
576
            placeholder="Nombre"
577
            class="border border-slate-300 rounded-lg px-3 py-2 w-32 focus:outline-none focus:ring-2 focus:ring-blue-500"
578
          />
579
          <button
580
            type="button"
581
            onclick="genererListeHtml()"
582
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
583
          >
584
            Générer la liste
585
          </button>
586
        </form>
587
        <output id="conteneurListe" class="block mt-3 min-h-[2.5rem]"></output>
588
      </section>
589
590
      <!-- Ex 18 -->
591
      <section
592
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
593
      >
594
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
595
          Exercice 18 : Galerie de mentions stylisées (switch &amp; innerHTML)
596
        </h2>
597
        <form
598
          onsubmit="return false;"
599
          class="flex flex-wrap gap-3 items-center"
600
        >
601
          <label for="noteBadge" class="sr-only">Note pour le badge</label>
602
          <input
603
            type="number"
604
            id="noteBadge"
605
            placeholder="Note /20"
606
            class="border border-slate-300 rounded-lg px-3 py-2 w-32 focus:outline-none focus:ring-2 focus:ring-blue-500"
607
          />
608
          <button
609
            type="button"
610
            onclick="afficherBadgeMention()"
611
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
612
          >
613
            Afficher le badge
614
          </button>
615
        </form>
616
        <output id="conteneurBadge" class="block mt-3 min-h-[2.5rem]"></output>
617
      </section>
618
619
      <!-- Ex 19 -->
620
      <section
621
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
622
      >
623
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
624
          Exercice 19 : Compteur visuel de cartes (while &amp; innerHTML)
625
        </h2>
626
        <form
627
          onsubmit="return false;"
628
          class="flex flex-wrap gap-3 items-center"
629
        >
630
          <label for="nombreCartes" class="sr-only">Nombre de cartes</label>
631
          <input
632
            type="number"
633
            id="nombreCartes"
634
            placeholder="Nombre de cartes"
635
            class="border border-slate-300 rounded-lg px-3 py-2 w-44 focus:outline-none focus:ring-2 focus:ring-blue-500"
636
          />
637
          <button
638
            type="button"
639
            onclick="genererCartes()"
640
            class="bg-blue-600 hover:bg-blue-700 text-white font-medium px-4 py-2 rounded-lg transition"
641
          >
642
            Générer les cartes
643
          </button>
644
        </form>
645
        <output
646
          id="conteneurCartes"
647
          class="flex flex-wrap gap-3 mt-4 min-h-[2.5rem]"
648
        ></output>
649
      </section>
650
651
      <!-- Ex 20 -->
652
      <section
653
        class="bg-white p-6 rounded-xl shadow-sm border border-slate-200"
654
      >
655
        <h2 class="text-lg font-semibold text-slate-800 mb-3">
656
          Exercice 20 : Synthèse — Ticket de caisse dynamique (innerHTML)
657
        </h2>
658
        <form
659
          onsubmit="return false;"
660
          class="flex flex-wrap gap-3 items-center"
661
        >
662
          <label for="montantTicket" class="sr-only">Montant d'achat</label>
663
          <input
664
            type="number"
665
            id="montantTicket"
666
            placeholder="Montant Achat €"
667
            class="border border-slate-300 rounded-lg px-3 py-2 w-40 focus:outline-none focus:ring-2 focus:ring-blue-500"
668
          />
669
          <button
670
            type="button"
671
            onclick="genererTicketCaisse()"
672
            class="bg-emerald-600 hover:bg-emerald-700 text-white font-medium px-4 py-2 rounded-lg transition"
673
          >
674
            Générer le ticket
675
          </button>
676
        </form>
677
        <output id="conteneurTicket" class="block mt-4 min-h-[2.5rem]"></output>
678
      </section>
679
    </main>
680
  </body>
681
</html>
682

SyntaxeExercice 1 : Test de majorité (if / else)

  • Récupérez la valeur du champ d'âge via document.querySelector et la propriété .value.

  • Si l'âge est supérieur ou égal à 18 :

    • Affichez "Majeur", sinon "Mineur".

    • Modifiez le style du texte en changeant sa classe Tailwind grâce à l'instruction : element.className = 'valeur' (ex: 'text-emerald-600' pour le vert, 'text-rose-600' pour le rouge).

SyntaxeExercice 2 : Évaluation d'une note minimale (Ternaire)

  • Récupérez la note via document.querySelector.

  • Utilisez une condition ternaire pour stocker "Admis" si la note est >= 10, sinon "Ajourné".

  • Affichez le résultat dans le paragraphe de rendu via .textContent.

SyntaxeExercice 3 : Comparaison Strictement Égal (== vs ===)

Créez une fonction déclenchée au clic qui compare le nombre 100 et la chaîne "100" avec == puis avec ===, et affichez les résultats dans la console avec console.log().

SyntaxeExercice 4 : Attribution de mention (if / else if / else)

  • Récupérez la moyenne.

  • Affichez "Insuffisant" (< 10), "Passable" (< 12), "Assez Bien" (< 14) ou "Bien" (>= 14) en modifiant le .textContent de l'élément ciblé.

SyntaxeExercice 5 : Vérification d'accès VIP (&& et ||)

  • Récupérez l'âge via .value.

  • Vérifier si la case à cocher est cochée, utilisez l'instruction : element.checked (qui renvoie true ou false).

  • Accordez l'accès si l'utilisateur est majeur ET a une invitation, OU s'il a plus de 65 ans.

SyntaxeExercice 6 : Jours de la semaine (switch)

  • Récupérez un numéro de 1 à 7 .

  • Affichez le jour correspondant grâce à une structure switch.

  • Affichez un message d'erreur si le nombre est invalide.

SyntaxeExercice 7 : Table de multiplication (for)

À l'aide d'une boucle for :

  • Générez la table de multiplication (de 1 à 10) du nombre saisi.

  • Affichez le résultat ligne par ligne dans la balise <pre>.

SyntaxeExercice 8 : Décompte interactif (while)

À partir d'un nombre de départ, utilisez une boucle while pour construire une chaîne de décompte (ex: "5... 4... 3... 2... 1... 0... Décollage !").

SyntaxeExercice 9 : Calcul de somme d'entiers (Accumulateur)

Calculez la somme des entiers de 1 jusqu'au nombre N saisi en utilisant une boucle et une variable accumulateur.

SyntaxeExercice 10 : Filtre de nombres pairs

Parcourez tous les nombres de 1 jusqu'à la limite saisie. Affichez uniquement les nombres pairs séparés par une virgule.

SyntaxeExercice 11 : Validation de mot de passe

  • Vérifiez la longueur de la chaîne saisie via la propriété .length.

  • Affichez un message vert ("Valide") si elle contient au moins 8 caractères, ou rouge sinon.

SyntaxeExercice 12 : Calculateur de remises progressives

Calculez le prix net :

  • 0% de remise sous 50€,

  • 10% entre 50€ et 100€,

  • 20% au-delà de 100€.

Affichez le montant de la remise et le prix final.

SyntaxeExercice 13 : Générateur de Motifs (Boucles imbriquées)

Construisez une pyramide d'étoiles (*) en fonction du nombre de lignes saisi, en utilisant deux boucles for imbriquées.

SyntaxeExercice 14 : Jeu du Nombre Mystère

Comparez la saisie avec le nombre secret (ex: 42) :

  • Décrémentez le nombre d'essais (max 5) à chaque tentative.

  • Affichez si le nombre est plus grand ou plus petit.

SyntaxeExercice 15 : Synthèse - Simulation DAB

  • Gérez les dépôts et retraits sur un solde bancaire.

  • Empêchez les retraits supérieurs au solde disponible.

  • Mettez à jour la valeur du champ solde.

SyntaxeExercice 16 : Affichage d'une alerte stylisée (if / else & innerHTML)

  • Récupérez la valeur du champ d'âge via .value.

  • Si l'utilisateur est majeur, injectez le composant HTML <div class="alert success">Accès autorisé</div> avec innerHTML.

  • Sinon, injectez le composant <div class="alert danger">Accès refusé</div> dans le même conteneur.

SyntaxeExercice 17 : Génération d'une liste à puces (for & innerHTML)

  • Récupérez le nombre choisi par l'utilisateur.

  • À l'aide d'une boucle for, construisez une chaîne HTML contenant des balises <ul> et <li>.

  • Affichez la table de multiplication sous forme de liste structurée dans la page via innerHTML.

SyntaxeExercice 18 : Galerie de mentions stylisées (switch & innerHTML)

  • Récupérez la note saisie par l'étudiant.

  • Utilisez une structure switch pour déterminer la tranche d'évaluation.

  • Générez et injectez un badge HTML personnalisé (ex: <span class="badge badge-warning">Passable</span>) avec innerHTML.

SyntaxeExercice 19 : Compteur visuel de cartes (while & innerHTML)

  • Lisez la quantité de cartes N demandée dans le formulaire.

  • Générez N blocs HTML de cartes (<div class="card">Jeton n°X</div>) grâce à une boucle while.

  • Injectez l'ensemble des éléments dans le conteneur visuel en une seule fois via innerHTML.

SyntaxeExercice 20 : Synthèse - Ticket de caisse dynamique (innerHTML)

  • Calculez le prix net en fonction de la remise appliquée.

  • Construisez un tableau HTML récapitulatif complet à l'aide des balises <table>, <tr> et <td>.

  • Insérez ce ticket de caisse dans l'élément cible en utilisant innerHTML.