Zdravo,
Radim digitron i konverter u js-u i na samom sam kraju, ali mi iz nekog razloga stalno izbacuje default opciju u switchu, tj. ne reaguje uopšte kad izaberem +, -, /, *;
HTML deo:
<!DOCTYPE html>
<head>
<meta charset ="UTF-8">
<title>Konverter</title>
<script src="script.js" defer></script>
<link rel="stylesheet" href ="style.css">
</head>
<body>
<div class ="wrapper">
<p>Enter a value in grams</p>
<input type="text" id="grams"><br> <span id="error"></span>
<p>
Convert to
<select id="type">
<option value="Pounds">Pounds</option>
<option value="Kilograms">Kilograms</option>
<option value="Ounces">Ounces</option>
<option value="Stones">Stones</option>
</select>
</p>
<button id="btn">Convert</button>
<p id="output"></p> <br><br>
<p>Insert first number</p>
<input type="text" id="fn"><br> <span id="error1"></span>
<p>Insert second number</p>
<input type="text" id="sn"><br> <span id="error2"></span>
<p>
Choose an operation
<select id="type2">
<option value="Addition">+</option>
<option value="Subtraction">-</option>
<option value="Multiplication">*</option>
<option value="Division">/</option>
</select>
</p>
<button id="but">Execute</button>
<p id="output2"></p> <br><br>
</div>
</body>
JS deo:
var button = document.getElementById('btn');
button.addEventListener('click', function(){
const gram = parseInt(document.getElementById('grams').value);
const type = document.getElementById('type').value;
if(gram === ''|| isNaN(gram))
{
document.getElementById('grams').focus();
document.getElementById('error').innerHTML = 'Please, insert the value';
document.getElementById('output').innerHTML='';
}
else
{
document.getElementById('error').innerHTML='';
switch(type)
{
case 'Pounds':
{
convert_pounds(gram);
}break;
case 'Kilograms':
{
convert_kilogram(gram);
}
break;
case 'Ounces':
{
convert_Ounces(gram);
}
break;
case 'Stones':
{
convert_Stones(gram);
}
break;
default:
alert('Error');
}
function convert_pounds(gram){
var rate = 0.0022;
var pounds = rate * gram;
document.getElementById('output').innerHTML = gram + " grams = " + pounds.toFixed(3) + " pounds.";
}
function convert_kilogram(gram){
var rate = 0.0001;
var kilogram = rate * gram;
document.getElementById('output').innerHTML = gram + " grams = " + kilogram.toFixed(3) + " kg.";
}
function convert_Ounces(gram){
var rate = 0.035274;
var ounces = rate * gram;
document.getElementById('output').innerHTML = gram + " grams = " + ounces.toFixed(3) + " ounces.";
}
function convert_Stones(gram){
var rate = 0.0022;
var stones = rate * gram;
document.getElementById('output').innerHTML = gram + " grams = " + stones.toFixed(3) + " stones.";
}
}
});
var button2 = document.getElementById('but');
button2.addEventListener('click',function()
{
var br1 = parseInt(document.getElementById('fn').value);
var br2 = parseInt(document.getElementById('sn').value);
var type = document.getElementById('type2').value;
if(br1 === ''|| isNaN(br1) && br2 === '' || isNaN(br2))
{
document.getElementById('fn').focus();
document.getElementById('error1').innerHTML = 'Please, insert a value';
document.getElementById('sn').focus();
document.getElementById('error2').innerHTML = 'Please, insert a value';
document.getElementById('output2').innerHTML='';
}
else
{
document.getElementById('error1').innerHTML='';
document.getElementById('error2').innerHTML='';
switch(type)
{
case '+':
{
Add(br1,br2);
}
break;
case '-':
{
Subtract(br1,br2);
}
break;
case '*':
{
Multiply(br1,br2);
}
break;
case '/':
{
Divide(br1,br2);
}
break;
default:
alert('error');
break;
}
function Add(br1, br2)
{
var rez = br1 + br2;
document.getElementById('output2').innerHTML = br1 + " + " +br2 + " = " + rez;
}
function Subtract(br1, br2)
{
var rez = br1 - br2;
document.getElementById('output2').innerHTML = br1 + " - " +br2 + " = " + rez;
}
}
});
Konverter radi skroz ok, dok mi samo digitron neće iz nekog razloga.
Ovako izgleda:
|