My code is focused on cooking (Banana Bread recipe). Depending on the number of people, I will sometimes make two Banana Bread's as opposed to one. Thus, I use the selection tool to account for this by changing the amount of each ingredient. Yet, my problem is JavaScript converting the fractions into decimals. I want to keep the numbers in fractions because this is how the majority of cooking is done.
Ideal Example
If 1 was selected, it would say 2 cups flour, ½ tsp salt.
If 2 was selected, it would say 4 cups flour, 1 tsp salt.
If 3 was selected, it would say 6 cups flour, 1 ½ tsp salt.
What actually happens:
If 1 was selected, it would say 2 cups flour, 0.5 tsp salt.
If 2 was selected, it would say 4 cups flour, 1 tsp salt.
If 3 was selected, it would say 6 cups flour, 1.5 tsp salt.
Code:
<body>
<script>
// Step 1: Find the element we want the event on
var button = document.getElementById("button");
// Step 2: Define the event listener function
var onButtonClick = function() {
var selection = document.getElementById("quantity").value;
const breadsQuantity = parseInt(selection, 10);
document.getElementById('amount').innerHTML = breadsQuantity * 2;
document.getElementById('amount2').innerHTML = breadsQuantity * 0.5;
}
// Step 3: Attach event listener to element
button.addEventListener("click", onButtonClick);
</script>
<!-- HTML SNIPPIT -->
<label>How many Banana Bread's are you making? </label>
<select id="quantity">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select><br><br>
<button id="button" type="button">
Let's get started!
</button>
<p>
Step 1: Add
<span id="amount">2</span> cups flour and
<span id="amount2">½</span>
tsp salt into a large, dry bowl.
</p>
</body>