Tengo que programar una calculadora para mi clase. No soy muy bueno en esto, así que lo hice exactamente como lo hicimos en clase, pero no funciona y no puedo encontrar el problema. No estoy buscando la solución completa, solo un consejo sobre dónde está el problema.
La calculadora consta de 2 campos de entrada para los números y 1 botón para sumar los números
HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <script src="node_modules"></script> <script src="script.js"></script> </head> <body> <label for="zahl1"></label> <input id="zahl1" type="text" placeholder="Zahl eingeben"> <label for="zahl2" type="text"></label> <input id="zahl2" type="text" placeholder="Zahl eingeben"> <button id="plusBtn" class="btn btn-info">+</button> <p id="ergebnis"></p> </body> </html> Typescript document.addEventListener("DomContentLoaded", () => { document.getElementById("plusBtn").addEventListener("click", () => { const zahl1Input = document.getElementById("zahl1") as HTMLInputElement; const zahl2Input = document.getElementById("zahl2") as HTMLInputElement; const zahl1: number = Number(zahl1Input.value); const zahl2: number = Number(zahl2Input.value); const sum = zahl1 + zahl2 let ergebnis: number; ergebnis = sum(zahl1, zahl2) const ergebnisInput = document.getElementById("ergebnis") as HTMLInputElement; ergebnisInput.value = ergebnis.toString(); }) })Arreglé el código. Compara las diferencias...
window.addEventListener("DOMContentLoaded", () => { document.getElementById("plusBtn").addEventListener("click", () => { const zahl1Input = document.getElementById("zahl1"); const zahl2Input = document.getElementById("zahl2"); const zahl1 = Number(zahl1Input.value); const zahl2 = Number(zahl2Input.value); const sum = zahl1 + zahl2 let ergebnis; ergebnis = sum const ergebnisInput = document.getElementById("ergebnis"); ergebnisInput.innerText = ergebnis.toString(); }) }) <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <script src="node_modules"></script> <script src="script.js"></script> </head> <body> <label for="zahl1"></label> <input id="zahl1" type="text" placeholder="Zahl eingeben"> <label for="zahl2" type="text"></label> <input id="zahl2" type="text" placeholder="Zahl eingeben"> <button id="plusBtn" class="btn btn-info">+</button> <p id="ergebnis"></p> </body> </html>ergebnis = sum(zahl1, zahl2)El problema está aquí. El uso de suma (argumentos) significa que está llamando a una función llamada 'suma', pero no ha hecho de 'suma' una función sino una variable que contiene la suma.
La forma correcta sería:
ergebnis = sumEsto funcionaría, pero puede optimizar aún más el código omitiendo variables innecesarias.