Nom : Diallo
Prénom : Aminata
IEN : N02742620222
Td/Tp1 introduction au JavaScript
Td/Tp 1 introduction JavaScript
Exercice 1
a) Page HTML affichant « Bonjour tout le monde » :
```html
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bonjour</title>
</head>
<body>
<script>
alert("Bonjour tout le monde");
</script>
</body>
</html>
```
b) Demander le prénom de l'utilisateur et afficher « Bonjour 'votre prénom' » :
```html
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bonjour</title>
</head>
<body>
<script>
var prenom = prompt("Quel est votre prénom ?");
alert("Bonjour " + prenom);
</script>
</body>
</html>
```
Exercice 2
Demander deux nombres et afficher leur somme :
```html
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Somme</title>
</head>
<body>
<script>
var nombre1 = parseFloat(prompt("Entrez le premier nombre :"));
var nombre2 = parseFloat(prompt("Entrez le deuxième nombre :"));
var somme = nombre1 + nombre2;
alert("La somme est : " + somme);
</script>
</body>
</html>
```
Exercice 3
Contrôler la validité de l'âge entré :
```html
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Âge</title>
</head>
<body>
<script>
var age;
do {
age = parseInt(prompt("Entrez votre âge :"));
if (isNaN(age) || age < 0) {
alert("Erreur : veuillez entrer un âge valide.");
}
} while (isNaN(age) || age < 0);
alert("Vous avez " + age + " ans.");
</script>
</body>
</html>
```
Exercice 4
Afficher les nombres premiers compris entre 0 et 100 :
```html
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Nombres Premiers</title>
</head>
<body>
<script>
for (var i = 2; i <= 100; i++) {
var estPremier = true;
for (var j = 2; j <= Math.sqrt(i); j++) {
if (i % j === 0) {
estPremier = false;
break;
}
}
if (estPremier) {
document.write(i + "<br>");
}
}
</script>
</body>
</html>
```
Exercice 5
Afficher la table de multiplication dans un tableau :
```html
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Table de Multiplication</title>
</head>
<body>
<table border="1">
<tr>
<th>*</th>
<th>0</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
<th>6</th>
<th>7</th>
<th>8</th>
<th>9</th>
</tr>
<script>
for (var i = 0; i <= 9; i++) {
document.write("<tr><th>" + i + "</th>");
for (var j = 0; j <= 9; j++) {
document.write("<td>" + (i * j) + "</td>");
}
document.write("</tr>");
}
</script>
</table>
</body>
</html>
```
Exercice 6
Modifier la couleur de l'arrière-plan avec un bouton :
```html
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Changer Couleur</title>
</head>
<body>
<button onclick="changerCouleur()">Changer la couleur</button>
<script>
function changerCouleur() {
document.bgColor = "#FFCC00"; // Choisir une couleur valide
}
</script>
</body>
</html>
```