Add calcul de base JS tp2

This commit is contained in:
flyingscorpio@clevo 2022-06-11 22:39:24 +02:00
parent c5b4a8f6c9
commit 7e0f277aa8

View file

@ -0,0 +1,67 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<title>Calcul de base</title>
</head>
<body>
<h1>Calcul de base</h1>
<form id="calculator">
<label for="one">1er numéro : </label>
<input type="text" name="one" id="one"/>
<br/>
<label for="two">2e numéro : </label>
<input type="text" name="two" id="two"/>
<br/>
<button type="submit" value="add">Ajouter</button>
<button type="submit" value="sub">Soustraire</button>
<button type="submit" value="mul">Multiplier</button>
<button type="submit" value="div">Diviser</button>
</form>
<p>Le résultat est :</p>
<p><code id="result"></code></p>
</body>
<script charset="utf-8">
let form = document.getElementById('calculator');
form.addEventListener("submit", calculate);
function add(a, b) {
result = `${a} + ${b} = ${a + b}`;
return result;
}
function sub(a, b) {
result = `${a} - ${b} = ${a - b}`;
return result;
}
function mul(a, b) {
result = `${a} x ${b} = ${a * b}`;
return result;
}
function div(a, b) {
result = `${a} / ${b} = ${a / b}`;
return result;
}
function calculate(event) {
event.preventDefault();
var a = Number(form.one.value);
var b = Number(form.two.value);
submitter = event.submitter.value;
switch (submitter) {
case "add":
result = add(a, b);
break;
case "sub":
sub(a, b);
break;
case "mul":
mul(a, b);
break;
case "div":
div(a, b);
break;
}
document.getElementById('result').innerHTML = result;
}
</script>
</html>