<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://wiki.spheredev.org/index.php?action=history&amp;feed=atom&amp;title=JavaScript%2FHigher-order_programming%2FExample_3</id>
	<title>JavaScript/Higher-order programming/Example 3 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.spheredev.org/index.php?action=history&amp;feed=atom&amp;title=JavaScript%2FHigher-order_programming%2FExample_3"/>
	<link rel="alternate" type="text/html" href="http://wiki.spheredev.org/index.php?title=JavaScript/Higher-order_programming/Example_3&amp;action=history"/>
	<updated>2026-11-03T11:12:05Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.9</generator>
	<entry>
		<id>http://wiki.spheredev.org/index.php?title=JavaScript/Higher-order_programming/Example_3&amp;diff=838&amp;oldid=prev</id>
		<title>Apollolux: created</title>
		<link rel="alternate" type="text/html" href="http://wiki.spheredev.org/index.php?title=JavaScript/Higher-order_programming/Example_3&amp;diff=838&amp;oldid=prev"/>
		<updated>2013-06-15T00:44:40Z</updated>

		<summary type="html">&lt;p&gt;created&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Here&amp;#039;s a bunch of enemies we&amp;#039;re fighting against:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
var enemies = [&lt;br /&gt;
  {name: &amp;quot;Duck&amp;quot;, hp: 10},&lt;br /&gt;
  {name: &amp;quot;Duck&amp;quot;, hp: 10},&lt;br /&gt;
  {name: &amp;quot;Goose&amp;quot;, hp: 15}&lt;br /&gt;
];&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let&amp;#039;s kill them off:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
forEach(enemies, function (enemy) {&lt;br /&gt;
   enemy.hp = 0;&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now they&amp;#039;re all dead.&lt;br /&gt;
&lt;br /&gt;
Our problem is to find out when every enemy&amp;#039;s HP has dropped to zero.&lt;br /&gt;
===The chump&amp;#039;s way===&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
var allDead = true;&lt;br /&gt;
for (var i = 0; i &amp;lt; enemies.length; ++i) {&lt;br /&gt;
  if (enemies[i].hp &amp;gt; 0)&lt;br /&gt;
    allDead = false;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This presents another problem which isn&amp;#039;t in the previous example: what if the variable &amp;lt;var&amp;gt;i&amp;lt;/var&amp;gt; is being used for something? Battle systems tend to occur in loops, and for all we know, that loop could be using &amp;lt;var&amp;gt;i&amp;lt;/var&amp;gt;. We could shift onto variable &amp;lt;var&amp;gt;j&amp;lt;/var&amp;gt;, and then future code changes would move into variable &amp;lt;var&amp;gt;k&amp;lt;/var&amp;gt;... and it goes on. What a mess.&lt;br /&gt;
&lt;br /&gt;
Another problem was developing the algorithm itself: I had to stop and think, how should I do this?&lt;br /&gt;
&lt;br /&gt;
#	Should &amp;lt;var&amp;gt;allDead&amp;lt;/var&amp;gt; start false and turn into true when going through the loop and discovering all enemies are dead?&lt;br /&gt;
#	Should &amp;lt;var&amp;gt;allDead&amp;lt;/var&amp;gt; start true and turn into false when discovering a single living enemy?&lt;br /&gt;
&lt;br /&gt;
I got side-tracked by the &amp;quot;how&amp;quot;, and lost sight of the &amp;quot;what&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
===A smarter way===&lt;br /&gt;
We could take advantage of our higher-order functions to express our intent more clearly:&lt;br /&gt;
&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
var allDead = filter(function (enemy) {&lt;br /&gt;
  return enemy.hp &amp;gt; 0;&lt;br /&gt;
}, enemies).length === 0;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is easy to read: we filter through all enemies to get all the living ones in an array. If the length of that array is 0, they&amp;#039;re all dead. Again, shorter and a lot easier to read.&lt;br /&gt;
&lt;br /&gt;
Note also that it expresses what I was after: all enemies are dead if the length of living enemies found is zero.&lt;/div&gt;</summary>
		<author><name>Apollolux</name></author>
	</entry>
</feed>