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.
You need to make sure that you get the latest value every time you run the
startfunction. So you need to move all the variable of input and select elements inside thestartfunction. Also when you are setting the return value ofstartfunction the element with idoutput, it doesn't run everytime the value changes since its outside of thestartfunction. 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 idoutputto show some value before user enters anything.<p id="output">0 ml</p>