Hallo,
ich habe versucht, einen Taschenrechner in JavaScript zu programmieren. Dazu habe ich mir Hilfe aus der Wiki geholt. Leider funktioniert alles bis auf die Berechnung. Kann mir vllt jemand weiterhelfen?
<head>
<title>Taschenrechner</title>
<style type="text/css">
h1 {
color: #000000;
}
</style>
<script type="text/javascript">
function Rechner () {
var zahl1;
var zahl2;
var ergebnis;
if(operator == "+") {
ergebnis = zahl1 - -zahl2;
}
if(operator == "-") {
ergebnis = zahl1 - zahl2;
}
if(operator == "*") {
ergebnis = zahl1 * zahl2;
}
if(operator == "/") {
ergebnis = zahl1 / zahl2;
}
window.onload = function () {
document.getElementById('rechnen').onclick = Rechner;
}
}
document.getElementById('ergebnis').value = ergebnis;
</script>
</head>
<body onload="Rechner()">
<h1>Taschenrechner</h1>
<form>
<p>
<label for="zahl1">erste Zahl:
<input id="zahl1" type="number" value="0">
</label>
</p>
<p>
<label for="operator">Operator:
<input id="operator" type="text">
</label>
<p>
<button type="button" id="plus">+</button>
<button type="button" id="minus">-</button>
<button type="button" id="mal">*</button>
<button type="button" id="durch">/</button>
</p>
<p>
<label for="zahl2">zweite Zahl:
<input id="zahl2" type="number" value="0">
</label>
</p>
<br>
<p><button type="button" id="rechnen">Berechnen</button></p>
<label>
Ergebnis:
<output id="ergebnis"></output>
</label>
</form>
</body>