-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
28 lines (27 loc) · 905 Bytes
/
scripts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/** Variables init **/
var resultatOutput = document.getElementById("resultat"),
btn = document.getElementById("calculateBTN"),
numberA = document.getElementById("a"),
numberB = document.getElementById("b");
/** Functions def **/
function calculer(e){
e.preventDefault();
var a = parseFloat(numberA.value),
b = parseFloat(numberB.value),
res = ((a/b)*100).toFixed(2);
if(b!==0){
resultatOutput.innerText = res + "%";
}else{
resultatOutput.classList.add("error");
resultatOutput.innerText = "Dévision par zero est impossible";
}
}
function resetElements(){
if(resultatOutput.classList.contains("error")){
resultatOutput.classList.remove("error");
resultatOutput.innerText = 0 + "%";
}
}
/** functions implementation **/
numberB.addEventListener('keyup',resetElements);
btn.addEventListener('click',calculer);