0

jsfiddle is here --> http://jsfiddle.net/diabetesjones/015k1tjn/5/

here's an example of one that does work: http://jsfiddle.net/Ahopefulmachine/dkhj8o38/

Regarding the second jsfiddle (not mine), I don't quite get why his works without using document.getElementById on the button.

i feel like my problem is in the click function itself of :

document.getElementById('mainButton').onclick = function () {

thanks :)

diabetesjones
  • 798
  • 1
  • 6
  • 17

2 Answers2

2

working now: http://jsfiddle.net/doniyor/015k1tjn/9/

you had document.getElementByID which should be document.getElementById

AND

  1. you didnot convert strings to int like numberOne = parseInt(numberOne, 10); where 10 is radix for decimal.
  2. you had question == ADD which should be question == 'ADD'
doniyor
  • 31,751
  • 50
  • 146
  • 233
2

There are three things wrong with your code:

  1. It's getElementById not getElementByID (case-sensitive).
  2. You didn't convert the strings you get from the input to numbers. You can do this easily by adding a + in front of the document.getElementById('number1').value;
  3. You were doing comparisons against variables, not strings. Ex question == ADD instead of question == 'ADD'

See corrected jsFiddle example

document.getElementById('mainButton').onclick = function () {

    //Getting values of inputs and saving them to variables
    var numberOne = +document.getElementById('number1').value;
    var numberTwo = +document.getElementById('number2').value;

    //Setting values of the equations
    var addition = (numberOne + numberTwo);
    var subtraction = (numberOne - numberTwo);
    var multiplication = (numberOne * numberTwo);
    var division = (numberOne / numberTwo);

    //Prompting user for their desired equation
    var question = prompt("Do you wish to ADD, SUBTRACT, MULTIPLY, or DIVIDE?").toUpperCase();

    //If statement to show the proper equation based on the user's prior prompt
    if (question == 'ADD') {
        alert('I added the shit out of those numbers for you - turns out it is ' + addition);
    } else if (question == 'SUBTRACT') {
        alert('Did some of this, some of that, some minusing - your answer is ' + subtraction);
    } else if (question == 'MULTIPLY') {
        alert('Yeah, I multipled the numbers, big whoop, wanna fight abouddit? the answers over there --> ' + multiplication);
    } else if (question == 'DIVIDE') {
        alert('This ones my favorite, I love a good division - ' + division);
    };

};
j08691
  • 190,436
  • 28
  • 232
  • 252
  • @doniyor - Yup. And I just noticed you edited your answer to include `parseInt`. Don't forget about the radix when using it. – j08691 Aug 07 '14 at 19:38
  • wait, so using + saves the time of parseint? in terms of what javascript is actually doing there, it is just running the parseint function on anything in front of the +? – diabetesjones Aug 07 '14 at 19:46
  • It's called a unary plus in JavaScript. See http://stackoverflow.com/questions/5450076/whats-the-significant-use-of-unary-plus-and-minus-operators, http://stackoverflow.com/questions/17106681/parseint-vs-unary-plus-when-to-use-which, and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Unary_plus_(.2B) – j08691 Aug 07 '14 at 19:48