Javascript get the sum of each array seperate -
i've been trying figure out how perform action , i'm sure i'm being idiot (new code here). have xml file (shown below), parsing , attempting build multiple tables each data set based on node "testset". xml file not change how it's designed, grow in how many "testsets" there are. have code below.
i have testspassed , testpassed showing 2 arrays need, can't figure out how sum of each array separate (and expect there unknown amount of arrays in further) , have write table. code below adds both parts of array in tests passed, need keep them separated. want array set 137, 70. thankful or suggestions.
xml
<?xml version="1.0" encoding="utf-8"?> <testsets> <testset> <testsetid>testset_ios1</testsetid> <hardware>ipad air 2</hardware> <version>ios 8.3</version> <build>pro 2.7.0.13 </build> <orientation>portrait</orientation> <numtestcases>46</numtestcases> <numtestscripts>17</numtestscripts> <totaltime>230891.363</totaltime> <testresults> <testrun> <date>24 mar 2015 09:12:00</date> <hardware>ipad air 2</hardware> <version>ios 8.1.3</version> <build>2.7.0.607</build> <orientation>portrait</orientation> <numpassed>37</numpassed> <numfailed>31</numfailed> </testrun> <testrun> <date>27 mar 2015 11:43:18</date> <hardware>ipad air 2</hardware> <version>ios 8.1.3</version> <build>pro 2.7.0.615</build> <orientation>portrait</orientation> <numpassed>100</numpassed> <numfailed>25</numfailed> </testrun> </testresults> </testset> <testset> <testsetid>testset_ios2</testsetid> <hardware>ipad air 2</hardware> <version>ios 8.1.3</version> <build>pro 2.7.0.623</build> <orientation>landscape</orientation> <numtestcases>38</numtestcases> <numtestscripts>9</numtestscripts> <totaltime>20800.255</totaltime> <testresults> <testrun> <date>30 mar 2015 10:29:00</date> <hardware>ipad air 2</hardware> <version>ios 8.1.3</version> <build>2.7.0.615</build> <orientation>landscape</orientation> <numpassed>34</numpassed> <numfailed>15</numfailed> </testrun> <testrun> <date>31 mar 2015 20:00:02</date> <hardware>ipad air 2</hardware> <version>ios 8.1.3</version> <build>pro 2.7.0.620</build> <orientation>landscape</orientation> <numpassed>10</numpassed> <numfailed>19</numfailed> </testrun> <testrun> <date>02 apr 2015 10:15:25</date> <hardware>ipad air 2</hardware> <version>ios 8.1.3</version> <build>pro 2.7.0.623</build> <orientation>landscape</orientation> <numpassed>26</numpassed> <numfailed>12</numfailed> </testrun> </testresults> </testset> </testsets> current code
document.write("<table><tr><th><st>test set id</st></th><th>hardware</th><th>op sys version</th><th>app build</th><th>orientation</th><th>number of test passed</th><th>number of test failed</th></tr>"); var x = xml.getelementsbytagname("testset"); var sum = 0; (i = 0; < x.length; i++) { document.write("<tr class='dynamictable'><td>"); document.write(x[i].getelementsbytagname("testsetid")[0].childnodes[0].nodevalue); document.write("</td><td>"); document.write(x[i].getelementsbytagname("hardware")[0].childnodes[0].nodevalue); document.write("</td><td>"); document.write(x[i].getelementsbytagname("version")[0].childnodes[0].nodevalue); document.write("</td><td>"); document.write(x[i].getelementsbytagname("build")[0].childnodes[0].nodevalue); document.write("</td><td>"); document.write(x[i].getelementsbytagname("orientation")[0].childnodes[0].nodevalue); var y = x[i].getelementsbytagname("testrun"); var testspassed = []; var testsfailed = []; (j = 0; j < y.length; j++) { testspassed.push(y[j].getelementsbytagname("numpassed")[0].childnodes[0].nodevalue); testsfailed.push(y[j].getelementsbytagname("numfailed")[0].childnodes[0].nodevalue); } (var k=0; k<testspassed.length; k++){ sum+=testspassed[k]<<0; } console.log(sum); }
probably not cleanest , best way here's did working:
document.write("<table><tr><th><st>test set id</st></th><th>hardware</th><th>op sys version</th><th>app build</th><th>orientation</th><th>number of test passed</th><th>number of test failed</th></tr>"); var x = xml.getelementsbytagname("testset"); (i = 0; < x.length; i++) { testspassed = []; testsfailed = []; passedsum = 0; failedsum = 0; var y = x[i].getelementsbytagname("testrun"); (j = 0; j < y.length; j++) { passed = (y[j].getelementsbytagname("numpassed")[0].childnodes[0].nodevalue); failed = (y[j].getelementsbytagname("numfailed")[0].childnodes[0].nodevalue); passedsum += passed << 0; failedsum += failed << 0; } document.write("<tr class='dynamictable'><td>"); document.write(x[i].getelementsbytagname("testsetid")[0].childnodes[0].nodevalue); document.write("</td><td>"); document.write(x[i].getelementsbytagname("hardware")[0].childnodes[0].nodevalue); document.write("</td><td>"); document.write(x[i].getelementsbytagname("version")[0].childnodes[0].nodevalue); document.write("</td><td>"); document.write(x[i].getelementsbytagname("build")[0].childnodes[0].nodevalue); document.write("</td><td>"); document.write(x[i].getelementsbytagname("orientation")[0].childnodes[0].nodevalue); document.write("</td><td>"); document.write(passedsum); document.write("</td><td>"); document.write(failedsum); document.write("</td></tr>"); }
Comments
Post a Comment