Tundra Posted February 21, 2008 Share Posted February 21, 2008 I am pulling two elements from a XML feed with numbers as their value (please correct my terminology if used incorrectly)I want to take the first value, lets call it <element1> and divide it by <element2> to produce something similar to this: document.write("<td class='02'>");document.write("Quotient");document.write("</td>"); Here is what I tried to do: (divide <numUnitsKilled> by <numUnitsLost>) document.write("<td class='01'>");document.write("numUnitsKilled" / "numUnitsLost");document.write("</td>"); Any thoughts? Link to comment Share on other sites More sharing options...
Tundra Posted February 22, 2008 Author Share Posted February 22, 2008 I am pulling two elements from a XML feed with numbers as their value (please correct my terminology if used incorrectly)I want to take the first value, lets call it <element1> and divide it by <element2> to produce something similar to this:document.write("<td class='02'>");document.write("Quotient");document.write("</td>"); Here is what I tried to do: (divide <numUnitsKilled> by <numUnitsLost>) document.write("<td class='01'>");document.write("numUnitsKilled" / "numUnitsLost");document.write("</td>"); Any thoughts? I've been studying JS but cant find how to use JS to divide two numbers.... Link to comment Share on other sites More sharing options...
boen_robot Posted February 22, 2008 Share Posted February 22, 2008 What you really need to do is convert the object to a number, after which you can divide it as you have tried to already.Instead of document.write("8" / "2"); you need to do document.write(8 / 2); and in order to turn a string into a number, you can use the Number() object like: document.write(new Number("8") / new Number("2")); Of course, you have to make sure the argument of the Number() object is convertable to a number. If the value of the element in your XML is a number, then you're set. eg. document.write(new Number(xmlDoc.getElementByTagName('element1').innerHTML) / new Number(xmlDoc.getElementByTagName('element2').innerHTML)); (again assuming "xmlDoc" is the XML document, which is already loaded) Link to comment Share on other sites More sharing options...
jesh Posted February 22, 2008 Share Posted February 22, 2008 "numUnitsKilled" / "numUnitsLost"This is telling the javascript parsing engine that you want to divide two strings rather than two numbers and there is no meaning in dividing one string from another.If you have two nodes - numUnitsKilled and numUnitsLost - which contain, in a text node, numbers that represent these values, then you'll have to get those values, make certain that they are numbers (and that numUnitsLost isn't 0), convert those values to numbers and then perform the division.Something along these lines. I haven't tested this so there may be an error or two, but it should point you in the right direction:var parentNode = document.getElementsByTagName("someparentnode")[0];var killedNode = parentNode.getElementsByTagName("numUnitsKilled")[0];var lostNode = parentNode.getElementsByTagName("numUnitsLost")[0];var numUnitsKilled = 0;var numUnitsLost = 0;if(killedNode && killedNode.nodeValue != "" && isNaN(killedNode.nodeValue) == false){ numUnitsKilled = Number(killedNode.nodeValue);}if(lostNode && lostNode.nodeValue != "" && isNaN(lostNode.nodeValue) == false){ numUnitsLost = Number(lostNode.nodeValue);}if(numUnitsLost > 0){ document.write(numUnitsKilled / numUnitsLost);} Link to comment Share on other sites More sharing options...
Tundra Posted February 23, 2008 Author Share Posted February 23, 2008 This looks like it should work but I may be messing it up by forcing it into my script table: document.write("<td class='01'>");var parentNode = document.getElementsByTagName("player")[0];var killedNode = parentNode.getElementsByTagName("numUnitsKilled")[0];var lostNode = parentNode.getElementsByTagName("numUnitsLost")[0];var numUnitsKilled = 0;var numUnitsLost = 0;if(killedNode && killedNode.nodeValue != "" && isNaN(killedNode.nodeValue) == false){ numUnitsKilled = Number(killedNode.nodeValue);}if(lostNode && lostNode.nodeValue != "" && isNaN(lostNode.nodeValue) == false){ numUnitsLost = Number(lostNode.nodeValue);}if(numUnitsLost > 0){ document.write(numUnitsKilled / numUnitsLost);}document.write("</td>"); Do you see anything wrong with doing that? Right now it doesnt work, just prints a blank cell.Thanks again! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.