Objectifs
- ِEtre capable de manipuler les chaînes de caractères en JavaScript
Exercice 01
Énoncé
-
Vous pouvez visualiser l'énoncé de l'exercice
Solution
<!doctype html> <html> <head> <title>JavaScript</title> <script language="JavaScript"> var mot =prompt("Saisir un mot"); var lg=mot.length; document.write("<br><b>Le mot saisie est : "+mot+"</b>"); document.writeln("<br>La première lettre du mot <strong><span style='color:blue;'>"+mot+"</span></strong> est: "+mot.charAt(0)+" et il se compose de "+lg+" caractères"); var lastChar = mot.substr(-1); document.writeln("<br>La dernière lettre du mot <strong><span style='color:blue;'>"+mot+"</span></strong> est: "+lastChar); </script> </head> <body> </body> </html>
Exercice 02
Énoncé
-
Vous pouvez visualiser l'énoncé de l'exercice
Solution
<!doctype html> <html> <head> <title>TP JavaScript Exe02</title> <script language="JavaScript"> var mot =prompt("Saisir un mot"); var lg=mot.length; document.write("<br><b>Le mot saisie est : "+mot+"</b>"); document.writeln("<br>La première lettre du mot <strong><span style='color:blue;'>"+mot+"</span></strong> est: "+mot.charAt(0)+" et il se compose de "+lg+" caractères"); document.writeln("<br>La deuxème lettre du mot <strong><span style='color:blue;'>"+mot+"</span></strong> est: "+mot.charAt(1)); document.writeln("<br>La troisième lettre du mot <strong><span style='color:blue;'>"+mot+"</span></strong> est: "+mot.charAt(2)); document.writeln("<br>La dernière lettre du mot <strong><span style='color:blue;'>"+mot+"</span></strong> est: "+mot.substr(-1)); </script> </head> <body> </body> </html>
Exercice 03
Énoncé
-
Vous pouvez visualiser l'énoncé de l'exercice
Solution
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script language="javascript"> function afficherMessage() { document.write('<CENTER><FONT COLOR = "BLUE" size="24px"> Bienvenue à ma page</FONT></CENTER>'); } </script> </head> <body onLoad="afficherMessage();"> </body> </html>