Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to add two numbers in javascript #2355

Closed
meganga opened this issue Sep 6, 2018 · 3 comments
Closed

how to add two numbers in javascript #2355

meganga opened this issue Sep 6, 2018 · 3 comments
Labels

Comments

@meganga
Copy link

meganga commented Sep 6, 2018

My Code

'''js

function math (firstArg, secondArg, thirdArg) {
var g = secondArg * thirdArg
return g += firstArg;
}
console.log(math('53', '61', '67'));

the output it shows is 408753.
whereas, the solution is 4140.That is 4087+53.
it is joins the numbers rather than adding them.

@hackergrrl
Copy link

hackergrrl commented Sep 6, 2018 via email

@gagandeepp
Copy link

hi @gangasingh94160 in js if we using + operation with string variables it perform concatenation of string but if we use any mathematical operator with string it will perform mathematical operation . That's why your code give this output
61*67 = 4087
'4087'+'53'= 408753

To fix this convert the firstarg to number or call your method like this: console.log(math(53, '61', '67'));

If you like this explanation ,please provide thumbsup and close this issue

@stale
Copy link

stale bot commented Oct 31, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the inactive label Oct 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants