General discussion

  • Creator
  • #4078980

    JavaScript Calculator Program Error

    by gulshan212 ·

    Hello this is Gulshan Negi
    Well, I am writing a program for making calculator using JavaScript but it shows some error at the time of its execution.
    I am getting error in this phase and here is my source code:

    const calculator = {
    displayValue: ‘0’,
    firstOperand: null,
    waitingForSecondOperand: false,
    operator: null,

    function inputDigit(digit) {
    const { displayValue, waitingForSecondOperand } = calculator;

    if (waitingForSecondOperand === true) {
    calculator.displayValue = digit;
    calculator.waitingForSecondOperand = false;
    } else {
    calculator.displayValue = displayValue === ‘0’ ? digit : displayValue + digit;

    function inputDecimal(dot) {
    if (calculator.waitingForSecondOperand === true) {
    calculator.displayValue = “0.”
    calculator.waitingForSecondOperand = false;

    I also checked and took a reference from a site but I dont know what thing I am missing.
    Can anyone give their suggestions on this ?

    Note: URL of site referenced removed by moderator.

    • This topic was modified 1 year, 2 months ago by Avatar photokees_b.

You are posting a reply to: JavaScript Calculator Program Error

The posting of advertisements, profanity, or personal attacks is prohibited. Please refer to our Community FAQs for details. All submitted content is subject to our Terms of Use.

All Comments

  • Author
    • #4078981

      Reply To: JavaScript Calculator Program Error

      by lokeshjoshi0996 ·

      In reply to JavaScript Calculator Program Error

      Hi @gulshan212

      You can try this code:

      <!DOCTYPE html>
      <title>JavaScript Calculator</title>
      function calculate() {
      var num1 = parseFloat(document.getElementById(“num1”).value);
      var num2 = parseFloat(document.getElementById(“num2”).value);
      var operator = document.getElementById(“operator”).value;
      var result;

      if (operator === “+”) {
      result = num1 + num2;
      } else if (operator === “-“) {
      result = num1 – num2;
      } else if (operator === “*”) {
      result = num1 * num2;
      } else if (operator === “/”) {
      result = num1 / num2;

      document.getElementById(“result”).value = result;
      <h1>JavaScript Calculator</h1>
      <label for=”num1″>Number 1:</label>
      <input type=”text” id=”num1″><br><br>
      <label for=”num2″>Number 2:</label>
      <input type=”text” id=”num2″><br><br>
      <label for=”operator”>Operator:</label>
      <select id=”operator”>
      <option value=”+”>+</option>
      <option value=”-“>-</option>
      <option value=”*”>*</option>
      <option value=”/”>/</option>
      <button onclick=”calculate()”>Calculate</button><br><br>
      <label for=”result”>Result:</label>
      <input type=”text” id=”result” readonly>

      You can run this code; I hope this will help you. In the code that you shared, add “:” after } in the 6th row. I also checked the reference that you have mentioned the same thing is missing.

Viewing 0 reply threads