-1

I have this calculator that is very simple. I want to put little easter eggs, essentially, into the function of the code. Here is my codepen to show what I have done so far. I have commented out my attempts that haven't worked. I know that codepen doesn't show alerts so I have tried them the live page and the alerts still do not work. Here is the javascript

// Variables

var appendDigits = false
var prevResult = 0
var op = "="

//Functions

function clearDisplay() {
document.calculator.display.value = 0
prevResult = 0
appendDigits = false
op = "="
} //clear

function doOp(newop) 
{
var newArg =eval(document.calculator.display.value)
if (op == "+") {
    prevResult = prevResult + newArg        
}
else if (op == "-") {
    prevResult = prevResult - newArg        
}
else if (op == "/") {
    prevResult = prevResult / newArg
}
else if (op == "*") {
    prevResult = prevResult * newArg
}
else if (op == "=") {
    prevResult = newArg
}
 // else if (newArg == 42) {
 //   alert("You have found the meaning of life, the universe, and everything.");
 // }
    else {
        prevResult = newArg
    }
    document.calculator.display.value = prevResult
    appendDigits = false
    op = newop  
} //doOp

function digit(dig) {
    if (appendDigits) {
        document.calculator.display.value += dig
    }
    else {
        document.calculator.display.value = dig
        appendDigits = true
    }
}

//fuction meaning(document.calculator.display.value)
//{
//  if ( "newArg" == "42" ) {
//    alert("You have found the meaning of life, the universe, and everything.");
//  } 
//  else {
//    prevResult = newArg
//  }
//}
MaxTim
  • 21
  • 1
  • 4
  • I added an "alert('hello')" to the top of your JS code on codepen, and the alert pops up correctly in latest chrome. http://codepen.io/anon/pen/Kxekc – Will Sep 30 '14 at 19:11
  • you didn't spell function correctly in the commented code. Could that be your issue? – Jeff Sanders - MSFT Sep 30 '14 at 19:17

3 Answers3

0

use this to convert the Calculator display value to an int:

 else if (parseInt(newArg) == 42) {
   alert("You have found the meaning of life, the universe, and everything.");
 }
0

You need to add your check outside the if/else if loop that checks 'op' as otherwise one of the "if op ==" options will evaluate true and end the loop.

You could put it right after you assign newArg:

var newArg =eval(document.calculator.display.value)

  if (newArg == 42) {
    alert("You have found the meaning of life, the universe, and everything.");
  }
Joseph
  • 760
  • 1
  • 4
  • 9
0

If possible, try not to use the eval statement in JS. It's considered a bad practice.
See: Why is using the JavaScript eval function a bad idea?

Demo: http://codepen.io/anon/pen/Kxekc

Instead use the following to convert your input to a number:

  var newArg = parseInt(document.calculator.display.value,10);

  if (newArg == 42) {
    alert("You have found the meaning of life, the universe, and everything.");
  }

The function at the bottom is also mistyped: //fuction meaning(document.calculator.display.value)

Community
  • 1
  • 1
Will
  • 1,299
  • 9
  • 9