JavaScript Why is my addition and subtraction not working? addition subtraction working model,addition subtraction working wall,addition and

So I am using readline-sync to perform basic calculations. Multiplying and dividing work fine, but when I add both numbers it just concatenates them and when I subtract it returns NaN.

Any idea what I am doing wrong?

Here's my code:

var readlineSync = require("readline-sync");

var enterNum1 = readlineSync.question("Please enter your first number: ");
var enterNum2 = readlineSync.question("Please enter your second number: ");
var operations = ["add", "sub", "mul", "div"];
var selectOperation = readlineSync.keyInSelect(operations, "What operation will you like to perform?");


function addNum(num1, num2){
    console.log("The result is : " + Number(num1) + Number(num2));
}

function subNum(num1, num2){
    console.log("The result is : " + Number(num1) - Number(num2));
}

function mulNum(num1, num2){
    console.log( "The result is : " + Number(num1) * Number(num2));
}

function divNum(num1, num2){
    console.log("The result is : " + Number(num1) / Number(num2));
}

if(selectOperation === 0){
    addNum(enterNum1, enterNum2);
}else if(selectOperation === 1){
    subNum(enterNum1, enterNum2)
}else if(selectOperation === 2){
    mulNum(enterNum1, enterNum2);
}else {
    divNum(enterNum1, enterNum2);
}
Answer:1

Just wrap your operation in brackets

e.g.

console.log("The result is : " + (Number(num1) + Number(num2)));
console.log("The result is : " + (Number(num1) - Number(num2)));

Reasoning

As per operator precedence, multiplication and division works as they have higher precedence, i.e. they will be executed first. In case of addition and subtraction, the precedence is same and is operated from left to right. Hence, first the the string "The result is : " is added to Number(num1) and then added to Number(num2) which result in concatenation and for subtraction, it becomes trying to subtract a number from a string, resulting in NaN

For reference, Operator precedence

Answer:2

The javascript Date object parse method is "optimistic", that is, it fills in information that is missing with the earliest possible date. e.g. new Date('January 2016'); // = Jan 01 2016 new Date('...

The javascript Date object parse method is "optimistic", that is, it fills in information that is missing with the earliest possible date. e.g. new Date('January 2016'); // = Jan 01 2016 new Date('...

I have 2 div lines, each of them has 4 tabs. If I open one tab in one of the divs, I want the other div tabs to be closed. How can I do that? This is my code (see it live also on codepen): $(...

I have 2 div lines, each of them has 4 tabs. If I open one tab in one of the divs, I want the other div tabs to be closed. How can I do that? This is my code (see it live also on codepen): $(...

  1. instagram
  2. italy
  3. indeed
  4. ikea
  5. iphone 11
  6. instacart
  7. icloud
  8. italy news
  9. i still believe
  10. imdb
  11. impeachment
  12. ihop
  13. internet speed test
  14. iready
  15. iphone 11 pro max
  16. itunes
  17. infinite campus
  18. instant pot

Following the guide, I came to a sorts of middlewares are there for redux application. Redux Thunk, Redux Promise, Redux Promise Middleware, Redux Observable, Redux Saga, Redux Pack It is just a ...

Following the guide, I came to a sorts of middlewares are there for redux application. Redux Thunk, Redux Promise, Redux Promise Middleware, Redux Observable, Redux Saga, Redux Pack It is just a ...

I am animating the entry and exit of an array of items using TransitionGroup and CSSTransition (with a fade effect). I would like the items to appear with a slight delay between them instead of all at ...

I am animating the entry and exit of an array of items using TransitionGroup and CSSTransition (with a fade effect). I would like the items to appear with a slight delay between them instead of all at ...