Skip to main content

Posts

Showing posts with the label for-loop

For loop Variations in javascript

In this website there are a list of for loop variations. I can understand the usage of for(var i=0, len=arr.length; i<len ;i++) loop (where arr is an array), since the arr.length isnt calculated every step there is marginal performance gain. However what are the advantages of using the other variants? For instance loops like

for-loop and object control

I'm trying to add elements to an array. The elements of the array are of a custom class called variable. In the problematic for loop, it basically adds the last element trying to be added throughout the loop. Any help would be appreciated! import java.util.*; public class ThiefsDilemma2{ public static void main(String[] args){ ArrayList values = new ArrayList(args.length/2); Valuable[] array = new Valuable[args.length/2]; if(args.length%2 ==1){ int weight = Integer.parseInt(args[args.length-1]); boolean room = true; int tracker = 0; //problem!!!! Adds the last element throughout the loop for(int i = 0; i < args.length/2; i++){ array[i] = new Valuable( Integer.parseInt(args[args.length/2+i]), Integer.parseInt(args[i])); } for(int i = 0; i < args.length/2; i++){ System.out.println(array[i]); } while(values.size() > 0 && room){ int lightest = 1000

How to loop through and extract property values from a complex object (Array)?

The following snippet is from an opensource poker client written in JQuery. It displays a poker table that has been previously spawned by the poker server. The table is displayed within a div on the page after the page is loaded. // // featured table // jpoker.plugins.featuredTable = function(url, options) { var opts = $.extend({}, jpoker.plugins.featuredTable.defaults, options); var server = jpoker.url2server({ url: url }); server.registerUpdate(function(server, what, packet) { if (packet && packet.type == 'PacketPokerTableList') { if (packet.packets.length === 0) { var updated = function(server, what, packet) { if(packet && packet.type == 'PacketPokerTableList') { var found = null; for(var i = packet.packets.length - 1; i >= 0 ; i--) { var subpacket = packet.packets[i]; if(opts.compare(found, subpacket) >= 0) { found = subpacket;

Gathering segmented data via jQuery with FOR loops

Here's my code (what I'm stuck on is after the jump) <script language="JavaScript" type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script> <script type="text/javascript"> var countThisMany = 4; //how many data-id's I want to count per "section" of images var finalString = ""; //blank var to be used later, must be defined here. </script> <img src="foo.jpg" data-id="id1" data-item="1" /> // just a bunch <img src="foo.jpg" data-id="id2" data-item="2" /> // of images with <img src="foo.jpg" data-id="id3" data-item="3" /> // data-* usage to <img src="foo.jpg" data-id="id4" data-item="4" /> // store two bits <img src="foo.jpg" data-id="id5" data-item="5" /> // of information &l

For loop while parsing xml in php?

I am trying to create a for loop that will parse out different elements of xml doc using php. It parses when I put the number of the entry in the line: $s = $xml->entry[0]->children($namespaces['s']); However I tried to replace the number with "i" to count through each of the entries in the xml doc and I get an error (Fatal error: Call to a member function children() on a non-object in /home/content/c/a/s/cashme/html/buylooper/xml.php on line 10). How do I resolve this? <?php $url = 'xml-file.xml'; $xml = simplexml_load_file($url); $namespaces = $xml->entry->getNameSpaces(true); for ($i = 0; $i <= 10; $i += 1){ $s = $xml->entry[i]->children($namespaces['s']); $title = $s->product->title; //print echo $retailer; } ?>