Skip to content

Commit

Permalink
section 9: calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeeppy committed Apr 16, 2023
1 parent 30092db commit 5244d34
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
63 changes: 63 additions & 0 deletions session9/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
function addition(numberOne, numberTwo) {
return numberOne + numberTwo;
}

function substract(numberOne, numberTwo) {
return numberOne - numberTwo;
}

function multiplication(numberOne, numberTwo) {
return numberOne * numberTwo;
}

function division(numberOne, numberTwo) {
if (numberTwo == 0) {
throw new Error("Impossible de diviser par 0 !");
}
return numberOne / numberTwo;
}

do {
try {
do {
var choice = prompt("Que souhaitez-vous faire ?\n\n"
+ "1 - Addition\n"
+ "2 - Soustraction\n"
+ "3 - Multiplication\n"
+ "4 - Division\n"
);
choice = Number(choice);
} while (isNaN(choice) || choice < 0 || choice > 4)

do {
var numberOne = Number(prompt("Premier nombre ?"));
} while (isNaN(numberOne))

do {
var numberTwo = Number(prompt("Deuxième nombre ?"));
} while (isNaN(numberTwo))

let result;
switch (choice) {
case 1:
result = addition(numberOne, numberTwo);
break;
case 2:
result = substract(numberOne, numberTwo);
break;
case 3:
result = multiplication(numberOne, numberTwo);
break;
case 4:
result = division(numberOne, numberTwo);
break;
default:
throw new Error("L'opération n'existe pas!");
}

alert("Résultat : " + result);
}
catch (error) {
alert(error.message);
}
} while (confirm("Voulez-vous continuer ?"))
12 changes: 12 additions & 0 deletions session9/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculatrice</title>
</head>
<body>
<script src="code.js"></script>
</body>
</html>

0 comments on commit 5244d34

Please sign in to comment.