Objectifs
- Être capable de manipuler les structures conditionnelles de type
if...else.
- Être capable de manipuler les structures conditionnelles de type
Exercice 01
Énoncé
-
Vous pouvez visualiser l'énoncé de l'exercice
Solution
<!DOCTYPE html > <html lang="fr"> <head> <title>JavaScript</title> <meta ="UTF-8"> </head> <body> <script> for (i=20; i>=0; i-=2) { if(i!=0) { document.writeln(i+" - "); } else{document.writeln(i); } } </script> </body> </html>
Exercice 02
Énoncé
-
Vous pouvez visualiser l'énoncé de l'exercice
Solution
<!doctype html> <html> <head>TP JavaScript</head> <body> <script language="javascript"> document.write("Les nombres premiers compris entre 0 et 100 sont :<br>"); /* 0 et 1 ne sont pas des nombres premiers*/ for(var i=2;i<=100;i++) { var trouve=false; for(j=2;j<i/2;j++) { if(i%j==0) trouve=true; } if(trouve==false)/* S'il n'existe aucun diviseur de i*/ document.write(i,","); } </script></body></html>
Exercice 03
Énoncé
-
Vous pouvez visualiser l'énoncé de l'exercice
Solution
<!DOCTYPE html > <html lang="fr"> <head> <title>JavaScript</title> <meta ="UTF-8"> </head> <body> <script language="javascript"> document.write("<center>"); /* Mettre un titre pour la table */ document.write("<caption> Table de multiplication</caption>"); /* Créer la table*/ document.write("<table width='60%' border=1>"); /* Créer la première ligne */ document.write("<tr><td bgcolor='#FFff00' align='center'><strong>"+"x"+"</strong></td>"); for(var i=1;i<=10;i++) { document.write("<td bgcolor='#D1D7D6' align='center'><strong>"+i+"</strong></td>"); } document.write("</tr>"); /* créer les autres lignes*/ for(var i=1;i<11;i++) { /* Créer la ligne numéro i*/ document.write("<tr>"); document.write("<td bgcolor='#D1D7D6' align='center'><strong>"+i+"</strong></td>"); for(var j=1;j<=10;j++) { /* Créer la colonne numéro j*/ document.write("<td align='center'>",i*j,"</td>"); } document.write("</tr>"); } document.write("</table>"); </script> </body> </html>Exercice 04
Énoncé
-
Vous pouvez visualiser l'énoncé de l'exercice
Solution
<!DOCTYPE html > <html lang="fr"> <head> <title>JavaScript</title> <meta ="UTF-8"> </head> <body> <script language="javascript"> document.write("<center>"); var k; do{ k=prompt("Saisissez un nombre entier inférieure à 100"); }while(isNaN(k) || k>20); /*isNaN(k) renvoie true si a est non numérique .*/ /* Mettre un titre pour la table */ document.write("<caption> Table de multiplication</caption>"); /* Créer la table*/ document.write("<table width='60%' border=1>"); /* Créer la première ligne */ document.write("<tr><td bgcolor='#FFff00' align='center'><strong>"+"x"+"</strong></td>"); for(var i=1;i<=k;i++) { document.write("<td bgcolor='#D1D7D6' align='center'><strong>"+i+"</strong></td>"); } document.write("</tr>"); /* créer les autres lignes*/ var h=1; var y=parseInt(k)+1; document.write("hhhhhhhh"+parseInt(k+1)); for(var i=1;i<y;i++) { /* Créer la ligne numéro i*/ document.write("<tr>"); document.write("<td bgcolor='#D1D7D6' align='center'><strong>"+i+"</strong></td>"); for(var j=1;j<=k;j++) { /* Créer la colonne numéro j*/ document.write("<td align='center'>",i*j,"</td>"); } document.write("</tr>"); } document.write("</table>"); </script> </body> </html>Exercice 05
Énoncé
-
Vous pouvez visualiser l'énoncé de l'exercice
Solution
<!doctype html> <HTML> <HEAD> <TITLE>la structure IF</TITLE> </HEAD> <BODY> <script language="javascript"> var a; do { a = parseFloat(window.prompt('Entrez un premier entier ')); } while(isNaN(a)); var b; do { b = parseFloat(window.prompt('Entrez un deuxième entier ')); } while(isNaN(b)); var c; do { c = parseFloat(window.prompt('Entrez un troisième entier ')); } while(isNaN(c)); if (a === b && b === c) { alert("Les 3 variables sont identiques."); } else { if (b === c || a === c || a === b) { alert("2 des variables sont de valeurs égales."); } else { alert("Les 3 variables sont différentes."); } } </script> </BODY> </HTML>