Search code examples
javascriptjavascript-objects

Javascript detects divide (/) as addition (+)


I am trying to make a basic, javascript based calculator using objects. For some reason, the property "calculator.divide" seems to return with an addition of the 2 numbers.

I have tried this in online compilers (js.do & code.sololearn.com) and notepad, but it doesn't seem to work.

var n1 = +(prompt("Enter 1st number:"));
var n2 = +(prompt("Enter 2nd number:"));
//gets user input & declares variables
//+ for changing string to integer
var calculator = {
 add: (n1 + n2), subtract: (n1 - n2), multiply: (n1 * n2), divide: (n1 / n2)
};
var operation = prompt("enter an operation: add, subtract, multiply, or divide");
if (operation = "add") {
    document.write(calculator.add);
}
else if (operation = "subtract") {
    document.write(calculator.subtract);
}
 else if (operation = "multiply") {
    document.write(calculator.multiply);
}
 else if (operation = "divide") {
    document.write(calculator.divide);
}

For example, if I enter 6 as my first number, and 2 as my second number, it was to my understanding that it would output "3" when "calculator.divide" is accessed. This does not seem to be the case. Instead, it outputs "8", as if it is adding them instead.


Solution

  • (operation = "add") is wrong , it has to be (operation === "add").Same for rest of the if. Instead of doing comparison , it is just assigning the value

    var n1 = +(prompt("Enter 1st number:"));
    var n2 = +(prompt("Enter 2nd number:"));
    //gets user input & declares variables
    //+ for changing string to integer
    var calculator = {
      add: (n1 + n2),
      subtract: (n1 - n2),
      multiply: (n1 * n2),
      divide: (n1 / n2)
    };
    var operation = prompt("enter an operation: add, subtract, multiply, or divide");
    if (operation === "add") {
      document.write(calculator.add);
    } else if (operation === "subtract") {
      document.write(calculator.subtract);
    } else if (operation === "multiply") {
      document.write(calculator.multiply);
    } else if (operation === "divide") {
      document.write(calculator.divide);
    }

    You can avoid if-else and use object look up

    var n1 = +(prompt("Enter 1st number:"));
    var n2 = +(prompt("Enter 2nd number:"));
    var operation = prompt("enter an operation: add, subtract, multiply, or divide");
    
    function execute(n1, n2, ops) {
      calculator = {
        add: (n1 + n2),
        subtract: (n1 - n2),
        multiply: (n1 * n2),
        divide: (n1 / n2),
      }
      return (calculator[ops]);
    
    }
    document.write(execute(n1, n2, operation.trim()))

    You can also avoid the the inner calculate function

    var n1 = +(prompt("Enter 1st number:"));
    var n2 = +(prompt("Enter 2nd number:"));
    var operation = prompt("enter an operation: add, subtract, multiply, or divide");
    
    function calculator(n1, n2, ops) {
      return {
        add: (n1 + n2),
        subtract: (n1 - n2),
        multiply: (n1 * n2),
        divide: (n1 / n2),
      }[ops];
    
    }
    document.write(calculator(n1, n2, operation.trim()))