Click here to Skip to main content
15,917,321 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
function calculate()
This function will take whatever the screen is currently displaying, turn it into an integer, and
store it in a global variable called num2. At this point, you have all the information you need to
carry out the computation. You have the global variable num1, which stores the first number.
You have the global variable called operation, which tells you what the user wanted to do with
the two numbers, and now you have a global variable num2 with the second number. At this
point, you’ll have to make a decision and figure out what operation the user wanted by
comparing the four choices you have against the global variable operation. Once you get a
match, carry out that operation and store the result of your computation in a global variable
called result. Finally, you will update the calculator’s display to show what the result of that
calculation was.

What I have tried:

JavaScript
var result;
var num2;
function calculate(){
  var current = document.getElementById("display").value;
current = num2;
 if(opPress == "+"){
 num1 + num2;
}
  if(opPress == "-"){
num1 - num2;
}
  if(opPress == "/"){
num1 / num2; 
}
  if(opPress == "*"){
num1 * num2;
}
result = operation; 
document.getElementById("display").value = result;
Posted
Updated 5-Dec-17 20:06pm
v2

Look at your code:

You get a screen value.
You store it.
You overwrite it with an undeclared variable so you don't have it any more.
You then look at a totally different variable (i.e. not operation) to decide what to do, and do something without storing the result anywhere.
Finally, you do use operation - but only to decide that that's the result and display that...

Stop guessing. Stop panicking. Neither of these is a viable design strategy.

Sit down, read your course notes through carefully. Twice. if anything doesn't make sense, read them again until they do. Now read the question again, carefully. And think about what it is trying to get you to do.
Then throw that code away and start again!
 
Share this answer
 
To answer you question (appeared in the title), Window prompt() Method[^]. The rest is your homework, start now...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900