473,396 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Getting Sum of Variables in JS

57
Hi,
I have javascripts that display the number of radio stations from my xml files. I'm in need of a javascript that can add all the variables and then display the grand total. I looked on W3Schools's JS Math section and I couldn't find a section on addition.

Here's my individual scripts that display each state's # of stations:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.     xmlDoc=loadXMLDoc("stationsAK.xml");
  3.     x1=xmlDoc.getElementsByTagName('city');
  4.     document.write("Total Stations: " + x1.length);
  5. </script><br />
  6. <script type="text/javascript">
  7.     xmlDoc=loadXMLDoc("stationsAL.xml");
  8.     x2=xmlDoc.getElementsByTagName('city');
  9.     document.write("Total Stations: " + x2.length);
  10. </script><br />
  11.  
etc...all the way to:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.     xmlDoc=loadXMLDoc("stationsWY.xml");
  3.     x50=xmlDoc.getElementsByTagName('city');
  4.     document.write("Total Stations: " + x50.length);
  5. </script>
  6.  
I just need a script that can take the "length" of all the variables (x1 through x50) and return the total # of stations from all 50 states.

I'm assuming it's something like this:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.   document.write(Math.sum(x1,x2,....,x50))
  3. </script>
  4.  
Thanks in advance!
Dec 1 '07 #1
3 2437
gits
5,390 Expert Mod 4TB
hi ...

that will not work ... but you may use a simple function that does the trick for your purpose, define the following FIRST:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. var total_stations = 0;
  3.  
  4. function add_stations(val) {
  5.     total_stations += val;
  6. }
  7. </script>
  8.  
and everytime you use document.write you call the func to add the station-no:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.     xmlDoc =loadXMLDoc("stationsWY.xml");
  3.  
  4.     var x50 = xmlDoc.getElementsByTagName('city');
  5.     var stations = x50.length;
  6.  
  7.     document.write("Total Stations: " + stations);
  8.  
  9.     add_stations(stations);
  10. </script>
  11.  
now the global var total_stations has the sum of all stations assigned and you may alert or write it ... or whatever you want to do with it ;)

kind regards
Dec 1 '07 #2
npm
57
Hi,
Sorry it took so long to get back on your reply, this project/experiment had to be put on the back burner for a while.

But it worked great! Thanks!
Dec 24 '07 #3
gits
5,390 Expert Mod 4TB
glad to hear that ;) ... post back to the forum anytime you have more questions ...

kind regards
Dec 24 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: John Hunter | last post by:
I have a largish data set (1000 observations x 100 floating point variables), and some of the of the data are missing. I want to try a variety of clustering, neural network, etc, algorithms on the...
5
by: Nathan Sokalski | last post by:
I have a user control that contains three variables which are accessed through public properties. They are declared immediately below the "Web Form Designer Generated Code" section. Every time an...
9
by: Randy | last post by:
Hello, I'm having a strange problem. I've got a .NET web app which uses Session variables. Sometime, not all the time, they get cross threaded...that is...one user will have another user's Session...
2
by: maxkumar | last post by:
Hi, I am running a ASP.NET 1.1 site on Win Server 2003 with IIS 6.0. The website has been running for about 1.5 years now. In the past, we used to have random cases of session variables getting...
2
by: ronchese | last post by:
I'm noticing this since I started developing a website project: my session variables are getting empty! I did a real easy test, and I checked my session variable is getting empty!! What is...
10
by: Cliff | last post by:
Greetings, I have been trying to teach myself C++ over the past few weeks and have finally came across a problem I could not fix. I made a simple program that prints out a square or rectangle...
7
by: alphasahoo | last post by:
Hi I am working on a program which writes the output a SQL select statements from number of source tables first to a load matrix and then writes to a load.dat file. But while writing to the...
8
by: Samant.Trupti | last post by:
Hi All, I am facing a strange problem.. I am calling a function func2 from func1. Before calling func2 all the local variables in func1 looks fine and has their respective values. When...
4
by: icarus | last post by:
global_vars.py has the global variables set_var.py changes one of the values on the global variables (don't close it or terminate) get_var.py retrieves the recently value changed (triggered right...
1
by: Ramchandar | last post by:
Hi, I have an ASP.Net web application hosted in my local server with IIS 5.0 as web server. This worked fine with no issues. So I tried hosting the same into the server machine where I have IIS...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.