Advertisement
OpataJoshua

Untitled

Oct 6th, 2022
760
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function calculate(event) {
  2.   // stop form from reloading the page on submit
  3.   event.preventDefault();
  4.  
  5.   // declare variables to hold the form data we need
  6.   let firstNumber = event.target.elements.num1.value;
  7.   let secondNumber = event.target.elements.num2.value;
  8.   let op = event.target.elements.operator.value;
  9.   let answerEl = document.getElementById('answer-value');
  10.  
  11.   if (firstNumber == "" || secondNumber == ""){
  12.     answerEl.innerText = "First number and second number must not be empty";
  13.     answerEl.style.color = "red";
  14.     return;
  15.   }
  16.  
  17.   answerEl.innerText = eval(firstNumber+op+secondNumber);
  18.   answerEl.style.color = "green";
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement