const juustuhind=1.0;
const magushind=2.0;
const pontsikhind=3.0;
function calculate(val, saiatyyp){
return (val*saiatyyp).toFixed(2);
}
//puhastab Tekstkasti sisestatud väärtus
function tyhistaTextValue(){
document.getElementById(“saiatyyp”).value=””;
}
//puhastab Select valiku
function tyhistaSelectOption(){
document.getElementById(“saiatyyp2”).selectedIndex=0;
}
//puhastab Radiovaliku
function tyhistaRadioOption(){
let elem=document.getElementsByName(“saiatyyp3”);
for (let i = 0; i < elem.length; i++){
elem[i].checked=false;
}
}
function tyhistaCheckOption() {
let elem = document.getElementsByName("saiatyyp4");
for (let i = 0; i < elem.length; i++) {
elem[i].checked = false;
}
}
//kontrollib et kogus väli ei ole tühi
function validateForm() {
let x = document.forms["kalk"]["kogus"].value;
if (x == "") {
alert("Palun sisesta kogus!");
return false;
}
}
//arvutame hind mis sõltub tekstist tekstkastist
function inputTextValue(){
tyhistaSelectOption();
tyhistaRadioOption();
tyhistaCheckOption();
//arvutab kindla hinnaga saia hind
validateForm();
let answer=document.getElementById("vastus");
let kogus=document.getElementById("kogus");
let inputValue=kogus.value;
let saiatyyp=document.getElementById("saiatyyp").value
if(saiatyyp==="juustu"){
answer.innerHTML="Hind on "+calculate(inputValue, juustuhind) +" euro"
}
else if(saiatyyp==="magus"){
answer.innerHTML="Hind on "+calculate(inputValue, magushind) +" euro"
}
else if(saiatyyp==="pontsik"){
answer.innerHTML="Hind on "+calculate(inputValue, pontsikhind) +" euro"
}
else{
answer.innerHTML="Saia tüüp määratud!";
}
}
function selectOptionChange(event){
tyhistaTextValue();
tyhistaRadioOption();
validateForm();
tyhistaCheckOption();
let answer=document.getElementById("vastus");
let kogus=document.getElementById("kogus");
let inputValue=kogus.value;
let saiatyyp2=event.target.value
if(saiatyyp2==="juustu"){
answer.innerHTML="Hind on "+calculate(inputValue, juustuhind) +" euro"
}
else if(saiatyyp2==="magus"){
answer.innerHTML="Hind on "+calculate(inputValue, magushind) +" euro"
}
else if(saiatyyp2==="pontsik"){
answer.innerHTML="Hind on "+calculate(inputValue, pontsikhind) +" euro"
}
else{
answer.innerHTML="Saia tüüp määratud!";
}
}
//radio nuppu valikud
function radioChange(event){
tyhistaSelectOption();
tyhistaTextValue();
validateForm();
tyhistaCheckOption();
let answer=document.getElementById("vastus");
let kogus=document.getElementById("kogus");
let inputValue=kogus.value;
if(event.target.id ==="juustu"){
answer.innerHTML="Hind on "+calculate(inputValue, juustuhind) +" euro"
}
else if(event.target.id==="magus"){
answer.innerHTML="Hind on "+calculate(inputValue, magushind) +" euro"
}
else if(event.target.id==="pontsik"){
answer.innerHTML="Hind on "+calculate(inputValue, pontsikhind) +" euro"
}
else{
answer.innerHTML="Saia tüüp määratud!";
}
answer.innerHTML="Hind on "+(kogus.value*saiahind).toFixed(2)+" euro";
}
//checkbox nuppu valikud
function checkChange() {
tyhistaSelectOption();
tyhistaTextValue();
tyhistaRadioOption();
validateForm();
let answer = document.getElementById("vastus");
let kogus = document.getElementById("kogus");
let inputValue = kogus.value;
let total = 0;
if (document.getElementById("juustu2").checked) {
total += inputValue * juustuhind;
}
if (document.getElementById("magus2").checked) {
total += inputValue * magushind;
}
if (document.getElementById("pontsik2").checked) {
total += inputValue * pontsikhind;
}
let formattedTotal = total.toFixed(2);
answer.textContent = "Hind on " + formattedTotal + " euro";
}form{
border: 1px solid grey;
border-radius: 60px;
padding: 2%;
Saia kalkulaator
Mitu saia sa soovid? (juustu – 1 €, magus – 2 €, pontsik – 3 €)