Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
var read=require('readline-sync')

var num1=read.question('Enter first number:')

var num2=read.question('Enter second number')

function sum(num1,num2){
  var total=num1+num2
    console.log('sum of number is :'+total)
}
sum(num1,num2);


What I have tried:

Enter first number:12
Enter second number:32
sum of number is : 1232
Posted
Updated 6-Feb-22 1:31am

Inputs are actually strings, not numbers. if you want sum these two numbers
you need to convert into Integer before sum them or convert into integer while
taking input. like:
var num1 = parseInt(read.question('Enter first number:'));
var num2 = parseInt(read.question('Enter second number:'));
OR User the ParseInt method to convert into integer datatype in your Sum method.
var total=parseInt(num1)+parseInt(num2)
 
Share this answer
 
Quote:
Result is shown as string while using node js

Yes, this is exactly what your code is doing as required.
If num1 and num2 must be seen as integers, you must tell it in code.
How to Convert a String to a Number in Node.js[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900