Exercices du livre JAVASCRIPT Solution Exercice 1 page 112 :
<html>
<head><title>calculatrice</title></head>
<script language="javascript">
function caluler() {
var a=Number(document.f.t1.value);
var b=Number(document.f.t2.value);
if (document.f.r1[0].checked) alert((a+b));
else if (document.f.r1[1].checked) alert((a-b));
else if (document.f.r1[2].checked) alert((a*b));
else if ((document.f.r1[3].checked) && (b!=0)) alert((a/b));
else alert("division impossible");
}
</script>
<body bgcolor=#3447A3>
<form name="f">
Variable 1: <input type="text"
name="t1"><br>
Variable 2: <input type="text"
name="t2"><br><br><br>
<h2>Opérateurs :</h2>
<table>
<tr><td>
<input type="radio" name="r1">Addition<br>
<input type="radio" name="r1">Soustraction<br>
<input type="radio" name="r1">Multiplication<br>
<input type="radio" name="r1">division<br>
</td>
<td><input type="button" value="calculer" onclick="caluler()">
<input type="reset"value="effacer"></td>
</tr>
</table>
</form>
</body>
</html>
Solution Exercice 9 page 117 :
<head>
<script language="javascript">
function calculer() {
t=new Array(3);
t[0]=new Array(3);
t[1]=new Array(3);
t[2]=new Array(3);
t[0][0]="1200";
t[0][1]="1250";
t[0][2]="1300";
t[1][0]="850";
t[1][1]="800";
t[1][2]="900";
t[2][0]="800";
t[2][1]="850";
t[2][2]="600";
i=document.F1.article.selectedIndex;
j=document.F1.taille.selectedIndex;
document.F1.p.value=t[i][j];
}
</script></head>
<body><form name="F1">
Rcherche de prix<br>
<select name="article">
<option> chemise
<option> polos
<option> T-shirts </select>
<select name="taille">
<option> T-Small
<option> T-médium
<option> T- Large</select>
<input type="button" value="donner le prix" onclick="calculer()">
<input type="text" name="p">
</form>
</body>
</html>