{"id":"l-js-2-3","courseId":"javascript-fundamentals","lang":"en","kind":"lesson","summary":"Write your own functions as declarations and arrow functions, pass arguments in and get results out with return, give parameters default values, and pass functions to other functions.","html":"<p>In the last two lessons you filled in function bodies that were written for you. Now you write your own. A <strong>function</strong> is a named, reusable piece of a program: you write the steps once and run them whenever you need, with different inputs. Pocket formats money in a dozen places; without functions you would copy <code dir=\"ltr\">(cents / 100).toFixed(2)</code> into every one, and fix every copy when the format changes.</p>\n<h2 id=\"sec-1\" tabindex=\"-1\">Declaring and calling a function</h2>\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\">function</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\"> formatMoney</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#953800;--shiki-dark:#FFA657\">cents</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\"> dollars</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> (cents </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">/</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 100</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">).</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">toFixed</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">2</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">);</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">  return</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\"> `$${</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">dollars</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>\n<span class=\"line\"></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:#8250DF;--shiki-dark:#D2A8FF\">formatMoney</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>\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:#8250DF;--shiki-dark:#D2A8FF\">formatMoney</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></code></pre><div class=\"run-output\" data-run-output=\"0\" hidden></div></div>\n<p>The first five lines are a <strong>function declaration</strong>. They do not run anything; they create a function named <code dir=\"ltr\">formatMoney</code> and remember its steps. <code dir=\"ltr\">cents</code> is a <strong>parameter</strong>, a name for an input that does not have a value yet.</p>\n<p><code dir=\"ltr\">formatMoney(1220)</code> is a <strong>call</strong>. The value in the parentheses, <code dir=\"ltr\">1220</code>, is an <strong>argument</strong>. When the engine reaches the call, it pauses the current line, jumps into the function with <code dir=\"ltr\">cents</code> pointing at 1220, runs the body, and comes back when it hits <code dir=\"ltr\">return</code>. The call expression is then replaced by the returned value, <code dir=\"ltr\">&quot;$12.20&quot;</code>, which <code dir=\"ltr\">console.log</code> prints.</p>\n<figure class=\"diagram\"><svg class=\"diagram-svg\" focusable=\"false\" viewBox=\"0 0 660 220\" role=\"img\" aria-labelledby=\"l-js-2-3-t1\"><defs><marker id=\"arrow-l-js-2-3-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-3-t1\">The call formatMoney(1220) passes 1220 into the parameter cents. The function body runs and returns the string $12.20, which replaces the call in the original line.</title>\n  <rect class=\"d-box\" x=\"20\" y=\"30\" width=\"250\" height=\"50\" rx=\"10\"/>\n  <text class=\"d-code\" x=\"145\" y=\"60\" text-anchor=\"middle\">formatMoney(1220)</text>\n  <rect class=\"d-box-primary\" x=\"380\" y=\"20\" width=\"260\" height=\"170\" rx=\"12\"/>\n  <text class=\"d-label-strong\" x=\"510\" y=\"46\" text-anchor=\"middle\">function formatMoney</text>\n  <text class=\"d-code\" x=\"510\" y=\"82\" text-anchor=\"middle\">cents = 1220</text>\n  <text class=\"d-code\" x=\"510\" y=\"114\" text-anchor=\"middle\">dollars = \"12.20\"</text>\n  <text class=\"d-code\" x=\"510\" y=\"150\" text-anchor=\"middle\">return \"$12.20\"</text>\n  <path class=\"d-arrow\" d=\"M270 50 L376 70\" marker-end=\"url(#arrow-l-js-2-3-1)\"/>\n  <text class=\"d-label-muted\" x=\"322\" y=\"46\" text-anchor=\"middle\">argument</text>\n  <path class=\"d-arrow\" d=\"M376 150 L200 150 L160 84\" marker-end=\"url(#arrow-l-js-2-3-1)\"/>\n  <text class=\"d-label-muted\" x=\"270\" y=\"172\" text-anchor=\"middle\">return value</text>\n</svg><figcaption>A call sends arguments in and gets the return value back</figcaption></figure>\n<p>A function can have several parameters, separated by commas, and the arguments are matched by position: the first argument goes to the first parameter, and so on.</p>\n<p><code dir=\"ltr\">return</code> does two things: it decides the result, and it ends the function on the spot. Any line after a <code dir=\"ltr\">return</code> in the same block never runs. If a function finishes without reaching a <code dir=\"ltr\">return</code>, the call evaluates to <code dir=\"ltr\">undefined</code>.</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>Logging instead of returning</span></p>\n<div class=\"code-block\" dir=\"ltr\" data-lang=\"js\" data-index=\"1\"><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\">function</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\"> toCents</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#953800;--shiki-dark:#FFA657\">dollars</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\">(Math.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">round</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(dollars </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">*</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 100</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">)); </span><span style=\"color:#6E7781;--shiki-dark:#8B949E\">// shows the answer...</span></span>\n<span class=\"line\"><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\"> total</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\"> toCents</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">4.1</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">) </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">+</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\"> toCents</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">12.2</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">); </span><span style=\"color:#6E7781;--shiki-dark:#8B949E\">// ...but returns undefined: NaN</span></span></code></pre></div>\n<p><code dir=\"ltr\">console.log</code> only displays a value; it gives nothing back to the caller. A function that computes something should <code dir=\"ltr\">return</code> it, and the caller decides whether to print it.</p>\n</aside>\n<h2 id=\"sec-2\" tabindex=\"-1\">Default parameters</h2>\n<p>If you call a function with fewer arguments than parameters, the missing ones are <code dir=\"ltr\">undefined</code>. A <strong>default parameter</strong> gives them a sensible value instead:</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\">function</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\"> formatMoney</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#953800;--shiki-dark:#FFA657\">cents</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#953800;--shiki-dark:#FFA657\">symbol</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</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\">  return</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\"> `${</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">symbol</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">}${</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">(</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">cents</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> /</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 100</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">).</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">toFixed</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">(</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">2</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>\n<span class=\"line\"></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:#8250DF;--shiki-dark:#D2A8FF\">formatMoney</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>\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:#8250DF;--shiki-dark:#D2A8FF\">formatMoney</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:#0A3069;--shiki-dark:#A5D6FF\">\"€\"</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">));</span></span></code></pre><div class=\"run-output\" data-run-output=\"2\" hidden></div></div>\n<p>The default is used when the argument is missing or <code dir=\"ltr\">undefined</code>, and only then. Passing <code dir=\"ltr\">0</code>, <code dir=\"ltr\">&quot;&quot;</code> or <code dir=\"ltr\">null</code> counts as a real argument.</p>\n<h2 id=\"sec-3\" tabindex=\"-1\">Arrow functions</h2>\n<p>There is a second, shorter way to write functions, the <strong>arrow function</strong>. It is a value, so you store it in a <code dir=\"ltr\">const</code>:</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\">const</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\"> toCents</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> (</span><span style=\"color:#953800;--shiki-dark:#FFA657\">dollars</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">) </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=></span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> Math.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">round</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(dollars </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">*</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 100</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:#8250DF;--shiki-dark:#D2A8FF\"> isBig</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> (</span><span style=\"color:#953800;--shiki-dark:#FFA657\">cents</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">) </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=></span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> cents </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">>=</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 10000</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">;</span></span>\n<span class=\"line\"></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:#8250DF;--shiki-dark:#D2A8FF\">toCents</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">12.2</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">), </span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">isBig</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">toCents</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">120</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>When the body is a single expression, as here, you leave out the braces and the <code dir=\"ltr\">return</code>: the expression’s value is returned automatically. When you need several statements, use braces, and then <code dir=\"ltr\">return</code> is required again:</p>\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:#8250DF;--shiki-dark:#D2A8FF\"> budgetLeft</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> (</span><span style=\"color:#953800;--shiki-dark:#FFA657\">budget</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#953800;--shiki-dark:#FFA657\">spent</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">) </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:#CF222E;--shiki-dark:#FF7B72\">  const</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> left</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> budget </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">-</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> spent;</span></span>\n<span class=\"line\"><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">  return</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> left </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">></span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 0</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> ?</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> left </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:#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:#8250DF;--shiki-dark:#D2A8FF\">budgetLeft</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">2000</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">2200</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">));</span></span></code></pre><div class=\"run-output\" data-run-output=\"4\" hidden></div></div>\n<p>Which style should you use? Both are everywhere in real code. A common default, and the one this course follows: <strong>declarations for named, top-level functions, arrows for short functions passed to other functions</strong>. One practical difference: declarations are <strong>hoisted</strong>, which means you can call them on a line above where they are written. A <code dir=\"ltr\">const</code> arrow function only exists once its line has run.</p>\n<h2 id=\"sec-4\" tabindex=\"-1\">Functions are values</h2>\n<p>In JavaScript a function is a value like a number or a string. You can store it, and you can pass it to another function as an argument. That is the idea behind much of modern JavaScript:</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\">function</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\"> applyToAll</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#953800;--shiki-dark:#FFA657\">amounts</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">, </span><span style=\"color:#953800;--shiki-dark:#FFA657\">transform</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\"> results</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:#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\">    results.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">push</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">transform</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:#CF222E;--shiki-dark:#FF7B72\">  return</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> results;</span></span>\n<span class=\"line\"><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\">const</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\"> formatMoney</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> (</span><span style=\"color:#953800;--shiki-dark:#FFA657\">cents</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">) </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=></span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\"> `$${</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">(</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">cents</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> /</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 100</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">).</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">toFixed</span><span style=\"color:#0A3069;--shiki-dark:#A5D6FF\">(</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">2</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\">console.</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">log</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#8250DF;--shiki-dark:#D2A8FF\">applyToAll</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\">], formatMoney));</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:#8250DF;--shiki-dark:#D2A8FF\">applyToAll</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:#953800;--shiki-dark:#FFA657\">c</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">) </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">=></span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> c </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">*</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 2</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">));</span></span></code></pre><div class=\"run-output\" data-run-output=\"5\" hidden></div></div>\n<p><code dir=\"ltr\">applyToAll</code> does not know or care what <code dir=\"ltr\">transform</code> does; it calls it once per item and collects the results with <code dir=\"ltr\">push</code>, which adds an item to the end of an array. A function passed in like this is called a <strong>callback</strong>. Notice that you pass <code dir=\"ltr\">formatMoney</code> without parentheses: you are handing over the function, not calling it. Arrays have this exact helper built in, called <code dir=\"ltr\">map</code>, and section 3 puts it to work.</p>\n<h2 id=\"sec-5\" tabindex=\"-1\">Small, pure functions</h2>\n<p>Notice what every helper in this lesson has in common: it uses only its parameters, returns a result, and changes nothing outside itself. A function like that is called <strong>pure</strong>. Give it the same arguments and it returns the same answer every time, so you can test it with one line, reuse it anywhere, and trust it without reading its body again.</p>\n<p>Not every function can be pure. Something eventually has to print to the screen or save data. The habit that keeps programs manageable is to do the calculating in small pure functions and keep the printing and saving in a few clearly named places. Pocket follows that split all the way to the final app.</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>Name functions after what they return</span></p>\n<p><code dir=\"ltr\">formatMoney</code>, <code dir=\"ltr\">toCents</code>, <code dir=\"ltr\">isBig</code>, <code dir=\"ltr\">budgetLeft</code>: a verb for functions that do or compute something, <code dir=\"ltr\">is</code>/<code dir=\"ltr\">has</code> for ones that return a boolean. Good names let you read <code dir=\"ltr\">if (isBig(amount))</code> like a sentence.</p>\n</aside>\n<p>Functions create their own little world of names inside them. How that world relates to the code around it is called scope, and it is the subject of the next lesson.</p>\n","toc":[{"id":"sec-1","text":"Declaring and calling a function","level":2},{"id":"sec-2","text":"Default parameters","level":2},{"id":"sec-3","text":"Arrow functions","level":2},{"id":"sec-4","text":"Functions are values","level":2},{"id":"sec-5","text":"Small, pure functions","level":2}],"takeaways":["A function packages steps under a name; parameters are the inputs it declares, arguments are the values you pass when you call it.","<code dir=\"ltr\">return</code> ends the function and hands a value back; a function without <code dir=\"ltr\">return</code> gives back <code dir=\"ltr\">undefined</code>.","Arrow functions (<code dir=\"ltr\">(x) =&gt; x * 2</code>) are a shorter syntax; with curly braces they need an explicit <code dir=\"ltr\">return</code>.","Default parameters (<code dir=\"ltr\">symbol = \\&quot;$\\&quot;</code>) apply when an argument is missing or <code dir=\"ltr\">undefined</code>.","Functions are values, so you can store them in variables and pass them to other functions."],"further":[{"title":"Functions (MDN guide)","url":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions"},{"title":"Arrow function expressions (MDN)","url":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions"},{"title":"Default parameters (MDN)","url":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters"}],"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:#8250DF;--shiki-dark:#D2A8FF\"> double</span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\"> =</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\"> (</span><span style=\"color:#953800;--shiki-dark:#FFA657\">n</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">) </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\">  n </span><span style=\"color:#CF222E;--shiki-dark:#FF7B72\">*</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\"> 2</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:#8250DF;--shiki-dark:#D2A8FF\">double</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">(</span><span style=\"color:#0550AE;--shiki-dark:#79C0FF\">4</span><span style=\"color:#1F2328;--shiki-dark:#E6EDF3\">));</span></span></code></pre></div>\n","options":[{"text":"<code dir=\"ltr\">undefined</code>","why":"Correct. A block body without <code dir=\"ltr\">return</code> returns <code dir=\"ltr\">undefined</code>. Write <code dir=\"ltr\">(n) =&gt; n * 2</code> or add <code dir=\"ltr\">return n * 2;</code>."},{"text":"<code dir=\"ltr\">8</code>","why":"The body computes 8 and throws it away. With curly braces, an arrow function needs an explicit <code dir=\"ltr\">return</code>."},{"text":"<code dir=\"ltr\">n * 2</code>","why":"JavaScript evaluates expressions; it never returns the source text of one."}],"answer":0},{"q":"<p><code dir=\"ltr\">function formatMoney(cents, symbol = &quot;$&quot;) { … }</code> is called as <code dir=\"ltr\">formatMoney(450, undefined)</code>. What is <code dir=\"ltr\">symbol</code> inside?</p>","options":[{"text":"<code dir=\"ltr\">undefined</code>, because you passed it explicitly.","why":"Defaults apply to missing arguments and to arguments that are <code dir=\"ltr\">undefined</code>, whether passed or not."},{"text":"<code dir=\"ltr\">&quot;$&quot;</code>","why":"Correct. Passing <code dir=\"ltr\">undefined</code> is treated exactly like leaving the argument out, so the default kicks in."},{"text":"<code dir=\"ltr\">null</code>","why":"Nothing here produces <code dir=\"ltr\">null</code>. And passing <code dir=\"ltr\">null</code> would not trigger the default; only <code dir=\"ltr\">undefined</code> does."}],"answer":1},{"q":"<p>Which line calls <code dir=\"ltr\">isBig</code> from before its definition without an error?</p>","options":[{"text":"<code dir=\"ltr\">console.log(isBig(100)); const isBig = (c) =&gt; c &gt; 50;</code>","why":"<code dir=\"ltr\">const</code> names cannot be used before their line runs, so this throws a ReferenceError."},{"text":"<code dir=\"ltr\">console.log(isBig(100)); let isBig = function (c) { return c &gt; 50; };</code>","why":"The function value is stored in a <code dir=\"ltr\">let</code>, which is not available before its declaration line."},{"text":"<code dir=\"ltr\">console.log(isBig(100)); function isBig(c) { return c &gt; 50; }</code>","why":"Correct. Function declarations are hoisted, meaning the whole function is available from the top of its scope."}],"answer":2},{"q":"<p><code dir=\"ltr\">const amounts = [450, 1220];</code> and <code dir=\"ltr\">function toDollars(c) { return c / 100; }</code>. What does <code dir=\"ltr\">amounts.map(toDollars)</code> need from you?</p>","options":[{"text":"Nothing more. <code dir=\"ltr\">map</code> calls <code dir=\"ltr\">toDollars</code> for each item and collects the results.","why":"Correct. You pass the function itself, without parentheses, and <code dir=\"ltr\">map</code> decides when to call it."},{"text":"<code dir=\"ltr\">amounts.map(toDollars())</code> with parentheses, so the function runs.","why":"That calls <code dir=\"ltr\">toDollars</code> once immediately with no argument and passes its result, <code dir=\"ltr\">NaN</code>, to <code dir=\"ltr\">map</code>, which then throws."},{"text":"A loop around it, because <code dir=\"ltr\">map</code> only works on one value.","why":"<code dir=\"ltr\">map</code> walks the whole array itself. That is the point of passing it a function."}],"answer":0}],"exercise":{"kind":"web","mode":"js","starter":{"js":"// 1. Dollars (like 4.1) to whole cents (410)\nfunction toCents(dollars) {\n}\n\n// 2. Cents to a display string, with a symbol that defaults to \"$\"\nfunction formatMoney(cents) {\n}\n\n// 3. Replace null with an arrow function: true for 10000 cents or more\nconst isBig = null;\n\n// 4. Count the amounts for which test(amount) returns true\nfunction countWhere(amounts, test) {\n  return 0;\n}\n\nconsole.log(formatMoney(toCents(12.2)));\n"},"solution":{"js":"function toCents(dollars) {\n  return Math.round(dollars * 100);\n}\n\nfunction formatMoney(cents, symbol = \"$\") {\n  return `${symbol}${(cents / 100).toFixed(2)}`;\n}\n\nconst isBig = (cents) => cents >= 10000;\n\nfunction countWhere(amounts, test) {\n  let count = 0;\n  for (const amount of amounts) {\n    if (test(amount)) {\n      count++;\n    }\n  }\n  return count;\n}\n\nconsole.log(formatMoney(toCents(12.2)));\n"},"checks":[{"id":"to-cents","test":"return toCents(4.1) === 410 && toCents(12.2) === 1220 && toCents(0.07) === 7;\n"},{"id":"format","test":"return formatMoney(1220) === '$12.20' && formatMoney(5) === '$0.05';\n"},{"id":"format-symbol","test":"return formatMoney(1220, '€') === '€12.20' && formatMoney(450, undefined) === '$4.50';\n"},{"id":"is-big-arrow","test":"return typeof isBig === 'function' && !('prototype' in isBig) && isBig(10000) === true && isBig(9999) === false;\n"},{"id":"count-where","test":"const amounts = [450, 12000, 99, 10000];\nreturn countWhere(amounts, isBig) === 2 && countWhere(amounts, (c) => c < 100) === 1 && countWhere([], isBig) === 0;\n"}],"title":"Pocket's money helpers","prompt":"<p>Write four small functions Pocket will use from now on:</p>\n<ol>\n<li><code dir=\"ltr\">toCents(dollars)</code> returns the amount in whole cents (<code dir=\"ltr\">4.1</code> gives <code dir=\"ltr\">410</code>).</li>\n<li><code dir=\"ltr\">formatMoney(cents, symbol)</code> returns a string like <code dir=\"ltr\">&quot;$12.20&quot;</code>. <code dir=\"ltr\">symbol</code> defaults to <code dir=\"ltr\">&quot;$&quot;</code>.</li>\n<li><code dir=\"ltr\">isBig</code>, an <strong>arrow function</strong> that returns <code dir=\"ltr\">true</code> for 10000 cents or more.</li>\n<li><code dir=\"ltr\">countWhere(amounts, test)</code> returns how many items in <code dir=\"ltr\">amounts</code> make the callback <code dir=\"ltr\">test(amount)</code> return a truthy value.</li>\n</ol>\n","hints":["Remember <code dir=\"ltr\">Math.round</code>, because <code dir=\"ltr\">4.1 * 100</code> is not exactly 410. And remember <code dir=\"ltr\">return</code>: without it every call gives <code dir=\"ltr\">undefined</code>.","A default parameter is written in the parameter list: <code dir=\"ltr\">function formatMoney(cents, symbol = &quot;$&quot;)</code>.","<code dir=\"ltr\">const isBig = (cents) =&gt; cents &gt;= 10000;</code> In <code dir=\"ltr\">countWhere</code>, call the callback inside a loop: <code dir=\"ltr\">if (test(amount)) { count++; }</code>."],"explanation":"<p>Each helper returns its result instead of logging it, so callers can combine them: <code dir=\"ltr\">formatMoney(toCents(12.2))</code> feeds one function’s return value straight into the next. The default <code dir=\"ltr\">symbol = &quot;$&quot;</code> applies whenever the argument is missing or <code dir=\"ltr\">undefined</code>. <code dir=\"ltr\">isBig</code> uses the arrow function’s short form, where the single expression after <code dir=\"ltr\">=&gt;</code> is the return value. <code dir=\"ltr\">countWhere</code> never needs to know what the test checks; it calls whatever function it was given, once per amount. Passing <code dir=\"ltr\">isBig</code> without parentheses hands over the function itself, which is exactly how the array methods in section 3 work.</p>\n","checkLabels":{"to-cents":"<code dir=\"ltr\">toCents</code> returns whole cents","format":"<code dir=\"ltr\">formatMoney</code> returns strings like “$12.20”","format-symbol":"<code dir=\"ltr\">formatMoney</code> uses a custom symbol and defaults to “$”","is-big-arrow":"<code dir=\"ltr\">isBig</code> is an arrow function with the 10000-cent threshold","count-where":"<code dir=\"ltr\">countWhere</code> counts items that pass the callback"}},"words":775,"readMinutes":4}