Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
Js beginner calculator. How to make the first string empty in case it's an operator

works, but allows the first input to be an operator(+,-,*,/,=). How can I make the first input be number only?

const buttons = document.querySelectorAll('button');
const display = document.querySelector('.display');

added a variable to distinguish the operators

const operator = ['+', '-', '*', '/'];
buttons.forEach(function(button) {
  button.addEventListener('click', calculate);
});

function calculate(event) {
const clickedButtonValue = event.target.value;

tried this, to no effect

if (display.value[0] = operator) {
  display.value = '';
}
else if (clickedButtonValue === '=' && display.value !== '') {
  display.value = eval(display.value)
} else if (clickedButtonValue === 'C') {
  display.value = ''
} else {
  display.value += clickedButtonValue;
}


}

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

if (display.value[0] === operator) { //changed your = operator to ===
  display.value = '';
}

This should work, your problem was that you were assigning display.value[0] to operator instead of comparing them.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!