I'm doing an example for this javascript textbook and it's a simple calculator. There are two input (number) tags where the user selects a number and a select tag with options for a modifier (see code).
<input type="number" value="2" id='num1'>
<select id="operation">
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
<option>%</option>
</select>
<input type="number" value="2" id='num2'> =
<input type='text' readonly>
<br>
<input id="button1" type="button" value="Do It!">
What I have to do is get the values of both the numbers and then calculate the operation based on which modifier is selected in the options.
window.addEventListener("load", function() {
document.getElementById("button1").onclick = function(event) {
var num1 = document.getElementById("num1").value;
var num2 = document.getElementById("num2").value;
var mod = document.getElementById("operation").selectedIndex;
}
});
However, I don't understand how to do the calculation with the modifier. Would I need an if statement where it checks what value the mod has and then depending on that it does a certain calculation? Or is there a simpler way to just get the modifier and do a certain calculation that way?