<form action="#">
<input type="number" placeholder="1" min="0" class="number">
<input type="number" placeholder="2" min="0" class="number">
<input type="number" placeholder="3" min="0" class="number">
<button id="btn">Oblicz</button>
</form>
< div class="result"></div>
const calculateButton = () => {
let button = document.getElementById("btn");
button.addEventListener("click", (e) =>{
e.preventDefault();
let numbersNode = document.querySelectorAll(".number");
let result = document.querySelector(".result");
const average = () => {
let avarage = 0;
numbersNode.forEach(number => {
avarage+=+number.value;
})
return result.innerHTML = (avarage/numbersNode.length).toFixed(2);
}
average();
});
const init =()=>{
calculateButton()
};
init();
" Life is not a problem to be solved but a reality to be experienced! "
© Copyright 2013 - 2024 KUDO.TIPS - All rights reserved.
Kod HT ML:
<form action="#">
<input type="number" placeholder="1" min="0" class="number">
<input type="number" placeholder="2" min="0" class="number">
<input type="number" placeholder="3" min="0" class="number">
<button id="btn">Oblicz</button>
</form>
< div class="result"></div>
Kod JavaScript:
const calculateButton = () => {
let button = document.getElementById("btn");
button.addEventListener("click", (e) =>{
e.preventDefault();
let numbersNode = document.querySelectorAll(".number");
let result = document.querySelector(".result");
const average = () => {
let avarage = 0;
numbersNode.forEach(number => {
avarage+=+number.value;
})
return result.innerHTML = (avarage/numbersNode.length).toFixed(2);
}
average();
});
}
const init =()=>{
calculateButton()
};
init();