|
| 1 | +const form = document.querySelector("form"); |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +// click function |
| 6 | +form.addEventListener('submit', function(e){ |
| 7 | + |
| 8 | + e.preventDefault(); |
| 9 | + |
| 10 | + const height = parseInt(document.querySelector("#height").value); |
| 11 | + const weight = parseInt(document.querySelector("#weight").value); |
| 12 | + const resultsText = document.querySelector('#results h4') |
| 13 | + const results = document.querySelector('#results') |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | + if((height === '' || height <= 0 || isNaN(height))){ |
| 18 | + |
| 19 | + resultsText.innerHTML = "Please give a valid Height and Weight"; |
| 20 | + |
| 21 | + } |
| 22 | + |
| 23 | + else if((weight === '' || weight <= 0 || isNaN(weight))){ |
| 24 | + |
| 25 | + resultsText.innerHTML = "Please give a valid Height and Weight"; |
| 26 | + |
| 27 | + } |
| 28 | + |
| 29 | + else{ |
| 30 | + const bmi = (weight / ((height * height) / 10000)).toFixed(2); |
| 31 | + |
| 32 | + if(bmi < 18.6){ |
| 33 | + results.style.backgroundColor = "yellow"; |
| 34 | + results.style.color = "black"; |
| 35 | + resultsText.innerHTML = `You are under weight \n ${bmi}`; |
| 36 | + } |
| 37 | + |
| 38 | + else if((bmi >= 18.6) && (bmi <= 24.9) ){ |
| 39 | + results.style.backgroundColor = "green"; |
| 40 | + results.style.color = "white"; |
| 41 | + resultsText.innerHTML = `You are normal \n ${bmi}`; |
| 42 | + } |
| 43 | + |
| 44 | + else if((bmi > 24.9) && (bmi <= 40) ){ |
| 45 | + results.style.backgroundColor = "orange"; |
| 46 | + results.style.color = "white"; |
| 47 | + resultsText.innerHTML = `You are overweight \n ${bmi}`; |
| 48 | + } |
| 49 | + |
| 50 | + else{ |
| 51 | + |
| 52 | + results.innerHTML = `<h4>Sorry, you are experiencing obesity \n ${bmi}</h4>`; |
| 53 | + results.style.color = "white"; |
| 54 | + results.style.backgroundColor = "red"; |
| 55 | + |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | +}); |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +// reset code |
| 64 | +function resetResults() { |
| 65 | + results.innerHTML = "<h4>Try your BMI.</h4>"; |
| 66 | + results.style.backgroundColor = ""; |
| 67 | +} |
| 68 | + |
| 69 | +height.addEventListener("input", resetResults); |
| 70 | +weight.addEventListener("input", resetResults); |
| 71 | + |
0 commit comments