aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes JavaScript array addition. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "JavaScript array addition." Watch "JavaScript array addition." New topic
Author

JavaScript array addition.

Victor Mendoza
Greenhorn

Joined: Jan 06, 2013
Posts: 2
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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!


Steve
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

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
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

And welcome to the Ranch, Victor!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Victor Mendoza
Greenhorn

Joined: Jan 06, 2013
Posts: 2
Problem solved. Thank you guys.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JavaScript array addition.