3

I'm using node.js, and I'm trying to add two integers, however, they just put them together... I've looked up Float, trying to float the integers, but node.js doesn't recognize float.

Roham Rafii
  • 2,929
  • 7
  • 35
  • 49
Christopher Allen
  • 7,769
  • 5
  • 20
  • 35

6 Answers6

11

Apparently at least one of both is actually a string containing a number. V8 then does string concatenation instead of adding the numbers.

What you need to do is to convert the strings to real numbers. You can do that using the parseInt() or parseFloat() functions, but a faster way is to subtract 0: As subtracting from a string is not possible, V8 tries to treat the content of the string as a number.

In the end you also get a number, but AFAIK this method is faster than using the parse functions.

Example:

var foo = '23';
typeof (foo - 0); // => 'number'

var a = '23',
    b = '42';
console.log((a - 0) + (b - 0)); // 65
Golo Roden
  • 140,679
  • 96
  • 298
  • 425
8

The best way is to cast it before doing any operation for example:

var result = Number(x1) + Number(x2) - Number(x3)

Source: http://www.w3schools.com/jsref/jsref_number.asp

Vijay13
  • 525
  • 2
  • 8
  • 18
2

In my node.js I did this.

this is the html file: index.html

<form action="/submission" method="POST"> 
First Number: <input name = "firstNumber" type="number" /> <br>
Second Number: <input name = "lastNumber" type="number" /> <br>
<input type="submit"/>
</form>

And this is the node.js file: node.js

Now, when you're doing this, you want to call it first too, so you can launch it on your local host, whatever that may be. You can make that choice.

var express = require('express');
var app = express();

var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended:false}));

Now I installed body parser up there from my terminal ^ which will be used on my app.post statement down below.

app.get('/', function(req, res){
    res.sendFile(__dirname + '/index.html'); //Gets the html
});



 app.post('/submission', function(req, res) {
   var first = parseInt(req.body.firstNumber);
   var second = parseInt(req.body.lastNumber);
    var sum = Number(first + second);
    res.send('The sum is: ' + Number(sum));
   });

Then don't forget to run your file on the terminal for your local host. Happy Coding!

Adan Vivero
  • 422
  • 12
  • 36
1

I have also same problem but solution is very simple... It's because of String data type

var variable_name="1"; // String Type
console.log(parseInt(variable_name)+1);
Muhammad Fahad
  • 1,352
  • 15
  • 15
  • 1
    As already mentioned in the [answer of a related question](https://stackoverflow.com/a/14496556/2375243), parsing a string to a number can be shortened using the `+` operator. `+"42" //42` – Stacky Aug 17 '18 at 08:03
0
**Add this to index.html file**

<form action="http://localhost:3000/sum" method="GET">
        <input id = "one" type="number" name="first" value="">
        <input id = "two" type="number" name="sec" value="">
        <input type="submit" value="OK">

</form>

**Add this to javascript file**

const express = require('express');
const app = express();

app.get('/sum',function(req,res){
    var a=Number(req.query.first);
    var b=Number(req.query.sec);
    var c;
    c=a+b;
    response = {
        result: c  
    };
    console.log(response);
    res.end(JSON.stringify(response));
}
);

Then run the file using node.js

-1

With a little delay, but for adding you can subtract the minus value, so

var result = a+b; //Strings appending

becomes

var result = a--b; //Integer a-(-b) --> a+b
Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94