File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JavaScript array addition.

 
Victor Mendoza
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys new here, I hope somebody can help me. I have this code and I need to add the numbers but unfortunately the code only puts the number together but doesn't add them, any suggestions?

Thank you.





for "+a[0][0]+a[1][0]+" I want to get the result of the addition but I only get the two numbers together. Thanks for the heads up Steve.


 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a lot of code with a lot of markup mixed in with the behavior. I haven't a clue what the problem is or where you see it. Can you take all the unnecessary bits out and just show code which pertains to the problem you are having?

Also, with your next post, please remember to UseCodeTags (<- click). It makes the code a lot easier to read!
 
Paul Clapham
Sheriff
Pie
Posts: 20177
25
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the "+" operator when applied to two strings produces the two strings concatenated together. Did you imagine that it would examine the two strings to see if they could be interpreted as integers, and if both of them could be, then it would add the two integers together and convert the result back to a string? No, it doesn't do anything complicated like that.

If you want the "+" operator to add two integers together in the normal arithmetic way, then give it two integers. Simple as that.
 
Paul Clapham
Sheriff
Pie
Posts: 20177
25
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch, Victor!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64182
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, document.write is considered quite the dinosaur for creating DOM elements. You should be using the DOM API or, preferably, jQuery to create DOM elements dynamically.

Getting rid of all that string concatenation would go a long way to not only eliminating problems such as you are having, it'll be much more easily understood and readable.

And, I'm confused as to why you are using an array to hold the values in the first place. That's certainly not helping the understandability or the readability.
 
Victor Mendoza
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem solved. Thank you guys.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic