{"id":"l-js-2-2","courseId":"javascript-fundamentals","lang":"en","kind":"lesson","summary":"Repeat work with for…of, the counting for loop and while, stop early with break and continue, and use the accumulator pattern to total a list of expenses.","html":"<p>So far every Pocket example added up a fixed number of expenses: <code dir=\"ltr\">coffee + lunch + bus</code>. Real spending is a list that grows every day. You cannot write <code dir=\"ltr\">+</code> for items you do not have yet. You need a way to say “do this for each one”, and that is a <strong>loop</strong>.</p>\n<h2 id=\"sec-1\" tabindex=\"-1\">A list to loop over</h2>\n<p>Lists get a full lesson in section 3, but you need the basics now. A list of values in square brackets is an <strong>array</strong>:</p>\n<div class=\"code-block\" dir=\"ltr\" data-lang=\"js\" data-run=\"js\" data-index=\"0\"><div class=\"code-head\"><span class=\"code-title\">JavaScript</span><span class=\"code-actions\"><button type=\"button\" class=\"code-btn code-run\" data-action=\"run\" data-lang=\"js\" data-index=\"0\" aria-label=\"Run this example\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><path d=\"M5 5a2 2 0 0 1 3.008-1.728l11.997 6.998a2 2 0 0 1 .003 3.458l-12 7A2 2 0 0 1 5 19z\"/></svg><span>Run</span></button><button type=\"button\" class=\"code-btn\" data-action=\"copy\" aria-label=\"Copy code\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"/><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"/></svg><span>Copy</span></button></span></div><pre class=\"shiki\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">const</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> amounts</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> [</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">450</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">1220</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">270</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">899</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">];</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">console.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">log</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(amounts.</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">length</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">);  </span><span style=\"color:#6E7781;--shiki-dark:#8B949E\">// how many items</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">console.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">log</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(amounts[</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">0</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">]);      </span><span style=\"color:#6E7781;--shiki-dark:#8B949E\">// the first item: positions start at 0</span></span></code></pre><div class=\"run-output\" data-run-output=\"0\" hidden></div></div>\n<h2 id=\"sec-2\" tabindex=\"-1\">for…of: do this for each item</h2>\n<p><code dir=\"ltr\">for...of</code> runs its block once per item, with a name of your choosing pointing at the current item:</p>\n<div class=\"code-block\" dir=\"ltr\" data-lang=\"js\" data-run=\"js\" data-index=\"1\"><div class=\"code-head\"><span class=\"code-title\">JavaScript</span><span class=\"code-actions\"><button type=\"button\" class=\"code-btn code-run\" data-action=\"run\" data-lang=\"js\" data-index=\"1\" aria-label=\"Run this example\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><path d=\"M5 5a2 2 0 0 1 3.008-1.728l11.997 6.998a2 2 0 0 1 .003 3.458l-12 7A2 2 0 0 1 5 19z\"/></svg><span>Run</span></button><button type=\"button\" class=\"code-btn\" data-action=\"copy\" aria-label=\"Copy code\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"/><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"/></svg><span>Copy</span></button></span></div><pre class=\"shiki\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">const</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> amounts</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> [</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">450</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">1220</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">270</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">899</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">];</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">let</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> total </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 0</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">;</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">for</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> (</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">const</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> amount</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> of</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> amounts) {</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">  total </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">+=</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> amount;</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">}</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">console.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">log</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(total);</span></span></code></pre><div class=\"run-output\" data-run-output=\"1\" hidden></div></div>\n<p>Walk through it the way the engine does. <code dir=\"ltr\">total</code> starts at 0. First pass: <code dir=\"ltr\">amount</code> is 450, so <code dir=\"ltr\">total</code> becomes 450. Second pass: <code dir=\"ltr\">amount</code> is 1220, <code dir=\"ltr\">total</code> becomes 1670. Then 1940, then 2839. There are no items left, so the loop ends and the next line logs the total.</p>\n<p>This shape is called the <strong>accumulator pattern</strong>: declare a result before the loop, update it inside, use it after. It is behind totals, counts, maximums and almost every summary you will write. The <code dir=\"ltr\">const amount</code> is fine even though it changes every pass: each pass creates a fresh <code dir=\"ltr\">amount</code>, it is never reassigned.</p>\n<p><code dir=\"ltr\">for...of</code> also works on strings, one character at a time: <code dir=\"ltr\">for (const ch of &quot;Pocket&quot;)</code>.</p>\n<h2 id=\"sec-3\" tabindex=\"-1\">The counting for loop</h2>\n<p>Sometimes you need the <strong>position</strong> as well as the item, for example to print “1. Coffee”. The classic <code dir=\"ltr\">for</code> loop has three parts in its parentheses:</p>\n<div class=\"code-block\" dir=\"ltr\" data-lang=\"js\" data-run=\"js\" data-index=\"2\"><div class=\"code-head\"><span class=\"code-title\">JavaScript</span><span class=\"code-actions\"><button type=\"button\" class=\"code-btn code-run\" data-action=\"run\" data-lang=\"js\" data-index=\"2\" aria-label=\"Run this example\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><path d=\"M5 5a2 2 0 0 1 3.008-1.728l11.997 6.998a2 2 0 0 1 .003 3.458l-12 7A2 2 0 0 1 5 19z\"/></svg><span>Run</span></button><button type=\"button\" class=\"code-btn\" data-action=\"copy\" aria-label=\"Copy code\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"/><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"/></svg><span>Copy</span></button></span></div><pre class=\"shiki\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">const</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> labels</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> [</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"Coffee\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"Lunch\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"Bus\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">];</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">for</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> (</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">let</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> i </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 0</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">; i </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">&#x3C;</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> labels.</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">length</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">; i</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">++</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">) {</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">  console.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">log</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">`${</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">i</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> +</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 1</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">}. ${</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">labels</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">[</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">i</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">]</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">}`</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">);</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">}</span></span></code></pre><div class=\"run-output\" data-run-output=\"2\" hidden></div></div>\n<figure class=\"diagram\"><svg class=\"diagram-svg\" focusable=\"false\" viewBox=\"0 0 660 230\" role=\"img\" aria-labelledby=\"l-js-2-2-t1\"><defs><marker id=\"arrow-l-js-2-2-1\" viewBox=\"0 0 10 10\" refX=\"9\" refY=\"5\" markerWidth=\"7\" markerHeight=\"7\" orient=\"auto-start-reverse\"><path d=\"M0 0 L10 5 L0 10 z\" class=\"d-arrowhead\"/></marker></defs>\n  <title id=\"l-js-2-2-t1\">A for loop runs the start part once, then repeatedly checks the condition, runs the body, and runs the update, until the condition is false and the loop exits.</title>\n  <rect class=\"d-box-primary\" x=\"20\" y=\"90\" width=\"140\" height=\"50\" rx=\"10\"/>\n  <text class=\"d-code\" x=\"90\" y=\"120\" text-anchor=\"middle\">let i = 0</text>\n  <rect class=\"d-box-accent\" x=\"220\" y=\"90\" width=\"190\" height=\"50\" rx=\"10\"/>\n  <text class=\"d-code\" x=\"315\" y=\"120\" text-anchor=\"middle\">i &lt; labels.length?</text>\n  <rect class=\"d-box\" x=\"470\" y=\"20\" width=\"170\" height=\"50\" rx=\"10\"/>\n  <text class=\"d-label\" x=\"555\" y=\"50\" text-anchor=\"middle\">run the body</text>\n  <rect class=\"d-box\" x=\"470\" y=\"160\" width=\"170\" height=\"50\" rx=\"10\"/>\n  <text class=\"d-code\" x=\"555\" y=\"190\" text-anchor=\"middle\">i++</text>\n  <rect class=\"d-box-success\" x=\"220\" y=\"175\" width=\"120\" height=\"40\" rx=\"10\"/>\n  <text class=\"d-label\" x=\"280\" y=\"200\" text-anchor=\"middle\">exit</text>\n  <path class=\"d-arrow\" d=\"M160 115 L216 115\" marker-end=\"url(#arrow-l-js-2-2-1)\"/>\n  <path class=\"d-arrow\" d=\"M400 95 L466 55\" marker-end=\"url(#arrow-l-js-2-2-1)\"/>\n  <text class=\"d-label-muted\" x=\"425\" y=\"62\" text-anchor=\"middle\">true</text>\n  <path class=\"d-arrow\" d=\"M555 70 L555 156\" marker-end=\"url(#arrow-l-js-2-2-1)\"/>\n  <path class=\"d-arrow\" d=\"M470 180 L414 135\" marker-end=\"url(#arrow-l-js-2-2-1)\"/>\n  <path class=\"d-arrow\" d=\"M280 140 L280 171\" marker-end=\"url(#arrow-l-js-2-2-1)\"/>\n  <text class=\"d-label-muted\" x=\"312\" y=\"162\" text-anchor=\"middle\">false</text>\n  <text class=\"d-label-muted\" x=\"90\" y=\"160\" text-anchor=\"middle\">runs once</text>\n</svg><figcaption>The three parts of a for loop and the order they run in</figcaption></figure>\n<p>The start, <code dir=\"ltr\">let i = 0</code>, runs once. Then, before every pass, the condition <code dir=\"ltr\">i &lt; labels.length</code> is checked; if it is true the body runs, then the update <code dir=\"ltr\">i++</code> adds one to <code dir=\"ltr\">i</code>. When the condition becomes false the loop ends. With three labels, <code dir=\"ltr\">i</code> takes the values 0, 1 and 2, exactly the valid indexes.</p>\n<aside class=\"callout callout-mistake\" role=\"note\"><p class=\"callout-title\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><path d=\"m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3\"/><path d=\"M12 9v4\"/><path d=\"M12 17h.01\"/></svg><span><span class=\"callout-kind\">Common mistake</span><span class=\"callout-sep\" aria-hidden=\"true\"> · </span>Off by one</span></p>\n<p><code dir=\"ltr\">i &lt;= labels.length</code> runs one extra pass with <code dir=\"ltr\">i</code> equal to 3, and <code dir=\"ltr\">labels[3]</code> is <code dir=\"ltr\">undefined</code>. Valid indexes go from 0 to <code dir=\"ltr\">length - 1</code>, so the condition is <code dir=\"ltr\">i &lt; length</code>. When you do not need the index, use <code dir=\"ltr\">for...of</code> and this mistake cannot happen.</p>\n</aside>\n<h2 id=\"sec-4\" tabindex=\"-1\">while: repeat until something changes</h2>\n<p>A <code dir=\"ltr\">while</code> loop has only a condition. It is for situations where you know when to stop but not how many passes it will take. How many days can you keep buying a $4.50 coffee on a $20 budget?</p>\n<div class=\"code-block\" dir=\"ltr\" data-lang=\"js\" data-run=\"js\" data-index=\"3\"><div class=\"code-head\"><span class=\"code-title\">JavaScript</span><span class=\"code-actions\"><button type=\"button\" class=\"code-btn code-run\" data-action=\"run\" data-lang=\"js\" data-index=\"3\" aria-label=\"Run this example\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><path d=\"M5 5a2 2 0 0 1 3.008-1.728l11.997 6.998a2 2 0 0 1 .003 3.458l-12 7A2 2 0 0 1 5 19z\"/></svg><span>Run</span></button><button type=\"button\" class=\"code-btn\" data-action=\"copy\" aria-label=\"Copy code\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"/><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"/></svg><span>Copy</span></button></span></div><pre class=\"shiki\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">let</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> balance </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 2000</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">;</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">const</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> coffee</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 450</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">;</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">let</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> days </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 0</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">;</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">while</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> (balance </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">>=</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> coffee) {</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">  balance </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">-=</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> coffee;</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">  days</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">++</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">;</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">}</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">console.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">log</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">`${</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">days</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">} coffees, ${</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">balance</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">} cents left`</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">);</span></span></code></pre><div class=\"run-output\" data-run-output=\"3\" hidden></div></div>\n<p>Each pass reduces <code dir=\"ltr\">balance</code>, so the condition eventually becomes false. If nothing in the body moved toward the exit, for example if you forgot <code dir=\"ltr\">balance -= coffee</code>, the condition would stay true forever. That is an <strong>infinite loop</strong>: the page freezes and the browser eventually offers to stop the script. When a tab hangs while you are practising, look for a loop whose condition never changes.</p>\n<h2 id=\"sec-5\" tabindex=\"-1\">break and continue</h2>\n<p>Two keywords change a loop’s flow from inside:</p>\n<ul>\n<li><code dir=\"ltr\">break</code> ends the loop immediately.</li>\n<li><code dir=\"ltr\">continue</code> skips the rest of this pass and moves to the next one.</li>\n</ul>\n<div class=\"code-block\" dir=\"ltr\" data-lang=\"js\" data-run=\"js\" data-index=\"4\"><div class=\"code-head\"><span class=\"code-title\">JavaScript</span><span class=\"code-actions\"><button type=\"button\" class=\"code-btn code-run\" data-action=\"run\" data-lang=\"js\" data-index=\"4\" aria-label=\"Run this example\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><path d=\"M5 5a2 2 0 0 1 3.008-1.728l11.997 6.998a2 2 0 0 1 .003 3.458l-12 7A2 2 0 0 1 5 19z\"/></svg><span>Run</span></button><button type=\"button\" class=\"code-btn\" data-action=\"copy\" aria-label=\"Copy code\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"/><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"/></svg><span>Copy</span></button></span></div><pre class=\"shiki\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">const</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> amounts</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> [</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">450</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">-</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">300</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">1220</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">270</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">899</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">];</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">const</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> budget</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 2000</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">;</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">let</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> running </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 0</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">;</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">for</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> (</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">const</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> amount</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> of</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> amounts) {</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">  if</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> (amount </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">&#x3C;</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 0</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">) {</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">    continue</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">; </span><span style=\"color:#6E7781;--shiki-dark:#8B949E\">// a refund: not spending, skip it</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">  }</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">  running </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">+=</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> amount;</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">  if</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> (running </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">></span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> budget) {</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">    console.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">log</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">`Budget passed at an expense of ${</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">amount</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">}`</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">);</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">    break</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">; </span><span style=\"color:#6E7781;--shiki-dark:#8B949E\">// no need to look further</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">  }</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">}</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">console.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">log</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(running);</span></span></code></pre><div class=\"run-output\" data-run-output=\"4\" hidden></div></div>\n<p>Trace it: 450 is added, then -300 is skipped, 1220 brings the total to 1670, 270 brings it to 1940, and 899 pushes it to 2839, which passes the budget, so the message is logged and the loop stops.</p>\n<h2 id=\"sec-6\" tabindex=\"-1\">Seeing what a loop does</h2>\n<p>Loops are where beginners most often lose track of what the program is doing, because the same lines run many times with different values. The cure is to make the loop show you. Put a <code dir=\"ltr\">console.log</code> at the top of the body that prints every variable involved:</p>\n<div class=\"code-block\" dir=\"ltr\" data-lang=\"js\" data-run=\"js\" data-index=\"5\"><div class=\"code-head\"><span class=\"code-title\">JavaScript</span><span class=\"code-actions\"><button type=\"button\" class=\"code-btn code-run\" data-action=\"run\" data-lang=\"js\" data-index=\"5\" aria-label=\"Run this example\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><path d=\"M5 5a2 2 0 0 1 3.008-1.728l11.997 6.998a2 2 0 0 1 .003 3.458l-12 7A2 2 0 0 1 5 19z\"/></svg><span>Run</span></button><button type=\"button\" class=\"code-btn\" data-action=\"copy\" aria-label=\"Copy code\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"/><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"/></svg><span>Copy</span></button></span></div><pre class=\"shiki\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">const</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> amounts</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> [</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">450</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">1220</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">270</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">];</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">let</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> total </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 0</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">;</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">for</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> (</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">let</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> i </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 0</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">; i </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">&#x3C;</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> amounts.</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">length</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">; i</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">++</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">) {</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">  console.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">log</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">({ i, amount: amounts[i], totalBefore: total });</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">  total </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">+=</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> amounts[i];</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">}</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">console.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">log</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">\"final\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, total);</span></span></code></pre><div class=\"run-output\" data-run-output=\"5\" hidden></div></div>\n<p>Each pass prints one line, so you can compare what actually happened with what you expected and find the first pass where they differ. That pass is where your bug is. Wrapping the values in <code dir=\"ltr\">{ }</code> prints them with their names, which saves you guessing which number is which. Professional developers do exactly this every day; in section 5 you will learn to do it with the DevTools debugger as well.</p>\n<aside class=\"callout callout-tip\" role=\"note\"><p class=\"callout-title\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><path d=\"M15 14c.2-1 .7-1.7 1.5-2.5 1-.9 1.5-2.2 1.5-3.5A6 6 0 0 0 6 8c0 1 .2 2.2 1.5 3.5.7.7 1.3 1.5 1.5 2.5\"/><path d=\"M9 18h6\"/><path d=\"M10 22h4\"/></svg><span><span class=\"callout-kind\">Tip</span><span class=\"callout-sep\" aria-hidden=\"true\"> · </span>Pick the loop by what controls it</span></p>\n<p>Walking through a list: <code dir=\"ltr\">for...of</code>. Need the position too: the counting <code dir=\"ltr\">for</code>. Stopping depends on a changing condition, not a list: <code dir=\"ltr\">while</code>. In section 3 you will meet array methods like <code dir=\"ltr\">map</code> and <code dir=\"ltr\">filter</code>, which replace many hand-written loops, but they are built on exactly these ideas.</p>\n</aside>\n<p>Loops and conditions together already let you write real logic. To reuse that logic instead of copying it, you need your own functions, which is the next lesson.</p>\n","toc":[{"id":"sec-1","text":"A list to loop over","level":2},{"id":"sec-2","text":"for…of: do this for each item","level":2},{"id":"sec-3","text":"The counting for loop","level":2},{"id":"sec-4","text":"while: repeat until something changes","level":2},{"id":"sec-5","text":"break and continue","level":2},{"id":"sec-6","text":"Seeing what a loop does","level":2}],"takeaways":["<code dir=\"ltr\">for...of</code> visits every item of a list in order and is the default loop for “do this for each expense”.","The counting <code dir=\"ltr\">for</code> loop gives you an index, which you need when position matters; valid indexes run from 0 to <code dir=\"ltr\">length - 1</code>.","<code dir=\"ltr\">while</code> repeats as long as a condition is true, so something inside the loop must eventually make it false.","The accumulator pattern (start at 0, add inside the loop, use after the loop) turns a list into a single total."],"further":[{"title":"Loops and iteration (MDN)","url":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration"},{"title":"for...of (MDN)","url":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of"}],"quiz":[{"q":"<p>What does this log?</p>\n<div class=\"code-block\" dir=\"ltr\" data-lang=\"js\" data-index=\"0\"><div class=\"code-head\"><span class=\"code-title\">JavaScript</span><span class=\"code-actions\"><button type=\"button\" class=\"code-btn\" data-action=\"copy\" aria-label=\"Copy code\"><svg class=\"icon \" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\" focusable=\"false\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"/><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"/></svg><span>Copy</span></button></span></div><pre class=\"shiki\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">const</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> amounts</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> [</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">400</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">900</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">250</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">];</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">let</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> total </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 0</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">;</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">for</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> (</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">const</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> a</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> of</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> amounts) {</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">  total </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> a;</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">}</span></span>\n<span class=\"line\"><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">console.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">log</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(total);</span></span></code></pre></div>\n","options":[{"text":"<code dir=\"ltr\">1550</code>","why":"That would need <code dir=\"ltr\">total += a</code>. Plain <code dir=\"ltr\">=</code> replaces the total with each amount instead of adding to it."},{"text":"<code dir=\"ltr\">250</code>","why":"Correct. Each pass overwrites <code dir=\"ltr\">total</code>, so it ends holding the last amount."},{"text":"<code dir=\"ltr\">400</code>","why":"The loop keeps going after the first item, and every pass reassigns <code dir=\"ltr\">total</code>."},{"text":"<code dir=\"ltr\">0</code>","why":"<code dir=\"ltr\">total</code> is reassigned inside the loop, so it no longer holds its starting value."}],"answer":1},{"q":"<p><code dir=\"ltr\">const days = [&quot;Mon&quot;, &quot;Tue&quot;, &quot;Wed&quot;];</code> Which loop throws no error but logs <code dir=\"ltr\">undefined</code> once at the end?</p>","options":[{"text":"<code dir=\"ltr\">for (let i = 0; i &lt; days.length; i++) console.log(days[i]);</code>","why":"This is the correct counting loop. It stops after index 2, the last valid index."},{"text":"<code dir=\"ltr\">for (const d of days) console.log(d);</code>","why":"<code dir=\"ltr\">for...of</code> visits exactly the items that exist, so it cannot run past the end."},{"text":"<code dir=\"ltr\">for (let i = 0; i &lt;= days.length; i++) console.log(days[i]);</code>","why":"Correct. <code dir=\"ltr\">&lt;=</code> lets <code dir=\"ltr\">i</code> reach 3, and <code dir=\"ltr\">days[3]</code> does not exist, so it logs <code dir=\"ltr\">undefined</code>. This off-by-one mistake is extremely common."}],"answer":2},{"q":"<p>When is <code dir=\"ltr\">while</code> a better fit than <code dir=\"ltr\">for...of</code>?</p>","options":[{"text":"When you do not know in advance how many repetitions you need, only when to stop.","why":"Correct. For example, “keep subtracting the daily spend until the balance runs out” has no list to walk through."},{"text":"When you want to visit every item in a list.","why":"That is exactly the job of <code dir=\"ltr\">for...of</code>, which cannot go past the end or forget to advance."},{"text":"When the loop body has more than one line.","why":"Every loop can have a body of any length. The choice depends on what controls the repetition."}],"answer":0},{"q":"<p>Inside a loop over expenses, what does <code dir=\"ltr\">continue</code> do?</p>","options":[{"text":"Ends the whole loop immediately.","why":"That is <code dir=\"ltr\">break</code>. <code dir=\"ltr\">continue</code> only skips the rest of the current pass."},{"text":"Skips the rest of the current pass and moves on to the next item.","why":"Correct. It is handy for ignoring items that do not apply, such as refunds, without nesting the rest of the body in an <code dir=\"ltr\">if</code>."},{"text":"Restarts the loop from the first item.","why":"Loops never rewind on their own. <code dir=\"ltr\">continue</code> moves forward to the next item."}],"answer":1}],"exercise":{"kind":"web","mode":"js","starter":{"js":"const amounts = [450, -300, 1220, 270, 899];\n\n// Total of all spending; negative amounts are refunds, skip them\nfunction totalSpent(amounts) {\n  let total = 0;\n  // loop here\n  return total;\n}\n\n// How many amounts are at least `minimum`\nfunction countAtLeast(amounts, minimum) {\n  return 0;\n}\n\n// The index of the expense at which the running total of spending\n// first goes over `budget`, or -1 if it never does (skip refunds here too)\nfunction firstOverBudget(amounts, budget) {\n  return -1;\n}\n\nconsole.log(totalSpent(amounts), countAtLeast(amounts, 500), firstOverBudget(amounts, 2000));\n"},"solution":{"js":"const amounts = [450, -300, 1220, 270, 899];\n\nfunction totalSpent(amounts) {\n  let total = 0;\n  for (const amount of amounts) {\n    if (amount < 0) {\n      continue;\n    }\n    total += amount;\n  }\n  return total;\n}\n\nfunction countAtLeast(amounts, minimum) {\n  let count = 0;\n  for (const amount of amounts) {\n    if (amount >= minimum) {\n      count++;\n    }\n  }\n  return count;\n}\n\nfunction firstOverBudget(amounts, budget) {\n  let running = 0;\n  for (let i = 0; i < amounts.length; i++) {\n    if (amounts[i] < 0) {\n      continue;\n    }\n    running += amounts[i];\n    if (running > budget) {\n      return i;\n    }\n  }\n  return -1;\n}\n\nconsole.log(totalSpent(amounts), countAtLeast(amounts, 500), firstOverBudget(amounts, 2000));\n"},"checks":[{"id":"total","test":"return totalSpent([450, -300, 1220, 270, 899]) === 2839 && totalSpent([]) === 0 && totalSpent([100, 200]) === 300;\n"},{"id":"count","test":"return countAtLeast([450, -300, 1220, 270, 899], 500) === 2 && countAtLeast([500, 499, 501], 500) === 2 && countAtLeast([], 1) === 0;\n"},{"id":"first-over","test":"return firstOverBudget([450, -300, 1220, 270, 899], 2000) === 4 && firstOverBudget([3000], 2000) === 0;\n"},{"id":"never-over","test":"return firstOverBudget([100, 200], 2000) === -1 && firstOverBudget([1000, 1000], 2000) === -1 && firstOverBudget([], 10) === -1;\n"}],"title":"Loop through Pocket's expenses","prompt":"<p>Each function receives <code dir=\"ltr\">amounts</code>, an array of cents where negative numbers are refunds. Fill in:</p>\n<ul>\n<li><code dir=\"ltr\">totalSpent(amounts)</code>: the sum of all spending, skipping refunds.</li>\n<li><code dir=\"ltr\">countAtLeast(amounts, minimum)</code>: how many amounts are greater than or equal to <code dir=\"ltr\">minimum</code>.</li>\n<li><code dir=\"ltr\">firstOverBudget(amounts, budget)</code>: the <strong>index</strong> of the expense at which the running total of spending first goes over <code dir=\"ltr\">budget</code> (skip refunds), or <code dir=\"ltr\">-1</code> if it never does.</li>\n</ul>\n","hints":["Each one is the accumulator pattern. Declare a result before the loop, update it inside, return it after.","<code dir=\"ltr\">if (amount &lt; 0) { continue; }</code> skips a refund. For <code dir=\"ltr\">firstOverBudget</code> you need the position, so use the counting loop: <code dir=\"ltr\">for (let i = 0; i &lt; amounts.length; i++)</code>.","In <code dir=\"ltr\">firstOverBudget</code>, as soon as <code dir=\"ltr\">running &gt; budget</code>, <code dir=\"ltr\">return i;</code> ends the function (and the loop). After the loop, <code dir=\"ltr\">return -1;</code>."],"explanation":"<p>All three functions share one shape: a variable set up before the loop, changed inside it, and returned after it. <code dir=\"ltr\">continue</code> lets you skip refunds without wrapping the rest of the body in another <code dir=\"ltr\">if</code>. <code dir=\"ltr\">firstOverBudget</code> needs the position, so it uses the counting loop with <code dir=\"ltr\">i &lt; amounts.length</code>, the exact range of valid indexes. Returning from inside the loop stops it at the first match, and the <code dir=\"ltr\">return -1</code> after the loop runs only when no match was ever found. Spending exactly the budget is not over it, so <code dir=\"ltr\">[1000, 1000]</code> with a budget of 2000 returns -1.</p>\n","checkLabels":{"total":"<code dir=\"ltr\">totalSpent</code> adds spending and skips refunds","count":"<code dir=\"ltr\">countAtLeast</code> counts amounts at or above the minimum","first-over":"<code dir=\"ltr\">firstOverBudget</code> returns the index where the budget is first passed","never-over":"<code dir=\"ltr\">firstOverBudget</code> returns -1 when the budget is never passed"}},"words":766,"readMinutes":4}