I am making a little calculator (practicing with functions etc), but no matter what I fill out it will always return the value 0.
my HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Index</title>
<script type="text/javascript" src="index.js"></script>
<link rel="stylesheet" type="text/css" href="normalize.css">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="mainContainer">
<form>
<input type="number" id="value1">
<select id="operator"><option value="add">add</option><option value="minus">minus</option><option value="div">Divide</option><option value="mul">multiple</option></select>
<input type="number" id="value2">
<button id="calculate">Calculate</button>
</form>
</div>
</body>
</html>
My javascript:
function prepCalculate() {
let v1 = document.querySelector("#value1");
let v2 = document.querySelector("#value2");
if (isNaN(v1) || isNaN(v2)) {
window.alert('one of the inputs does not equal a nummeric value!');
return;
} else {
calculate();
}
}
prepCalculate();
function calculate() {
let v1 = document.querySelector("#value1");
let v2 = document.querySelector("#value2");
let operator = document.querySelector("#operator");
let cal;
if (operator == 'add') {
cal = v1 + v2;
} else if (operator == 'minus') {
cal = v1 - v2;
} else if (operator == 'div') {
cal = v1 / v2;
} else {
cal = v1 * v2;
}
alert(cal);
return cal;
}
My console.log()
doesn't show me any errors either... So my question: What am I doing wrong?