I’m stuck with another program I can’t seem to get right. For this case I have an index.html file (I can’t change anything in this) and the js code I made for it.
In this case I need to add the logic for a kitchen scale. I need to capture the various inputs, so the number in the text field and the input from the start (startEenheid) select and conversion (convertEenheid) select, then calculate the correct output and write this to the output paragraph. So if the start number = 10ml and I need to convert it to cl, I’ll need to calculate this and then have an output of (in this case) 1 cl below.
I’ve been struggeling with this for a 2 days now. At first it showed undefined + the conversionrate as output but after a lot of random changes it’s now at 0 instead of undefined (progress yay). This is ofcourse still not the right output but whatever I do, I just can seem to figure out what it is that makes it not calculate and output the right answer instead of 0.
The fix is probably an easy one but my beginner programming brain can’t seem to grasp what it is. I do like the challenge exercises like this give but it gets very annoying when you get stuck and can’t find the answer.
let input = document.getElementById("invoer").innerText;
let invoer = document.getElementById("invoer");
let invoerNummer = parseInt(invoer, 10);
let startEenheid = document.getElementById("startEenheid").value;
let startButton = document.getElementById("startEenheid");
let convertEenheid = document.getElementById("convertEenheid").value;
let convertButton = document.getElementById("convertEenheid");
let select1;
let select2;
const conversie = [1, 10, 100, 1000];
startButton.addEventListener("input", start);
convertButton.addEventListener("input", start);
invoer.addEventListener("input", start);
function start() {
let temp;
let output;
if (startEenheid === "ml") {
temp = input / conversie[3];
} else if (startEenheid === "cl") {
temp = input / conversie[2];
} else if (startEenheid === "dl") {
temp = input / conversie[1];
} else {
temp = input;
}
if (convertEenheid === "ml") {
output = temp * conversie[3];
} else if (convertEenheid === "cl") {
output = temp * conversie[2];
} else if (convertEenheid === "dl") {
output = temp * conversie[1];
} else {
output = temp;
}
return output;
}
document.getElementById("output").innerText = `${start()} ${convertEenheid}`;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<script src="script.js" defer></script>
<title>My Kitchen Aid</title>
</head>
<body>
<div class="kitchenAid">
<h1>KitchenAid</h1>
<div class="row">
<label for="invoer">Invoer:</label>
<input type="text" id="invoer">
</div>
<div class="row">
<label for="startEenheid">Start Eenheid</label>
<select name="startEenheid" id="startEenheid">
<option value="ml">ml</option>
<option value="cl">cl</option>
<option value="dl">dl</option>
<option value="l">l</option>
</select>
</div>
<div class="row">
<label for="convertEenheid">Gewenste Eenheid</label>
<select name="convertEenheid" id="convertEenheid">
<option value="ml">ml</option>
<option value="cl">cl</option>
<option value="dl">dl</option>
<option value="l">l</option>
</select>
</div>
<p id="output">-</p>
</div>
</body>
</html>
After getting to the output being 0 I tried using parseInt. This was more of a random solution as I honestly didn’t know what the problem was. I tried using 2 functions instead of one but this honestly made it more difficult for myself haha.
2
Answers
You need to make sure that you get the latest value every time you run the
start
function. So you need to move all the variable of input and select elements inside thestart
function. Also when you are setting the return value ofstart
function the element with idoutput
, it doesn’t run everytime the value changes since its outside of thestart
function. Here is the bare minimum changes for it to work. Please modify as per your needs. Also you can provide some initial value like 0 within your p tag with idoutput
to show some value before user enters anything.<p id="output">0 ml</p>
Since you can’t change the HTML you will need to bind an event listener to each input element that can trigger a change. In your case you have an
<input>
and 2<select>
elements. If any of them change you want the conversion to happen.The crux of this problem is to convert whatever value the user types into your smallest base unit and then convert to whatever output value the user selects. In other words, just convert the input value to
ml
and then you will be able to multiply consistently.In the code below I have created a
Map
which is a key-value map object to make doing the conversion consistent. Maps come with an easyget()
method so the conversion can be done with a couple of lines of code: