Christopher Allen
Christopher Allen

Reputation: 8589

I'm using node.js, and I'm trying to add two integers, however, they just put them together

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.

Upvotes: 3

Views: 34281

Answers (6)

Adan Vivero
Adan Vivero

Reputation: 422

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!

Upvotes: 2

MEHA RAVI
MEHA RAVI

Reputation: 11

**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

Upvotes: 0

Golo Roden
Golo Roden

Reputation: 150684

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

Upvotes: 11

Muhammad Fahad
Muhammad Fahad

Reputation: 1412

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);

Upvotes: 1

Vijay13
Vijay13

Reputation: 605

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

Upvotes: 8

Roee Gavirel
Roee Gavirel

Reputation: 19443

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

Upvotes: -1

Related Questions