-1

I'm trying to build a calculator using JavaScript.I tried to display the value '0' in the screen,but it doesn't working.Can i know why is that ?

My js file

const calculator = {
  displayValue: '0';firstOperand: null;waitingForSecondOperand: false;operator: null;
}

function updateDisplay() {
  const display = document.querySelector('.calculator-dipalay');
  display.value = calculator.displayValue;
}

updateDisplay();

my HTML code

</html>

<head>
  <meta charset="UTF-8">
  <link rel="stylesheet" type="text/css" href="style.css">
  <script src="myscript.js"></script>
</head>

<body>

  <div class="calculator">

    <input type="text" class="calculator-dipalay" value="" disabled/>
  </div>
</body>

</html>
Eddie
  • 26,593
  • 6
  • 36
  • 58
Asha
  • 805
  • 3
  • 10
  • 18
  • `const calculator` object is incorrect format. – Eddie Apr 15 '19 at 13:14
  • Include your script at the end of your body. You are currently trying to access an element ('calculator-dipalay') that has not yet been loaded. – Christoph Herold Apr 15 '19 at 13:14
  • add script file after used selector elements or you should learn more about onload events if you are executing code before selectors – Girish Apr 15 '19 at 13:14
  • 1
    try putting your script tag as the last thing in your body tag. the JS file is probably getting called before your html is finished loading – jtylerm Apr 15 '19 at 13:14
  • 1
    i guess `document.querySelector('.calculator-dipalay')` is returning an array and afterwards you try to set the value to that array instead of setting it to the items inside it – messerbill Apr 15 '19 at 13:15
  • @messerbill — No. `querySelector` returns either an element or null. It never returns an array (nor does it return any kind of array-like object). You are probably confusing it for `querySelectorAll`. – Quentin Apr 15 '19 at 13:16
  • 1
    Aside from the problem described in the duplicate, you have some basic syntax errors. Open the developer tools in your browser. Look at the console. Read the error messages. – Quentin Apr 15 '19 at 13:16
  • @Quentin is right....like always :D https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector – messerbill Apr 15 '19 at 13:17
  • @jtylerm your idea was worked for me.Thank you – Asha Apr 15 '19 at 13:48

1 Answers1

2

Replace ; by , for constant and you must move <script src="myscript.js"></script> before </body> tag.

</html>
<head>
  <meta charset="UTF-8">
  <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
  <div class="calculator">
    <input type="text" class="calculator-dipalay" value="" disabled/>
  </div>
  <script src="myscript.js"></script>
</body>
</html>
const calculator={ 

And update:

    displayValue: '0',
    firstOperand: null, 
    waitingForSecondOperand: false,
    operator: null }

const calculator={ 
displayValue: '0',
firstOperand: null, 
waitingForSecondOperand: false,
operator: null }

function updateDisplay(){ 
const display = document.querySelector('.calculator-dipalay'); 
display.value = calculator.displayValue; 
}

updateDisplay();
<html>
<head>
  <meta charset="UTF-8">
      <link rel="stylesheet"  type="text/css" href="style.css" >
      <script src="myscript.js"></script>
</head>
  <body>

      <div class="calculator">

      <input type="text" class="calculator-dipalay" value="" disabled/>
</div>
</body>
</html>
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62