<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: BeGoodTool.com</title>
    <description>The latest articles on DEV Community by BeGoodTool.com (begoodtool).</description>
    <link>https://dev.to/begoodtool</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Forganization%2Fprofile_image%2F14295%2Fd519278f-433a-4dab-9489-447ef51fc6d4.jpg</url>
      <title>DEV Community: BeGoodTool.com</title>
      <link>https://dev.to/begoodtool</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/begoodtool"/>
    <language>en</language>
    <item>
      <title>Why an Inches-to-Centimeters Input Needs More Than HTML Number Controls</title>
      <dc:creator>Joe Lin</dc:creator>
      <pubDate>Mon, 16 Nov 2026 13:00:03 +0000</pubDate>
      <link>https://dev.to/begoodtool/why-an-inches-to-centimeters-input-needs-more-than-html-number-controls-36b3</link>
      <guid>https://dev.to/begoodtool/why-an-inches-to-centimeters-input-needs-more-than-html-number-controls-36b3</guid>
      <description>&lt;p&gt;Putting &lt;code&gt;type="number"&lt;/code&gt; on a measurement field does not finish its validation. I ran into the distinction while reading the input flow for an inches-to-centimeters page: the browser's editing rules, Vue's model conversion, the arithmetic helper, and blur-time validation each handle a different part of the problem.&lt;/p&gt;

&lt;p&gt;The conversion factor is not the difficult part. One inch is 2.54 centimeters. The interesting question is what the application should do while a user is clearing a field, entering a negative value, or trying to paste a fractional measurement such as “5 1/2.”&lt;/p&gt;

&lt;h2&gt;
  
  
  HTML Describes the Control, Not the Whole Domain
&lt;/h2&gt;

&lt;p&gt;The fixed-pair component uses this input on its left side:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt;
  &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"number"&lt;/span&gt;
  &lt;span class="na"&gt;v-model.number&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"fromValue"&lt;/span&gt;
  &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text-input"&lt;/span&gt;
  &lt;span class="na"&gt;inputmode&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"decimal"&lt;/span&gt;
  &lt;span class="na"&gt;min&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"0"&lt;/span&gt;
  &lt;span class="na"&gt;step&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"any"&lt;/span&gt;
  &lt;span class="na"&gt;@blur&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"validateInput('from')"&lt;/span&gt;
&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each attribute has a purpose. The number type gives the browser a numeric control. &lt;code&gt;inputmode="decimal"&lt;/code&gt; is a keyboard hint for devices with a virtual keyboard. &lt;code&gt;step="any"&lt;/code&gt; permits fractional values rather than expressing an integer-only stepping rule.&lt;/p&gt;

&lt;p&gt;The minimum of zero describes a validity constraint. It does not mean every attempt to type a negative value is intercepted before JavaScript can observe it, and it does not automatically invoke an application-specific error message.&lt;/p&gt;

&lt;p&gt;Vue's &lt;code&gt;.number&lt;/code&gt; modifier helps convert model values to numbers, but editing still has transitional states. In particular, clearing a number field can leave an empty string in the model. Treating every model value as an already validated measurement would lose that distinction.&lt;/p&gt;

&lt;p&gt;I find it helpful to review numeric forms as a sequence: what can be edited, what reaches the model, what the calculation accepts, and when the UI reports an error. Combining those into a vague statement that “the field only accepts numbers” makes edge cases harder to reason about.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Shared Conversion Helper Has Its Own Coercion Policy
&lt;/h2&gt;

&lt;p&gt;This page fixes the source unit to inches and the destination to centimeters, but it uses the shared meter-based length conversion helper:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;convertLength&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fromKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;toKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;meters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lengthUnitMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;fromKey&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;lengthUnitMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;toKey&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;meters&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;lengthUnitMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;toKey&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The inch scale is 0.0254 meters and the centimeter scale is 0.01 meters. Converting 21.5 inches therefore produces 54.61 centimeters after the shared rounding step.&lt;/p&gt;

&lt;p&gt;The coercion deserves as much attention as the scales. &lt;code&gt;Number("")&lt;/code&gt; is zero, so clearing the source field makes the opposite field calculate a zero result. A nonnumeric string passed directly to this helper also falls back to zero through &lt;code&gt;|| 0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That is a real implementation choice, not a general validation technique. It keeps the result numeric, but collapses “no measurement supplied,” “invalid supplied value,” and “genuine zero” into the same calculation output.&lt;/p&gt;

&lt;p&gt;The rounding helper separately returns zero for nonfinite results. If you are building a stricter data-entry application, a numeric zero should not stand in for every failure mode. You would need an explicit validity state, rather than assuming this conversion helper already provides one.&lt;/p&gt;

&lt;p&gt;On this page, both fields remain editable. Synchronous watchers and a synchronization guard prevent a derived right-side value from immediately replacing the left-side input. That also allows the source control to remain visibly empty while its counterpart shows the helper's zero result.&lt;/p&gt;

&lt;h2&gt;
  
  
  Blur-Time Validation Is an Explicit Second Step
&lt;/h2&gt;

&lt;p&gt;The component adds its domain check when a field loses focus:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;validateInput&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;side&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;side&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"from"&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;fromValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;toValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isFinite&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;t&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`${categoryConfig.value.i18nKey}.invalidInput`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;side&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"from"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;fromValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;toValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a negative or nonfinite model value, this shows a translated error and resets the relevant field to zero. Because conversion watchers run as values change, a negative intermediate result can exist before blur performs that correction.&lt;/p&gt;

&lt;p&gt;That timing is important. The behavior is not “invalid input can never appear.” It is “this check runs when editing leaves the control.” If a future feature submits or exports values without first triggering blur, it should not assume the blur handler already ran.&lt;/p&gt;

&lt;p&gt;An empty field is also accepted by this particular check because numeric conversion turns an empty string into zero. That may be acceptable for a casual calculator, but it means the handler does not distinguish an intentionally entered zero from a missing measurement.&lt;/p&gt;

&lt;p&gt;The handler reads the model, not the control's &lt;code&gt;validity.badInput&lt;/code&gt; state. Browsers can expose an unusable number entry as an empty value, so the translated error is not a guarantee that every malformed edit or rejected paste will be reported. Native validity and application feedback are separate concerns here. If I needed an explicit “please enter a measurement” state, I would check for an empty value before numeric coercion and decide how to handle the control's native validity too; that is not what this implementation currently does.&lt;/p&gt;

&lt;p&gt;I would test the model transitions as well as the final result: enter a normal decimal, clear it, enter a negative number, and leave the field. For a two-way converter, repeat those actions on both sides. A happy-path test of 1 inch becoming 2.54 centimeters would not exercise the validation policy at all.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fractional Notation and Rounding Need Separate Decisions
&lt;/h2&gt;

&lt;p&gt;A decimal number input is not a parser for workshop or height notation. The page has no grammar for &lt;code&gt;1/2&lt;/code&gt;, &lt;code&gt;5 1/2&lt;/code&gt;, or feet-and-inches strings.&lt;/p&gt;

&lt;p&gt;If a measurement is five and a half inches, the supported representation is 5.5. Adding support for mixed fractions would require deciding how spaces, slashes, zero denominators, signs, and unit marks should behave. It is not something supplied by the existing conversion factor.&lt;/p&gt;

&lt;p&gt;There is a precision limit too. The shared rounding helper is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;roundLengthResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;isFinite&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;1e6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;1e6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For ordinary measurement magnitudes, this rounds the derived result to at most six decimal places. It is not just text formatting: the watchers assign that rounded number to the opposite field's model. Significant figures and manufacturing tolerances are not preserved.&lt;/p&gt;

&lt;p&gt;For example, entering 1 centimeter on the right produces 0.393701 inches on the left. Converting that rounded inch value back gives 1.000001 centimeters, not exactly 1. The synchronization guard prevents that reverse calculation from immediately overwriting the centimeter value the user just entered. I would keep unrounded calculation state separate from formatted output if repeated conversions needed to preserve more precision; this page does not do that.&lt;/p&gt;

&lt;p&gt;The finite-value check is not an unlimited range guarantee, either. A very large but finite input can overflow during conversion or during multiplication by a million in the rounding helper. The helper maps an already nonfinite input to zero, but it does not recheck the result of that multiplication. This is another reason not to reuse the calculation as a strict validation boundary.&lt;/p&gt;

&lt;p&gt;Finally, interpreting a product measurement remains the reader's job. A monitor's 21.5-inch size typically refers to its diagonal; converting that scalar to centimeters does not calculate width or height. Likewise, a converted suitcase dimension is not a guarantee of compliance with a particular airline's baggage rules.&lt;/p&gt;

&lt;p&gt;I turned this numeric-input flow into a small free tool: &lt;a href="https://begoodtool.com/length-converter/inches-to-cm/en" rel="noopener noreferrer"&gt;Inches to CM Converter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>vue</category>
      <category>forms</category>
    </item>
    <item>
      <title>Converting Centimeters to Inches Without Pretending Rounded Results Are Exact</title>
      <dc:creator>Joe Lin</dc:creator>
      <pubDate>Sun, 15 Nov 2026 13:00:03 +0000</pubDate>
      <link>https://dev.to/begoodtool/converting-centimeters-to-inches-without-pretending-rounded-results-are-exact-5d29</link>
      <guid>https://dev.to/begoodtool/converting-centimeters-to-inches-without-pretending-rounded-results-are-exact-5d29</guid>
      <description>&lt;p&gt;One inch is exactly 2.54 centimeters, but that does not make every centimeter-to-inch result an exact decimal. I wanted a small conversion page to make that distinction clear: an exact unit definition, a floating-point calculation, and a rounded display are three different things.&lt;/p&gt;

&lt;p&gt;This matters when you build a measurement UI. The usual multiplication or division is easy; deciding how many digits to show, what a reverse conversion means, and whether a tiny measurement should disappear is where the numerical contract actually takes shape.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use Exact Definitions Without Duplicating Pair Formulas
&lt;/h2&gt;

&lt;p&gt;The conversion page uses the same helper as the site's general length converter. Instead of storing a formula for each direction, the helper gives each supported unit a scale relative to meters.&lt;/p&gt;

&lt;p&gt;The relevant entries are &lt;code&gt;cm: 0.01&lt;/code&gt; and &lt;code&gt;inch: 0.0254&lt;/code&gt;. The actual conversion function is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;convertLength&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fromKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;toKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;meters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lengthUnitMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;fromKey&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;lengthUnitMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;toKey&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;meters&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;lengthUnitMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;toKey&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For centimeters to inches, it multiplies the input by 0.01 and divides by 0.0254. Algebraically, that is the familiar division by 2.54.&lt;/p&gt;

&lt;p&gt;The useful design decision is not making the arithmetic shorter. It is having one unit table serve both the general converter and the fixed-pair page. Correcting a scale or adding a supported unit should not require hunting through many slightly different copies of the same formula.&lt;/p&gt;

&lt;p&gt;The inch definition itself is exact. A rounded reciprocal such as 0.3937 inches per centimeter is an approximation, so repeatedly using that shortened multiplier introduces an avoidable source of error before display rounding even begins.&lt;/p&gt;

&lt;p&gt;The helper does not use a decimal-arithmetic library, however. Its constants and operations are JavaScript &lt;code&gt;Number&lt;/code&gt; values, so the exact definition is still evaluated through binary floating-point arithmetic.&lt;/p&gt;

&lt;h2&gt;
  
  
  An Exact Constant Does Not Mean an Exact Binary Fraction
&lt;/h2&gt;

&lt;p&gt;Most decimal fractions do not have a finite representation in binary. The same issue that makes &lt;code&gt;0.1 + 0.2&lt;/code&gt; surprising also applies to ordinary measurement constants.&lt;/p&gt;

&lt;p&gt;That does not make JavaScript unusable for a household measurement converter. It means the presentation should not confuse a long sequence of digits with certainty, or assume two algebraically equivalent expressions must produce bit-for-bit identical intermediate results.&lt;/p&gt;

&lt;p&gt;The shared helper rounds its output as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;roundLengthResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;isFinite&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;1e6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;1e6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For 100 centimeters, the displayed result is 39.370079 inches. For 2.54 centimeters, it is 1 inch. Those are sensible outputs for the page's six-decimal policy.&lt;/p&gt;

&lt;p&gt;Notice what the function returns: a number, not a string padded to six decimal places. A result of 1 is displayed as 1, not necessarily &lt;code&gt;1.000000&lt;/code&gt;. “At most six decimal places” is a different promise from “always show six places.”&lt;/p&gt;

&lt;p&gt;The limit also concerns decimal places, not significant figures. A value close to zero can lose a much larger proportion of its information than a large value. A single display policy is convenient, but it is not a complete scientific measurement policy.&lt;/p&gt;

&lt;p&gt;I would keep input precision, calculation precision, and display precision separate when reviewing a numeric interface. A precise-looking display cannot recover uncertainty in a ruler reading, and rounding output should not be described as increasing the accuracy of the original measurement.&lt;/p&gt;

&lt;h2&gt;
  
  
  Round Trips Can Lose Information
&lt;/h2&gt;

&lt;p&gt;The fixed-pair page lets the user edit either side. Its left-to-right synchronization writes a rounded output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="n"&gt;toValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;categoryConfig&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;categoryConfig&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;convert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fromValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pair&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pair&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The reverse direction uses the corresponding reverse conversion. A synchronization guard prevents the programmatically written output from immediately rewriting the original input.&lt;/p&gt;

&lt;p&gt;That protects the current editing direction, but it does not make rounding invertible. If the user later edits or copies the rounded inch result, that rounded number becomes the next input. Information discarded from the earlier result is no longer available.&lt;/p&gt;

&lt;p&gt;A particularly clear boundary is 0.000001 centimeters. Converted to inches, it is about 0.0000003937 before display rounding. The six-decimal helper returns zero. Converting that zero back produces zero centimeters, not the original tiny measurement.&lt;/p&gt;

&lt;p&gt;This is quantization, not evidence that the unit factor is wrong. Many different small inputs map to the same displayed number, so no reverse operation can identify which original value produced it.&lt;/p&gt;

&lt;p&gt;For tests, I would check representative values with an explicitly chosen tolerance rather than asserting exact equality for every arbitrary round trip. I would also include a tiny input that intentionally rounds to zero. That test documents the actual output contract instead of treating the loss as an unexpected accident.&lt;/p&gt;

&lt;p&gt;For ordinary nonnegative values, I can estimate that tolerance from the two rounding steps. Rounding inches to six decimal places contributes at most 0.0000005 inch, or 0.00000127 centimeters. Rounding the reverse result adds at most 0.0000005 centimeters, giving a combined absolute error budget of 0.00000177 centimeters, before allowing for floating-point error. That is a display-rounding budget, not a guarantee about the original measurement's accuracy or extreme numeric inputs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Unit Conversion Does Not Interpret the Measurement
&lt;/h2&gt;

&lt;p&gt;The page's English content discusses heights and screen sizes. The underlying function only converts a scalar length; it does not know what was measured.&lt;/p&gt;

&lt;p&gt;A screen measurement in inches generally describes the diagonal. Converting that diagonal to centimeters does not tell you the display's width or height without an aspect ratio. Similarly, a number of inches is not automatically a feet-and-inches height string.&lt;/p&gt;

&lt;p&gt;The rounding helper returns zero when its incoming, unrounded result is nonfinite. That check happens before multiplication by one million: a sufficiently large finite result can still overflow during rounding and return &lt;code&gt;Infinity&lt;/code&gt;. For example, converting &lt;code&gt;1e308&lt;/code&gt; centimeters produces a finite intermediate result, but rounding it overflows. The function does not recheck after scaling.&lt;/p&gt;

&lt;p&gt;Input handling is a separate boundary. Both fields use &lt;code&gt;type="number"&lt;/code&gt;, &lt;code&gt;v-model.number&lt;/code&gt;, and &lt;code&gt;min="0"&lt;/code&gt;. On blur, &lt;code&gt;validateInput&lt;/code&gt; checks whether the value converts to a finite, nonnegative number and resets an invalid field to zero. That validation does not run before every conversion. The conversion helper itself also coerces nonnumeric input to zero, so these fallbacks do not establish that every displayed zero represents a genuine zero-length measurement.&lt;/p&gt;

&lt;p&gt;For ordinary sizing comparisons, the implementation is useful precisely because its scope is small. For engineering tolerances, explicit error states, locale-aware parsing, or preservation of significant figures, I would define a stricter numeric model rather than silently extending the meaning of this one.&lt;/p&gt;

&lt;p&gt;I turned this conversion and its shared rounding policy into a small free tool: &lt;a href="https://begoodtool.com/length-converter/cm-to-inches/en" rel="noopener noreferrer"&gt;CM to Inches Converter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>vue</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Preventing Feedback Loops in a Two-Way Vue Unit Converter</title>
      <dc:creator>Joe Lin</dc:creator>
      <pubDate>Sat, 14 Nov 2026 13:00:04 +0000</pubDate>
      <link>https://dev.to/begoodtool/preventing-feedback-loops-in-a-two-way-vue-unit-converter-1gni</link>
      <guid>https://dev.to/begoodtool/preventing-feedback-loops-in-a-two-way-vue-unit-converter-1gni</guid>
      <description>&lt;p&gt;If both fields in a Vue unit converter are editable, how do you stop a calculated result from overwriting the user's input? I use explicit conversion directions and synchronous watchers so that each numeric edit updates the opposite field without bouncing back.&lt;/p&gt;

&lt;p&gt;A Vue length converter gave me a compact example. Each side can update the other, both results are rounded, and a swap button changes units and values together. The arithmetic is routine; the interesting part is stopping intermediate state from becoming a new user instruction.&lt;/p&gt;

&lt;h2&gt;
  
  
  One Conversion Function, Two Directions
&lt;/h2&gt;

&lt;p&gt;The component imports a shared length-unit table and conversion helper. Each unit has a scale relative to meters, so a conversion goes from the source unit to meters and then from meters to the destination unit.&lt;/p&gt;

&lt;p&gt;That keeps the component out of the business of maintaining a separate formula for every possible pair. With eight units, separate directional formulas would create many opportunities for a mismatched constant. A shared base-unit representation needs one scale per unit instead.&lt;/p&gt;

&lt;p&gt;The component aliases the shared conversion and rounding helpers, then uses them in two mirror-image synchronization functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;syncFromLeft&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isSyncing&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;isSyncing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;toValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;roundResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;convert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fromValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fromUnit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;toUnit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;isSyncing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;syncFromRight&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isSyncing&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;isSyncing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;fromValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;roundResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;convert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;toValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;toUnit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fromUnit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;isSyncing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The direction is explicit. Editing the left side means the right side is derived; editing the right side means the left side is derived. The implementation does not average the fields or try to reconcile both as independent measurements.&lt;/p&gt;

&lt;p&gt;There is an important boundary to that rule: changing either unit dropdown always recalculates from the left. The component does not store a "last edited side." After entering 1 inch on the right, the left reads 2.54 centimeters. Changing the right unit to feet keeps that left value and produces 0.083333 feet; it does not reinterpret the right-hand 1 as one foot.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;isSyncing&lt;/code&gt; variable is not rendered state. It is an imperative guard around a short synchronous operation. Making it a ref would not help explain the calculation, because the template has no reason to react to it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Guard Depends on Watcher Timing
&lt;/h2&gt;

&lt;p&gt;Here are the value watchers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;watch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fromValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;syncFromLeft&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;flush&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"sync"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;watch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;toValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;syncFromRight&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;flush&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"sync"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The flush mode is part of the design, not an incidental performance setting.&lt;/p&gt;

&lt;p&gt;When &lt;code&gt;syncFromLeft&lt;/code&gt; writes &lt;code&gt;toValue&lt;/code&gt;, the synchronous watcher for the right side runs while &lt;code&gt;isSyncing&lt;/code&gt; is still true. Its guard returns immediately. The original left-side value remains the authoritative input.&lt;/p&gt;

&lt;p&gt;Without that coordination, a rounded result could be converted back and overwrite the user's original value. Vue may stop some repeated assignments when the primitive value is unchanged, so this is not a claim that every naive converter must loop forever. The more subtle problem is that derived output becomes a new input, potentially losing precision.&lt;/p&gt;

&lt;p&gt;For a concrete trace, entering 1 centimeter produces 0.393701 inches after rounding. Converting that displayed result back produces 1.000001 centimeters. The right-side watcher must return before performing that second conversion; otherwise the original 1 is no longer preserved. This is worth testing even when the form appears to settle without an infinite loop.&lt;/p&gt;

&lt;p&gt;Using default queued watchers with the same short-lived boolean would change the reasoning. A callback that runs after the original function releases the guard no longer sees the protected state. If you change watcher scheduling, you need to reconsider the guard rather than assuming it still protects the same operation.&lt;/p&gt;

&lt;p&gt;Synchronous watchers are not batched, so this is a deliberate trade-off for two small calculations, not a blanket recommendation for large reactive collections.&lt;/p&gt;

&lt;p&gt;The converter is synchronous, with no request or awaited task inside either helper. I would not copy this pattern unchanged into an asynchronous address lookup or pricing form. A boolean released before an asynchronous response arrives does not solve response ordering or stale updates.&lt;/p&gt;

&lt;h2&gt;
  
  
  Swap Units and Values as One State Change
&lt;/h2&gt;

&lt;p&gt;The swap button has a related problem: assignments happen one at a time, but the operation conceptually changes four values together.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;swapUnits&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;isSyncing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;tmpUnit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fromUnit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;tmpVal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fromValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;fromUnit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;toUnit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;toUnit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tmpUnit&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;fromValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;toValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;toValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tmpVal&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;isSyncing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;persistUnits&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without the guard, changing the first unit could immediately trigger a calculation using one new unit and one old unit. A later assignment might then swap a value that has already been recalculated from that temporary combination.&lt;/p&gt;

&lt;p&gt;The guarded version snapshots the original left unit and value, exchanges the pairs, and only then releases synchronization. It preserves the currently displayed relationship instead of inventing an intermediate conversion.&lt;/p&gt;

&lt;p&gt;This is not a general Vue transaction: the assignments still happen separately. The boolean only tells these synchronization functions to ignore intermediate states. Another synchronous watcher would need its own coordination if it depended on the complete pair.&lt;/p&gt;

&lt;p&gt;The unit watchers also use synchronous flushing. They call the left-to-right synchronization function and persist unit choices only when the guard is not active. The swap function explicitly persists once after completing the exchange.&lt;/p&gt;

&lt;p&gt;That persistence stores the selected unit keys, not the entered measurement. On mount, saved units are loaded if their keys exist in the unit map. The initial values remain deterministic for server rendering, and an initial synchronization already runs during setup so the first rendered result is not just a placeholder zero.&lt;/p&gt;

&lt;p&gt;I like this separation: rendering a valid default conversion, restoring preferences, and processing a user edit are distinct operations. They should not depend on a single late initialization step.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rounding and Validation Still Need Honest Boundaries
&lt;/h2&gt;

&lt;p&gt;The shared helper rounds ordinary finite results to six decimal places, not six significant figures:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;roundLengthResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;isFinite&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;1e6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;1e6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Returning a number avoids padding the display with trailing zeros. It does not preserve arbitrary measurement precision or guarantee a reversible round trip. For example, 0.1 millimeters is 0.0000001 kilometers, which rounds to zero here. A fixed number of decimal places has very different consequences depending on the destination unit.&lt;/p&gt;

&lt;p&gt;Swapping the displayed pair preserves the displayed values. It cannot recover digits already removed when a previous conversion was rounded. Likewise, entering a rounded result on the opposite side makes that rounded number the new source of truth.&lt;/p&gt;

&lt;p&gt;The shared conversion function also coerces values with &lt;code&gt;Number(value) || 0&lt;/code&gt;. Empty or nonnumeric input can therefore produce zero rather than a distinct invalid result. Clearing the left input leaves that source field empty while calculating zero on the right; the guard prevents the derived zero from being written back into the empty field. The numeric inputs have no &lt;code&gt;min&lt;/code&gt; restriction, and the helper accepts negative numbers. I would not advertise these fields as a complete domain-validation layer. If an application requires a positive physical length or a separate invalid state, those would be additional rules, not behavior this converter already provides.&lt;/p&gt;

&lt;p&gt;Useful tests include editing each side, changing either unit, swapping after editing the right side, and swapping twice. The important assertions concern both numbers &lt;strong&gt;and&lt;/strong&gt; units. Checking only that a familiar conversion appears once would miss the state-transition bugs this design is meant to prevent.&lt;/p&gt;

&lt;p&gt;I turned this two-way synchronization example into a small free tool: &lt;a href="https://begoodtool.com/length-converter/en" rel="noopener noreferrer"&gt;Length Unit Converter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>vue</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Parsing Ride-History CSVs Before Drawing Spending Charts</title>
      <dc:creator>Joe Lin</dc:creator>
      <pubDate>Fri, 13 Nov 2026 13:00:04 +0000</pubDate>
      <link>https://dev.to/begoodtool/parsing-ride-history-csvs-before-drawing-spending-charts-d46</link>
      <guid>https://dev.to/begoodtool/parsing-ride-history-csvs-before-drawing-spending-charts-d46</guid>
      <description>&lt;p&gt;A spending chart can look perfectly reasonable while summing the wrong CSV column. I'll walk through the browser-side import pipeline I use to separate correctly parsed fields from values that are actually safe to summarize.&lt;/p&gt;

&lt;p&gt;The implementation accepts a ride export, identifies date and amount columns, builds normalized records, and derives summaries and charts from those records. Three details are worth separating from the visualization layer: quoted CSV fields, exact-first header matching, and currencies that must never be treated as interchangeable numbers.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Comma Is Not Always a Field Separator
&lt;/h2&gt;

&lt;p&gt;Splitting every line on commas works until an address contains a comma. Splitting the whole file into lines first also breaks when a quoted field contains a newline.&lt;/p&gt;

&lt;p&gt;The parser in this implementation walks the text character by character. It tracks the current field, the current row, and whether it is inside a quoted field. This is the quoted-field branch:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inQuotes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;char&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'"'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'"'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="s1"&gt;'"'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;inQuotes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;char&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two consecutive quotes inside a quoted field become one literal quote. A single closing quote exits quoted mode. Everything else, including a comma or newline, is appended to the field rather than interpreted as a boundary.&lt;/p&gt;

&lt;p&gt;Outside quoted mode, commas finish fields and newlines finish rows. The loop skips carriage returns outside quotes, supporting the usual CRLF row endings. It also retains the final row when the file does not end with a newline.&lt;/p&gt;

&lt;p&gt;I find the state-machine explanation more useful than describing CSV as “comma-separated text.” The separator only has meaning in the right state. That same observation turns up in log parsers, command-line tokenizers, and almost any format with escaping.&lt;/p&gt;

&lt;p&gt;This parser is intentionally small, though. It does not expose a detailed malformed-CSV diagnostic, and an unmatched quote is not reported as a specific syntax error. Supporting ordinary exports is not the same as implementing a strict validator for every possible CSV dialect.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try Exact Header Matches Before Broad Keywords
&lt;/h2&gt;

&lt;p&gt;Real exports use inconsistent naming. The implementation normalizes headers by trimming whitespace, lowercasing, replacing underscores and hyphens with spaces, and collapsing repeated spaces.&lt;/p&gt;

&lt;p&gt;After normalization, the column search makes two passes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;findColumnIndex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;candidates&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;normalized&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;normalizeHeader&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;candidate&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="n"&gt;candidates&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;idx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;normalized&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;candidate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;idx&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;candidate&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="n"&gt;candidates&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;idx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;normalized&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findIndex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;candidate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;idx&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The amount candidates put &lt;code&gt;original fare local&lt;/code&gt; before broader names such as &lt;code&gt;fare&lt;/code&gt;, &lt;code&gt;amount&lt;/code&gt;, and &lt;code&gt;total&lt;/code&gt;. That matters for an export containing both &lt;code&gt;original_fare_local&lt;/code&gt; and a field such as &lt;code&gt;is_fare_split&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A substring-only search for “fare” could select a flag column just because it appears earlier in the file. Exact matching across the candidate list first gives a known real amount column priority over that weak match.&lt;/p&gt;

&lt;p&gt;The fallback still helps with unfamiliar exports, but it remains a heuristic. A header containing a generic word such as “time” is not proof that it represents the trip timestamp you intended.&lt;/p&gt;

&lt;p&gt;Source detection is separate from column selection. The UI can show an inferred provider and let the user override its badge, but that choice does not rewrite the date and amount candidate lists or reparse the file. I would not describe that control as a provider-specific parsing mode.&lt;/p&gt;

&lt;h2&gt;
  
  
  Normalize Records Before Summarizing Them
&lt;/h2&gt;

&lt;p&gt;Each accepted record stores a date, an amount, a currency, and derived weekday and hour values. Rows producing an invalid &lt;code&gt;Date&lt;/code&gt; or &lt;code&gt;NaN&lt;/code&gt; amount are skipped. When a status column is available, a case-insensitive substring check skips statuses containing the English fragments &lt;code&gt;cancel&lt;/code&gt;, &lt;code&gt;reject&lt;/code&gt;, or &lt;code&gt;refund&lt;/code&gt;. This is not a whitelist of completed trips, and it does not recognize equivalent statuses in every language.&lt;/p&gt;

&lt;p&gt;That means the result is an analysis of the retained ride records, not automatically a reconciliation of every transaction in an account statement. Excluding refunds may be useful for examining completed trips, but it changes the accounting question.&lt;/p&gt;

&lt;p&gt;Currency handling is another boundary. The import collects distinct nonempty currency codes and enables a currency selector when more than one exists. The filtered-records computation contains this check:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;multiCurrency&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;selectedCurrency&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;currency&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="n"&gt;selectedCurrency&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is filtering, not foreign-exchange conversion. One hundred USD and one hundred TWD should not become an unexplained total of two hundred. For correctly labeled records, selecting one currency gives the sums and average fare a consistent unit.&lt;/p&gt;

&lt;p&gt;Missing labels are a hole in that guarantee. Empty currency fields do not count toward the distinct-code total. With only USD and unlabeled records, the multi-currency check stays off and both are summed; with USD, TWD, and unlabeled records, selecting either code excludes the unlabeled rows. I would verify those missing labels before trusting a total, rather than assume the importer inferred their currency.&lt;/p&gt;

&lt;p&gt;Monthly aggregation then operates on those filtered records. It groups by a year-and-month key, accumulates spend and ride count, sorts the keys, and divides each month's spend by its ride count.&lt;/p&gt;

&lt;p&gt;There is a useful limitation in the implementation: it does not create missing months between observed months. A month with no retained rides is absent, rather than represented as an explicit zero. A line chart connecting surrounding months should not be mistaken for a complete monthly time series.&lt;/p&gt;

&lt;h2&gt;
  
  
  Parsing Successfully Does Not Prove the Numbers Are Right
&lt;/h2&gt;

&lt;p&gt;The amount parser strips characters other than digits, decimal points, and minus signs, then calls &lt;code&gt;parseFloat&lt;/code&gt;. It also recognizes parentheses as a negative amount.&lt;/p&gt;

&lt;p&gt;That suits inputs such as &lt;code&gt;1,234.56&lt;/code&gt;, but it is not locale-aware. A value written as &lt;code&gt;1.234,56&lt;/code&gt; becomes &lt;code&gt;1.23456&lt;/code&gt;, not 1,234.56. The CSV parser can correctly preserve the field while the amount parser still assigns the wrong numeric meaning.&lt;/p&gt;

&lt;p&gt;Dates have a similar boundary: the implementation uses the built-in &lt;code&gt;Date&lt;/code&gt; constructor and browser-local weekday and hour accessors. A valid timestamp is not proof of a valid calendar entry: &lt;code&gt;2026-02-30T12:00:00&lt;/code&gt; rolls into March instead of being rejected. Ambiguous date strings and timestamps with offsets need careful checking before interpreting a late-night spending pattern; month grouping also follows the browser's local timezone, not necessarily the trip's original timezone.&lt;/p&gt;

&lt;p&gt;File contents are read with &lt;code&gt;FileReader&lt;/code&gt; and processed in this client-side flow; they are not sent to a ride-analysis backend by this code. That is narrower than claiming the entire website is offline or makes no unrelated network requests.&lt;/p&gt;

&lt;p&gt;For testing, I would begin with this tiny fixture:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;trip_date,is_fare_split,original_fare_local,currency,status,address
2026-01-15T12:00:00,false,"1,234.56",USD,completed,"Main St, Apt ""B"""
2026-03-15T12:00:00,false,100,TWD,completed,Station
not-a-date,false,25,USD,completed,Station
2026-01-20T12:00:00,false,10,USD,refunded,Station
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The expected result is two retained rides: USD 1,234.56 and TWD 100, viewed separately, with the invalid date and refunded row excluded. The first address should become &lt;code&gt;Main St, Apt "B"&lt;/code&gt; without shifting the amount column. February should not appear as a generated zero month. These checks exercise field boundaries, header priority, row exclusion, and aggregation together. I would add separate cases for quoted newlines, missing currency labels, and locale-specific amounts; the fixture above does not cover those. Expected totals are more valuable than a visually polished chart built from an unverified import.&lt;/p&gt;

&lt;p&gt;I turned that import-and-summary pipeline into a small free tool: &lt;a href="https://begoodtool.com/ride-history-visualizer/en" rel="noopener noreferrer"&gt;Ride History Visualizer&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>vue</category>
      <category>data</category>
    </item>
    <item>
      <title>Finding the Middle Date Without Confusing Calendar Days and Timestamps</title>
      <dc:creator>Joe Lin</dc:creator>
      <pubDate>Thu, 12 Nov 2026 13:00:04 +0000</pubDate>
      <link>https://dev.to/begoodtool/finding-the-middle-date-without-confusing-calendar-days-and-timestamps-35fi</link>
      <guid>https://dev.to/begoodtool/finding-the-middle-date-without-confusing-calendar-days-and-timestamps-35fi</guid>
      <description>&lt;p&gt;If two dates are three days apart, no calendar date is equally distant from both. I'll show how I handle that gap in JavaScript with an explicit earlier-date rule, without confusing calendar-day counts with elapsed timestamps.&lt;/p&gt;

&lt;p&gt;I built a small date-only calculator around that question. The useful implementation detail is not averaging two timestamps: it is making the meaning of “middle” explicit, normalizing the inputs, and keeping distance labels attached to the dates the user actually entered.&lt;/p&gt;

&lt;h2&gt;
  
  
  Normalize to Calendar Days Before Comparing
&lt;/h2&gt;

&lt;p&gt;The inputs come from date pickers rather than timestamp fields. The calculation first checks that both dates exist, then converts them to Day.js values at the start of their respective days:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;d1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dayjs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;date1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"day"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;d2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dayjs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;date2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"day"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;earlier&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;d1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isBefore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;d1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;d2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;later&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;d1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isBefore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;d2&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;d1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;totalDays&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;later&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;diff&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;earlier&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"day"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That normalization prevents a hidden time-of-day value from influencing a task presented as date-only. It is especially relevant to a “Today” button: the current instant has an hour, minute, and second, but those are not part of the question the UI asks.&lt;/p&gt;

&lt;p&gt;Sorting the normalized dates gives the main calculation one consistent direction. The user can enter the later date first without creating a negative interval or needing a second midpoint algorithm.&lt;/p&gt;

&lt;p&gt;I also want to distinguish calendar operations from dividing elapsed milliseconds by 86,400,000. A local calendar day does not always contain 24 elapsed hours around daylight-saving changes. This implementation uses Day.js day differences and calendar-day addition; it does not claim to find the instant exactly halfway through an arbitrary elapsed-time interval.&lt;/p&gt;

&lt;p&gt;For example, March 7 to March 10, 2026 in America/New_York spans three calendar days but only 71 elapsed hours between local midnights. The code selects March 8 by counting whole days, not by halving those 71 hours.&lt;/p&gt;

&lt;p&gt;That distinction should be decided before choosing an API. A project planning checkpoint and a midpoint between two precisely timed events may sound similar in conversation, but they need different definitions and potentially different input fields.&lt;/p&gt;

&lt;h2&gt;
  
  
  Odd Intervals Need a Visible Tie Policy
&lt;/h2&gt;

&lt;p&gt;For an even interval, selecting the middle day is straightforward. January 1 to January 5 spans four days, so adding two days to the earlier date selects January 3.&lt;/p&gt;

&lt;p&gt;For an odd interval, the implementation chooses the earlier of the two neighboring calendar dates:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;isOdd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;totalDays&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;halfDays&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;totalDays&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;midpointDate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;earlier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;halfDays&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"day"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;January 1 to January 4 spans three days. Flooring half the interval gives one day, so the selected result is January 2. It is one day from the earlier endpoint and two days from the later one.&lt;/p&gt;

&lt;p&gt;The important thing is not that flooring is the only acceptable policy. Choosing January 3 could also be reasonable for a scheduling product that prefers the later checkpoint. What would be misleading is returning January 2 with no explanation and implying that both distances are equal.&lt;/p&gt;

&lt;p&gt;The component stores &lt;code&gt;isOdd&lt;/code&gt; in the result and displays a notice when that flag is true. That gives the presentation enough information to explain the asymmetric distances instead of hiding the decision in a formatting function.&lt;/p&gt;

&lt;p&gt;There is a wording bug worth separating from the algorithm: the current English warning says the exact midpoint falls at midnight. Between January 1 at 00:00 and January 4 at 00:00 in a fixed-offset zone, the elapsed-time midpoint is January 2 at noon. The code never calculates that instant; it selects January 2 as a calendar date. I would change the warning to describe the earlier-date policy rather than promise a particular clock time.&lt;/p&gt;

&lt;p&gt;There is a useful boundary case here: two consecutive dates span one day, so flooring gives zero. The selected midpoint is then the earlier endpoint itself. That can initially look like a calculation failure, but it follows directly from the stated rule. If the product requires a strictly interior date, no valid result exists for that interval.&lt;/p&gt;

&lt;p&gt;This is also a calendar-day difference, not an inclusive count of labeled dates. January 1 through January 4 contains four date labels but spans three day boundaries. Mixing those conventions is an easy way to introduce an off-by-one error.&lt;/p&gt;

&lt;h2&gt;
  
  
  Preserve the Original Input Labels
&lt;/h2&gt;

&lt;p&gt;Sorting dates simplifies the arithmetic, but it should not silently relabel the user's inputs. The result still needs to say how far the selected day is from “Date 1” and “Date 2,” not merely from “earlier” and “later.”&lt;/p&gt;

&lt;p&gt;The implementation explicitly maps the distances back:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;daysFromEarlier&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;midpointDate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;diff&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;earlier&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"day"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;daysFromLater&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;later&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;diff&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;midpointDate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"day"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;dFromDate1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;d1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isBefore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;daysFromEarlier&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;daysFromLater&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;dFromDate2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;d1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isBefore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;daysFromLater&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;daysFromEarlier&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If January 4 is entered first and January 1 second, the chosen midpoint remains January 2. The first input is now two days away and the second is one day away.&lt;/p&gt;

&lt;p&gt;That is a small example of a broader UI pattern: normalize data for computation without losing the association between a value and its original control. The computational order and the presentation order are not always the same thing.&lt;/p&gt;

&lt;p&gt;The component also clears the result when an input changes, when dates are swapped, or when “Today” is selected. It requires another calculation rather than leaving an old midpoint visible beside new endpoints. A correct algorithm attached to a stale result is still a confusing experience.&lt;/p&gt;

&lt;p&gt;Equal endpoints have a dedicated branch: the selected date is the endpoint, both distances are zero, and an informational message explains the case. No fractional midpoint logic is needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Formatting Does Not Add Time-Zone Semantics
&lt;/h2&gt;

&lt;p&gt;Results are formatted with &lt;code&gt;Intl.DateTimeFormat&lt;/code&gt;. The application maps its internal language codes to BCP 47 locale identifiers; English uses the browser language when available. That changes how a date is written, not what kind of interval was calculated.&lt;/p&gt;

&lt;p&gt;There is no input for two different time zones, no business-day exclusion, and no holiday calendar. A date picker does not make this a meeting-time converter or a working-day scheduler. Those features would require additional rules rather than another localized label.&lt;/p&gt;

&lt;p&gt;For testing, I would keep same-day, consecutive-day, even-gap, odd-gap, reversed-input, leap-day, and local daylight-saving cases separate. Each checks a different promise: zero handling, rounding, stable labels, or calendar arithmetic. One ordinary mid-month example cannot stand in for all of them.&lt;/p&gt;

&lt;p&gt;I turned these date-only rules into a small free tool: &lt;a href="https://begoodtool.com/midpoint-date-calculator/en" rel="noopener noreferrer"&gt;Midpoint Date Calculator&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>vue</category>
      <category>webdev</category>
    </item>
    <item>
      <title>What a Number-Guessing Game Teaches About Binary Search and Vue State</title>
      <dc:creator>Joe Lin</dc:creator>
      <pubDate>Wed, 11 Nov 2026 13:00:04 +0000</pubDate>
      <link>https://dev.to/begoodtool/what-a-number-guessing-game-teaches-about-binary-search-and-vue-state-445a</link>
      <guid>https://dev.to/begoodtool/what-a-number-guessing-game-teaches-about-binary-search-and-vue-state-445a</guid>
      <description>&lt;p&gt;A higher-or-lower number game can give misleading hints or attempt counts if the secret and round state change at the wrong time. I'll use a small Vue implementation to show how to keep those updates consistent, then explain why midpoint guesses put a worst-case bound on the number of attempts.&lt;/p&gt;

&lt;p&gt;The player chooses a range, submits a number, and gets a high, low, or correct result. There is no computer player performing binary search. The useful connection is that the feedback gives a human player exactly the information needed to shrink a search interval.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pick One Secret for the Whole Round
&lt;/h2&gt;

&lt;p&gt;The implementation offers upper bounds of 10, 50, 100, and 1,000, always starting at 1. The target generator is deliberately ordinary:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;randomTarget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Multiplying a random value in the interval from zero up to, but not including, one produces a value below &lt;code&gt;max&lt;/code&gt;. Taking the floor gives integers from zero through &lt;code&gt;max - 1&lt;/code&gt;; adding one moves the range to the numbers the UI promises.&lt;/p&gt;

&lt;p&gt;The target must be generated once per round, not once per submission. Otherwise a helpful-looking “too low” message would describe a secret that might no longer exist when the next guess arrives. That destroys the premise of interval narrowing.&lt;/p&gt;

&lt;p&gt;The round reset is also a state transition, not just another random assignment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;newGame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"undefined"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;randomTarget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;maxNum&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;currentGuess&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;guesses&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;won&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;feedback&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;feedbackType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I like reading this function as a checklist of everything belonging to one round. The secret changes, input clears, history disappears, the win lock resets, and the previous hint goes away together.&lt;/p&gt;

&lt;p&gt;Changing the selected range calls this same reset. Keeping two partially different reset implementations would make it easy to carry an old guess history into a new range or leave the submission button disabled after a previous win.&lt;/p&gt;

&lt;h2&gt;
  
  
  Record the Attempt Before Announcing the Win
&lt;/h2&gt;

&lt;p&gt;The submission function first checks whether the round is already won, rejects an empty or nonnumeric input, and reports numbers outside the selected bounds. Only then does it compare the guess with the secret.&lt;/p&gt;

&lt;p&gt;In the successful branch, the guess is appended before the attempt count is used:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;guesses&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dir&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"correct"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;won&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;feedback&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;t&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"guessNumber.correct"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;guesses&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;feedbackType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"correct"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bestScore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;guesses&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;bestScore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;bestScore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;guesses&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The history is held in a Vue ref, so both the comparison and the stored score read &lt;code&gt;guesses.value.length&lt;/code&gt;. More importantly, the successful submission is included before either read. Counting first and appending afterward would underreport every win by one attempt.&lt;/p&gt;

&lt;p&gt;Each history item stores both the submitted value and its direction. The template derives its arrow and appearance from that record. I would rather keep that compact structured history than try to reconstruct previous results from translated feedback strings.&lt;/p&gt;

&lt;p&gt;The best score is a separate ref, so starting a new round does not erase it. It is not stored in localStorage, and it is not partitioned by range. A one-guess win in the smallest range can therefore remain the best score when the player switches to 1–1,000. Its lifetime is that of the component instance: refreshing the page or unmounting and recreating the component loses the record. It is a convenience, not a normalized measure of skill.&lt;/p&gt;

&lt;h2&gt;
  
  
  Higher-or-Lower Feedback Defines a Search Interval
&lt;/h2&gt;

&lt;p&gt;Suppose the secret is somewhere from 1 through 100. Guessing 50 and hearing “too low” eliminates everything from 1 through 50, including the number just tried. The remaining candidates are 51 through 100.&lt;/p&gt;

&lt;p&gt;Guessing near the middle of the remaining interval keeps the larger of the two possible remaining groups small. Repeating that strategy is binary search performed by the player. It is the same idea used to search a sorted collection, except the game supplies a comparison instead of exposing an array.&lt;/p&gt;

&lt;p&gt;For an inclusive interval &lt;code&gt;[low, high]&lt;/code&gt;, I would choose &lt;code&gt;Math.floor((low + high) / 2)&lt;/code&gt;. After a “too low” result for &lt;code&gt;g&lt;/code&gt;, the next lower bound is &lt;code&gt;g + 1&lt;/code&gt;; after “too high,” the next upper bound is &lt;code&gt;g - 1&lt;/code&gt;. Excluding the failed guess matters: keeping it as a candidate can make the search repeat a value instead of progressing, especially when only two candidates remain.&lt;/p&gt;

&lt;p&gt;For a range containing 100 distinct targets, seven guesses suffice in the worst case with an appropriate midpoint strategy. More generally, a comparison tree with at most &lt;code&gt;k&lt;/code&gt; guesses can accommodate up to &lt;code&gt;2^k - 1&lt;/code&gt; target values: each comparison can finish at its own node or continue into one of two smaller intervals.&lt;/p&gt;

&lt;p&gt;The smallest sufficient bound for &lt;code&gt;N&lt;/code&gt; targets is therefore &lt;code&gt;Math.ceil(Math.log2(N + 1))&lt;/code&gt;. Six guesses cover at most 63 targets; seven cover 127. That explains the seven-guess guarantee for 100 targets without assuming that every guess has only two outcomes: equality ends the search at the current node.&lt;/p&gt;

&lt;p&gt;That bound does not mean every game takes seven attempts. The secret might be the first midpoint, and random guesses can occasionally get lucky. Nor does it mean the component calculates a recommended next guess; it only provides feedback and history.&lt;/p&gt;

&lt;p&gt;The component also keeps the input bounds at 1 and the selected maximum, not the narrowed interval. Repeating an earlier guess, or guessing something earlier feedback already ruled out, still counts as an attempt. The guarantee depends on the player's strategy; the interface does not enforce it.&lt;/p&gt;

&lt;p&gt;This distinction makes the game a useful teaching example. The state machine supplies trustworthy observations, while the search strategy is a separate layer of reasoning. You can explain interval halving without pretending the application contains a solver that is absent from its source.&lt;/p&gt;

&lt;h2&gt;
  
  
  Browser Games Still Have Initialization and Input Boundaries
&lt;/h2&gt;

&lt;p&gt;The component begins with a deterministic target of 50 and calls &lt;code&gt;newGame()&lt;/code&gt; from &lt;code&gt;onMounted&lt;/code&gt;. Randomness is deferred until the browser has mounted the page, rather than producing different secrets during server rendering and the browser's first render.&lt;/p&gt;

&lt;p&gt;For a casual guessing game, &lt;code&gt;Math.random()&lt;/code&gt; is adequate. It is not a cryptographic secret generator, and a target stored in client-side state is not protected from someone inspecting the application. This is not an architecture for competitive play or prize distribution.&lt;/p&gt;

&lt;p&gt;There is also a narrower validation gap: the submission logic checks numeric conversion and bounds but does not check &lt;code&gt;Number.isInteger(g)&lt;/code&gt;. A fractional guess inside the range can be counted as an attempt even though the generated target is always an integer. For example, 50.5 against a secret of 51 produces “too low” and enters the history. The number input's default step can mark that value invalid, but the click handler and Enter handler call &lt;code&gt;submitGuess&lt;/code&gt; directly, without a validating form submission. An integer guard before recording history would close that gap; it is not part of the current implementation.&lt;/p&gt;

&lt;p&gt;I turned this small state-machine example into a free browser game: &lt;a href="https://begoodtool.com/guess-number-game/en" rel="noopener noreferrer"&gt;Guess the Number Game&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>vue</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Modeling Retirement Withdrawals Without Hiding Cash-Flow Timing</title>
      <dc:creator>Joe Lin</dc:creator>
      <pubDate>Tue, 10 Nov 2026 13:00:03 +0000</pubDate>
      <link>https://dev.to/begoodtool/modeling-retirement-withdrawals-without-hiding-cash-flow-timing-2kn0</link>
      <guid>https://dev.to/begoodtool/modeling-retirement-withdrawals-without-hiding-cash-flow-timing-2kn0</guid>
      <description>&lt;p&gt;A retirement projection can change even when you keep the starting balance, withdrawal amount, and return rate identical. I'll show how to audit the order of spending and investment growth, keep inflation adjustments consistent, and interpret the final row without mistaking a finite simulation for a guarantee.&lt;/p&gt;

&lt;p&gt;I'm using a Vue calculator that produces a year-by-year table as the working example. The arithmetic is small enough to read in one sitting, but the labels need more care than the loop: a fixed-return scenario is not a forecast, and surviving a finite simulation is not the same as lasting forever.&lt;/p&gt;

&lt;h2&gt;
  
  
  Spend First, Then Grow What Remains
&lt;/h2&gt;

&lt;p&gt;The inputs describe starting savings, a first-year annual withdrawal, an expected annual return, and inflation. Internally, percentage inputs become decimal rates. Savings and withdrawals must both be positive before the computed result returns a projection.&lt;/p&gt;

&lt;p&gt;The central convention is that the withdrawal happens at the &lt;strong&gt;start&lt;/strong&gt; of each year. Here is the loop from the implementation, with its inline comment omitted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="no"&gt;MAX_YEARS&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;withdrawThisYear&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;W&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;inf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;afterWithdraw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;withdrawThisYear&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;afterWithdraw&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;withdraw&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;withdrawThisYear&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;yearsLasted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;depleted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;afterWithdraw&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;withdraw&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;withdrawThisYear&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;yearsLasted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With one million in savings, a 40,000 withdrawal, and a 5% return, the first year ends at 1,008,000: subtract 40,000, then multiply the remaining 960,000 by 1.05. Growing the original million first and subtracting afterward would produce 1,010,000.&lt;/p&gt;

&lt;p&gt;That 2,000 difference is the return on money the person already spent. Neither ordering is universally wrong, but they describe different cash-flow assumptions. A monthly spending model would describe something else again. I prefer showing the convention next to the results instead of making readers infer it from a smooth chart.&lt;/p&gt;

&lt;p&gt;The table stores each year's withdrawal and closing balance. That makes it possible to inspect the first row and check the arithmetic manually before trusting the longer projection. For financial UI work, a small auditable table can explain more than another decimal place.&lt;/p&gt;

&lt;h2&gt;
  
  
  Inflation Changes the Withdrawal, Not Just a Summary Label
&lt;/h2&gt;

&lt;p&gt;The first withdrawal uses an exponent of zero, so it remains exactly the entered amount. In the following year, the withdrawal grows by one inflation factor; in year three, by two.&lt;/p&gt;

&lt;p&gt;For example, with 2% inflation, a 40,000 initial withdrawal becomes 40,800 in year two and 41,616 in year three. Those are nominal amounts: the currency totals increase to represent the assumption that the same spending basket becomes more expensive.&lt;/p&gt;

&lt;p&gt;The implementation also calculates a real-return summary:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;realReturn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;inf&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At 5% return and 2% inflation, that is approximately 2.94%, rather than exactly 3%. Subtracting the rates is a useful approximation, but dividing the growth factors is the calculation actually used here.&lt;/p&gt;

&lt;p&gt;An important detail: the loop still grows the balance using the &lt;strong&gt;nominal&lt;/strong&gt; return &lt;code&gt;r&lt;/code&gt;. The real return is explanatory output, not a second adjustment applied to the portfolio. Replacing &lt;code&gt;r&lt;/code&gt; with the real return while continuing to increase withdrawals for inflation would mix accounting conventions and effectively penalize inflation twice.&lt;/p&gt;

&lt;p&gt;You can model everything in today's purchasing power instead, but then the cash flows must use that convention consistently. I find this distinction easy to miss because both approaches can produce plausible-looking tables. The presence of an inflation input does not, by itself, tell you which currency basis a result uses.&lt;/p&gt;

&lt;p&gt;The first-year withdrawal rate is similarly descriptive: it is &lt;code&gt;W / S&lt;/code&gt;, not a rule that recalculates spending as a percentage of the remaining balance every year. Those two withdrawal policies behave very differently after losses.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Depletion Row Has a Specific Meaning
&lt;/h2&gt;

&lt;p&gt;When the next withdrawal would leave a balance of zero or less, the code adds a final row with a zero balance and stops. It sets &lt;code&gt;yearsLasted&lt;/code&gt; to the number of earlier completed years.&lt;/p&gt;

&lt;p&gt;That row records the &lt;strong&gt;planned&lt;/strong&gt; withdrawal, not necessarily an amount the portfolio could fully pay. If the year starts with 10,000 but the planned withdrawal is 12,000, the table still contains 12,000 in its withdrawal column. It is not a cash ledger proving that 12,000 was available.&lt;/p&gt;

&lt;p&gt;There is an equality boundary too: if savings exactly cover the next withdrawal, the &lt;code&gt;&amp;lt;= 0&lt;/code&gt; condition takes the depletion branch. Consequently, the completed-year count excludes that year, even though its start-of-year withdrawal could be paid exactly. Here, &lt;code&gt;yearsLasted&lt;/code&gt; counts earlier years that reached the growth step; it does not count every fully funded annual withdrawal.&lt;/p&gt;

&lt;p&gt;With savings of 100, annual withdrawals of 50, and zero return and inflation, year one closes at 50. The year-two withdrawal uses the remaining 50 exactly, but the result reports &lt;code&gt;yearsLasted: 1&lt;/code&gt;. The English subtitle then substitutes that count into "your savings run out around year 1," although the zero-balance row is year two. That is a mismatch between the table and subtitle, not just rounding.&lt;/p&gt;

&lt;p&gt;I would test this branch separately from the usual long-running example. Inputs that never enter the depletion path cannot tell you whether the last row, count, and explanatory text agree. Zero return and zero inflation make particularly useful fixtures because the expected arithmetic is transparent. A clearer UI would distinguish the depletion row's year from the completed-year count; the current implementation does not.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sixty Years Is Not Indefinitely
&lt;/h2&gt;

&lt;p&gt;The implementation caps the simulation at 60 years:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;MAX_YEARS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the loop, it sets &lt;code&gt;sustainable&lt;/code&gt; to &lt;code&gt;!depleted&lt;/code&gt;. That only establishes that the scenario did not hit the depletion condition during the simulated horizon. The current English headline says savings can last indefinitely; that wording is stronger than the calculation supports.&lt;/p&gt;

&lt;p&gt;For a simple counterexample, take 1,000 in savings, a 10 annual withdrawal, and zero return and inflation. There is still money after year 60, but the balance plainly does not support withdrawals forever. This is a label limitation worth stating explicitly.&lt;/p&gt;

&lt;p&gt;The model also omits taxes, fees, changing spending needs, and variable investment returns. A constant average return cannot demonstrate resilience to an unfortunate sequence of losses. I use this kind of calculation to understand assumptions and cash-flow mechanics, not to establish a safe withdrawal guarantee or give investment advice.&lt;/p&gt;

&lt;p&gt;I turned that year-by-year calculation into a small free tool: &lt;a href="https://begoodtool.com/retirement-withdrawal-calculator/en" rel="noopener noreferrer"&gt;Retirement Withdrawal Calculator&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>vue</category>
      <category>finance</category>
    </item>
    <item>
      <title>How to Measure Reaction Time Without Counting the Waiting Period</title>
      <dc:creator>Joe Lin</dc:creator>
      <pubDate>Mon, 19 Oct 2026 13:00:03 +0000</pubDate>
      <link>https://dev.to/begoodtool/how-to-measure-reaction-time-without-counting-the-waiting-period-2h2k</link>
      <guid>https://dev.to/begoodtool/how-to-measure-reaction-time-without-counting-the-waiting-period-2h2k</guid>
      <description>&lt;p&gt;A reaction-time test becomes unfair if it measures how long the screen waited before turning green. I built the test as a small state machine: idle, waiting, ready, result, and early. The timer starts only when the ready state begins, so the result represents the click-to-signal interval rather than the random delay.&lt;/p&gt;

&lt;h2&gt;
  
  
  Random delay prevents anticipation
&lt;/h2&gt;

&lt;p&gt;Starting a round chooses a delay between 1.2 and 4 seconds:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;startWaiting&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;phase&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;waiting&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1200&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2800&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;clearWait&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;waitTimer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;phase&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ready&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;startTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;performance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The random interval makes memorizing a fixed countdown less useful. &lt;code&gt;clearWait&lt;/code&gt; runs before scheduling a new timer, which prevents an old round from changing the state after the player has already started another one.&lt;/p&gt;

&lt;h2&gt;
  
  
  A state machine makes early clicks explicit
&lt;/h2&gt;

&lt;p&gt;The pad handler treats the same click differently depending on the phase:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;onPad&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;switch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;phase&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;idle&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;result&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;early&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="nf"&gt;startWaiting&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;waiting&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="nf"&gt;clearWait&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="nx"&gt;phase&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;early&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ready&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ms&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;performance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;startTime&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;lastMs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ms&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nx"&gt;attempts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ms&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;phase&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;result&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An early click cancels the timer and records no fake reaction time. A result click starts the next round. This is simpler to reason about than a collection of booleans such as &lt;code&gt;isStarted&lt;/code&gt;, &lt;code&gt;isReady&lt;/code&gt;, and &lt;code&gt;hasError&lt;/code&gt;, where contradictory combinations are possible.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use a monotonic clock for elapsed time
&lt;/h2&gt;

&lt;p&gt;The test records &lt;code&gt;performance.now()&lt;/code&gt; at the exact transition to &lt;code&gt;ready&lt;/code&gt;, then subtracts another &lt;code&gt;performance.now()&lt;/code&gt; on the click. That clock is designed for intervals and is not affected by a user changing the wall clock. Rounding to an integer millisecond gives the display a stable unit while preserving the basic measurement.&lt;/p&gt;

&lt;p&gt;The computed average is also intentionally transparent:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;avgMs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;attempts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;attempts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;attempts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The best score is saved separately in local storage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bestMs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;ms&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;bestMs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;bestMs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ms&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;saveBest&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Only the best value is restored on mount; the current attempt history is a session concern. Storage errors and quota failures are ignored so a privacy setting cannot break the game.&lt;/p&gt;

&lt;p&gt;The limitations are significant. Browser scheduling, display refresh rate, input devices, touch latency, and human anticipation all affect the result. A four-second random delay is not cryptographic randomness, and this is not a medical or scientific reflex measurement. A fast score on one device is not directly comparable with a score on another.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cleanup is part of measuring correctly
&lt;/h2&gt;

&lt;p&gt;The component calls &lt;code&gt;onBeforeUnmount(clearWait)&lt;/code&gt;. That is easy to overlook, but a pending timeout is still a future state transition. If the user navigates away while the test is waiting, the old callback should not wake up and mutate a component that is no longer visible. The same cleanup happens when an early click cancels the round.&lt;/p&gt;

&lt;p&gt;The phase names also make the user experience testable. An idle pad can start a round; a waiting pad can only produce an early result; a ready pad records a number; and a result pad starts again. I can test those transitions without relying on the color of the button. In a small game, that is often a better test seam than trying to simulate an entire browser repaint.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;performance.now()&lt;/code&gt; is intentionally paired with a click event, but event timing still includes the browser and hardware pipeline. A high-refresh monitor may show the signal earlier than a low-refresh one, and touch events usually have different latency from a mouse. The right takeaway is relative practice on one setup, not a universal ranking.&lt;/p&gt;

&lt;p&gt;The saved best score has a similarly narrow promise. It stores one integer under &lt;code&gt;bgt_reaction_best_v1&lt;/code&gt;, catches storage errors, and restores it on mount. It does not sync across devices or prove that a score was produced honestly. That modest scope keeps the game reliable when storage is blocked and keeps the result’s limitations visible.&lt;/p&gt;

&lt;p&gt;The rating thresholds are deliberately simple: under 200 ms is fast, under 300 is good, under 400 is average, and everything else is slow. They are labels for feedback, not a scientific classification. Keeping those boundaries in a computed value means changing the copy or adding localization does not alter how elapsed time is recorded.&lt;/p&gt;

&lt;p&gt;In other words, the state machine measures one interval, while the rating only interprets it afterward. Keeping those responsibilities separate makes both pieces easier to explain and replace.&lt;/p&gt;

&lt;p&gt;That separation also keeps the game responsive when copy changes, localization changes, or a device reports an unusual input event.&lt;/p&gt;

&lt;p&gt;The test also benefits from refusing to manufacture a number after an early click. An early result is a distinct phase with no &lt;code&gt;lastMs&lt;/code&gt; update, so the average is based only on completed ready-state attempts. That keeps the summary honest and gives the player a clear way to try again instead of quietly adding a penalty or an impossible zero to the history.&lt;/p&gt;

&lt;p&gt;In practice, I would use several attempts rather than treating the best result as a personal measurement. The component exposes the average and best values separately for that reason. One unusually quick click can be interesting, but repeated measurements are a better description of how a particular person and device behave together.&lt;/p&gt;

&lt;p&gt;I turned this state-machine experiment into a small free tool: &lt;a href="https://begoodtool.com/reaction-time-test/en" rel="noopener noreferrer"&gt;Reaction Time Test&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>vue</category>
      <category>games</category>
    </item>
    <item>
      <title>Why CAGR Is Not the Same as Your Actual Year-by-Year Return</title>
      <dc:creator>Joe Lin</dc:creator>
      <pubDate>Sun, 18 Oct 2026 13:00:03 +0000</pubDate>
      <link>https://dev.to/begoodtool/why-cagr-is-not-the-same-as-your-actual-year-by-year-return-1k47</link>
      <guid>https://dev.to/begoodtool/why-cagr-is-not-the-same-as-your-actual-year-by-year-return-1k47</guid>
      <description>&lt;p&gt;CAGR is useful because it compresses a beginning value, ending value, and time span into one annualized rate. It is also easy to misuse as if an investment grew at that rate every year. I built the calculator to show both the compact percentage and a reconstructed year-by-year path, making that distinction hard to miss.&lt;/p&gt;

&lt;h2&gt;
  
  
  The core formula describes an equivalent rate
&lt;/h2&gt;

&lt;p&gt;The computed result normalizes the inputs and clamps the number of years:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;beginValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;endValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;years&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="mi"&gt;80&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cagr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;Y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The formula asks: “What constant annual rate would turn the beginning value into the ending value over &lt;code&gt;Y&lt;/code&gt; years?” It does not inspect the path between those endpoints. A portfolio could crash and recover and still have the same CAGR as a smooth line.&lt;/p&gt;

&lt;h2&gt;
  
  
  Total return answers a different question
&lt;/h2&gt;

&lt;p&gt;The source keeps simple growth separate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;totalReturn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;gain&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;totalReturn&lt;/code&gt; is the complete change across the period. &lt;code&gt;cagr&lt;/code&gt; is that change annualized with compounding. A 21 percent total return over three years is not a 21 percent annual return; the equivalent annual rate is smaller. Showing both prevents a familiar percentage from being read with the wrong time unit.&lt;/p&gt;

&lt;h2&gt;
  
  
  The yearly table is a reconstructed path
&lt;/h2&gt;

&lt;p&gt;Once CAGR is known, the tool builds illustrative values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;rows&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;Y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;cagr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;year&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;growth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This table is not historical data. It is the smooth path implied by the equivalent rate. That makes it useful for explaining compounding, but it should not be mistaken for the actual annual returns that produced the endpoint.&lt;/p&gt;

&lt;p&gt;The formatter makes the sign explicit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fmtPct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nf"&gt;isNaN&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;—&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pct&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sign&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;pct&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;+&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;sign&lt;/span&gt;&lt;span class="p"&gt;}${&lt;/span&gt;&lt;span class="nx"&gt;pct&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toFixed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;%`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Negative CAGR is valid when the ending value is lower, while zero gets no plus sign. The input guard rejects zero or negative endpoints because the logarithmic intuition behind growth rates becomes misleading for this simple ratio model.&lt;/p&gt;

&lt;p&gt;The major limitation is what the formula leaves out: deposits, withdrawals, dividends, taxes, fees, inflation, volatility, and cash-flow timing. If money enters or leaves during the period, a money-weighted or time-weighted return measure may be more appropriate. The 80-year cap is also a display boundary, not a statement about finance.&lt;/p&gt;

&lt;h2&gt;
  
  
  A smooth chart can be honest if it labels the assumption
&lt;/h2&gt;

&lt;p&gt;The reconstructed rows start at year zero, where &lt;code&gt;value&lt;/code&gt; equals the beginning value and &lt;code&gt;growth&lt;/code&gt; is zero. Each later row compounds the same equivalent rate again. That makes the table a teaching aid: it shows what “annualized” means without claiming that the account actually followed that line.&lt;/p&gt;

&lt;p&gt;There is also a useful relationship to check manually. The final row should equal the ending input apart from floating-point and display rounding, because the rate was derived from that endpoint. &lt;code&gt;gain&lt;/code&gt; should equal &lt;code&gt;end - begin&lt;/code&gt;, while the cumulative &lt;code&gt;growth&lt;/code&gt; in the last row should express the same difference. These simple invariants are valuable when changing a financial component; they catch swapped endpoints and off-by-one year loops quickly.&lt;/p&gt;

&lt;p&gt;Negative results deserve the same care as positive ones. If the ending value is lower, &lt;code&gt;fmtPct&lt;/code&gt; adds no plus sign and the reconstructed path declines each year. That path is still only an equivalent path. A real investment can have negative years followed by recovery, and CAGR alone cannot tell the story of volatility or drawdown.&lt;/p&gt;

&lt;p&gt;If a future version adds cash flows, I would not quietly overload these inputs. I would introduce a separate return method and explain its assumptions, because a single “CAGR” label should not start pretending to be an internal rate of return calculator.&lt;/p&gt;

&lt;p&gt;The formatter rounds currency-like values to whole units while leaving the percentage at two decimal places. That is a display choice, not a loss of precision in the calculation. Keeping raw numeric fields in the returned result means another presentation—more decimals, a chart tooltip, or a localized currency symbol—can be added without rewriting the formula.&lt;/p&gt;

&lt;p&gt;That split between raw result and formatted text is especially helpful when comparing two scenarios: the UI can show friendly numbers while tests still inspect the unrounded &lt;code&gt;cagr&lt;/code&gt;, &lt;code&gt;gain&lt;/code&gt;, and row values.&lt;/p&gt;

&lt;p&gt;The final result remains a model, not a guarantee, and the input values still need context before anyone makes a decision.&lt;/p&gt;

&lt;p&gt;I would also avoid comparing CAGR values with different time windows as if they were automatically equivalent. The exponent normalizes each result to an annual rate, but a two-year result and a twenty-year result can have very different uncertainty, volatility, and practical meaning. The component can calculate both because the formula is compact; interpretation still belongs to the person supplying the beginning and ending values.&lt;/p&gt;

&lt;p&gt;That boundary is useful for implementation too. The view does not fetch market prices, infer missing dates, or decorate the result with a prediction. It receives three numbers, calculates a reproducible equivalent rate, and exposes the intermediate rows. Fewer hidden inputs make the output easier to audit, even when the financial story behind those inputs is complicated.&lt;/p&gt;

&lt;p&gt;I turned this explanation into a small free tool: &lt;a href="https://begoodtool.com/cagr-calculator/en" rel="noopener noreferrer"&gt;CAGR Calculator&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>vue</category>
      <category>finance</category>
    </item>
    <item>
      <title>How a Scoreboard Keeps Responsive Layout and Game State in Sync</title>
      <dc:creator>Joe Lin</dc:creator>
      <pubDate>Sat, 17 Oct 2026 13:00:05 +0000</pubDate>
      <link>https://dev.to/begoodtool/how-a-scoreboard-keeps-responsive-layout-and-game-state-in-sync-1l37</link>
      <guid>https://dev.to/begoodtool/how-a-scoreboard-keeps-responsive-layout-and-game-state-in-sync-1l37</guid>
      <description>&lt;p&gt;An online scoreboard is mostly buttons and numbers, but a useful one has to survive team changes, narrow screens, fullscreen mode, and accidental resets. I built the view with a small reactive team model and derived layout values, instead of storing every CSS position as mutable state.&lt;/p&gt;

&lt;h2&gt;
  
  
  The team list is the source of truth
&lt;/h2&gt;

&lt;p&gt;The initial state is plain data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MAX_TEAMS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stepOptions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;teams&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;score&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;score&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;hasScore&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nx"&gt;teams&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;some&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;score&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Adding a team appends an object with a stable id and refuses to exceed &lt;code&gt;MAX_TEAMS&lt;/code&gt;. Removing a team splices the same array. Scores are changed by a delta, so the step control can support one, two, three, or five points without duplicating four button handlers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Leaders are derived, including ties
&lt;/h2&gt;

&lt;p&gt;The view does not store a “leader” flag on every team:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;leaderIds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;teams&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;max&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;teams&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;score&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;teams&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;score&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;max&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That naturally handles ties. If two teams have the same highest score, both ids are highlighted. Derived state also avoids stale flags after a score decrement or team removal.&lt;/p&gt;

&lt;h2&gt;
  
  
  Layout depends on viewport and team count
&lt;/h2&gt;

&lt;p&gt;The number of columns is a computed breakpoint rule:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;maxColumns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;viewportWidth&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;768&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;viewportWidth&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;minLineCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ceil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;teams&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;maxColumns&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The actual CSS variables come from &lt;code&gt;lineCount&lt;/code&gt; and &lt;code&gt;columnCount&lt;/code&gt;. &lt;code&gt;getGridItemStyle&lt;/code&gt; handles an incomplete last row and adjusts it for left, center, or right alignment. That is why the view needs both a responsive width listener and explicit line-count clamping: changing teams can make the previous layout invalid.&lt;/p&gt;

&lt;h2&gt;
  
  
  LED digits are a rendering adapter
&lt;/h2&gt;

&lt;p&gt;The LED theme does not use a font. It maps each character to segments:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ledCharacterSegments&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;abcdef&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bc&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;abdeg&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;abcdg&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bcfg&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;abcdefg&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;abcdfg&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;-&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;g&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getLedSegments&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;character&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;ledCharacterSegments&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;character&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;getLedCharacters(score)&lt;/code&gt; splits the score into characters, allowing negative scores to render the mapped minus segment. An unknown character simply has no segments, which is safer than throwing during a game.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fullscreen is progressive enhancement
&lt;/h2&gt;

&lt;p&gt;Entering fullscreen first sets the component’s own &lt;code&gt;isFullscreen&lt;/code&gt; state, then tries &lt;code&gt;requestFullscreen&lt;/code&gt;. If the browser rejects it or does not support it, the CSS mode still works. Escape removes the listener and exits the native document fullscreen when present. That fallback is important on iOS and embedded browsers.&lt;/p&gt;

&lt;p&gt;The limitations are practical: state is browser-local, the score has no undo history, and the fullscreen API can be denied by permissions or user gesture rules. A reset uses SweetAlert confirmation, but closing a tab still loses an unsaved match unless the rest of the source’s persistence logic is used. The 18-team cap also makes the layout predictable rather than infinite.&lt;/p&gt;

&lt;h2&gt;
  
  
  Small defensive rules keep games from becoming layout bugs
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;clampLineCount&lt;/code&gt; is one of those helpers that looks like UI plumbing but protects the actual game. It rounds a user-entered line count, keeps it at least the number of lines required by the current team count, and caps it at the number of teams. After adding or removing a team, &lt;code&gt;syncLineCount&lt;/code&gt; recalculates the default unless the player explicitly customized the layout.&lt;/p&gt;

&lt;p&gt;The viewport listener follows the same principle. It ignores updates when &lt;code&gt;window.innerWidth&lt;/code&gt; has not changed, then synchronizes the grid only when necessary. Without that check, resize events could trigger a stream of redundant reactive updates while someone drags a browser edge. It is not a performance breakthrough, but it keeps the state transitions predictable.&lt;/p&gt;

&lt;p&gt;I also like that reset operations are separate. &lt;code&gt;resetScores&lt;/code&gt; preserves names, themes, and layout; &lt;code&gt;resetAll&lt;/code&gt; returns the whole game to two unnamed teams and the default style. A confirmation dialog protects both actions, but their scopes remain different in the data model. That distinction is exactly what prevents a “reset scores” button from becoming an accidental “erase the match setup” button.&lt;/p&gt;

&lt;p&gt;The score itself is not formatted with a fixed width, so a negative value can still be represented as a normal string and then mapped through the minus LED segment. That is a small edge case, but it is a good reminder that a scoreboard is not necessarily a countdown: decrementing below zero should not crash the renderer or silently become an empty display.&lt;/p&gt;

&lt;p&gt;The same defensive approach appears in the LED lookup: unknown characters return an empty segment string. A missing mapping becomes a blank glyph rather than an exception that stops every team from rendering.&lt;/p&gt;

&lt;p&gt;The responsive calculations also avoid storing pixel coordinates. A team’s position is derived from its index, the current column count, the selected alignment, and the number of empty cells in the final row. When the viewport changes, the same team data can be laid out again. This is less fragile than updating every card’s &lt;code&gt;left&lt;/code&gt; and &lt;code&gt;top&lt;/code&gt; values and then trying to remember which values belonged to the previous breakpoint.&lt;/p&gt;

&lt;p&gt;That is why the scoreboard remains a useful example beyond keeping points. It demonstrates a general Vue pattern: store the smallest meaningful state, derive presentation details from it, and put browser capabilities such as fullscreen behind a fallback. The game remains usable even when a particular visual enhancement is unavailable.&lt;/p&gt;

&lt;p&gt;I turned this state-and-layout model into a small free tool: &lt;a href="https://begoodtool.com/scoreboard/en" rel="noopener noreferrer"&gt;Online Scoreboard&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>vue</category>
      <category>games</category>
    </item>
    <item>
      <title>Why Splitting a Bill Requires an Explicit Rounding Policy</title>
      <dc:creator>Joe Lin</dc:creator>
      <pubDate>Thu, 15 Oct 2026 13:00:05 +0000</pubDate>
      <link>https://dev.to/begoodtool/why-splitting-a-bill-requires-an-explicit-rounding-policy-i7b</link>
      <guid>https://dev.to/begoodtool/why-splitting-a-bill-requires-an-explicit-rounding-policy-i7b</guid>
      <description>&lt;p&gt;Dividing a restaurant bill is easy until the cents do not divide evenly, or a group wants every person to pay a whole currency unit. I built the tip splitter with two separate totals: the mathematically exact split and an optional round-up split. That keeps the rounding decision visible instead of hiding a mismatch in the final number.&lt;/p&gt;

&lt;h2&gt;
  
  
  Normalize people before dividing
&lt;/h2&gt;

&lt;p&gt;The calculator clamps the number of diners:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;changePeople&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;delta&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;people&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;delta&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;people&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;99&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The computed calculation applies similar protection even if a value arrives from a pasted form field:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bill&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tipPercent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;people&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;Math.floor&lt;/code&gt; prevents a fractional person count, and the lower bound avoids division by zero. A zero bill returns &lt;code&gt;null&lt;/code&gt;, which lets the template show an empty state rather than pretending that a blank form is a valid split.&lt;/p&gt;

&lt;h2&gt;
  
  
  Calculate tip and total independently
&lt;/h2&gt;

&lt;p&gt;The core values are intentionally boring:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;tipAmount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;tipAmount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;perPerson&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;rounding&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;chargedTotal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keeping &lt;code&gt;tipAmount&lt;/code&gt; and &lt;code&gt;total&lt;/code&gt; separate matters when the interface displays a breakdown. A reader can audit whether a percentage was applied to the bill, and the program does not lose the original total just because the display later rounds numbers with &lt;code&gt;toLocaleString&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Round up the person, then explain the difference
&lt;/h2&gt;

&lt;p&gt;The optional policy is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;roundUp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;perPerson&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ceil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;perPerson&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;chargedTotal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;perPerson&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;rounding&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;chargedTotal&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is not “round each person independently.” It rounds the shared per-person charge upward, then recomputes the amount collected. &lt;code&gt;rounding&lt;/code&gt; is the extra money created by that policy, so the result can say where the difference went. With a total of 100 divided among three people, a whole-unit per-person charge collects 102, not 100.&lt;/p&gt;

&lt;p&gt;The formatting helper keeps calculation and presentation apart:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fmt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nf"&gt;isNaN&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;—&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toLocaleString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;minimumFractionDigits&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;maximumFractionDigits&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The locale is left to the browser, while the arithmetic stays in ordinary JavaScript numbers. That is convenient for a small tool, but it is also a limitation: binary floating-point can produce tiny internal fractions, and currencies with special minor-unit rules are not modeled. The calculator does not account for tax, per-item ordering, unequal shares, service charges, or a tip base different from the entered bill.&lt;/p&gt;

&lt;p&gt;The maximum of 99 people is another deliberate UI boundary, not a mathematical one. Large events need a different allocation model, especially when some attendees pay only for selected items.&lt;/p&gt;

&lt;h2&gt;
  
  
  The result should expose the policy, not just the answer
&lt;/h2&gt;

&lt;p&gt;I intentionally return both &lt;code&gt;total&lt;/code&gt; and &lt;code&gt;chargedTotal&lt;/code&gt;. When round-up is disabled, they are equal and &lt;code&gt;rounding&lt;/code&gt; is zero. When it is enabled, &lt;code&gt;chargedTotal&lt;/code&gt; explains why multiplying the displayed per-person number back by the number of people no longer matches the exact bill. That makes the UI suitable for a real conversation at the table: “Do we want to split exact cents, or should everyone pay a whole unit?”&lt;/p&gt;

&lt;p&gt;The presets &lt;code&gt;[10, 15, 18, 20]&lt;/code&gt; are convenience values for the tip percentage, not a claim that one percentage is correct. The calculator does not infer tax or service fees, and it does not decide whether the tip should be calculated before or after tax. Those choices belong in the entered bill and the user’s local custom.&lt;/p&gt;

&lt;p&gt;For unequal groups, the next feature would not be another rounding flag. It would be an item-level allocation model, because “three people” is not enough information when one person ordered a much more expensive meal. That is a good example of a simple tool’s boundary: making equal splitting precise does not make it fair for every group.&lt;/p&gt;

&lt;p&gt;Even with that boundary, separating arithmetic from display is worthwhile. The computed object is small, testable, and easy to inspect, while &lt;code&gt;fmt&lt;/code&gt; can change locale presentation without changing who owes what.&lt;/p&gt;

&lt;p&gt;The number of people is also changed through a dedicated helper rather than trusting a stepper component to enforce its own bounds. That matters for keyboard input and pasted values, where browser controls can be bypassed. The calculation repeats the lower-bound check, so the UI remains safe even if the state is changed by another component later.&lt;/p&gt;

&lt;p&gt;That duplication is intentional validation at the calculation boundary. Form constraints improve the experience, but computed financial output should not depend on a particular input widget behaving perfectly.&lt;/p&gt;

&lt;p&gt;It also keeps the result predictable when the bill is edited rapidly.&lt;/p&gt;

&lt;p&gt;One subtle benefit of returning &lt;code&gt;rounding&lt;/code&gt; explicitly is that the interface can explain a surplus without pretending it is part of the tip. The tip remains &lt;code&gt;tipAmount&lt;/code&gt;, the exact bill remains &lt;code&gt;total&lt;/code&gt;, and the whole-unit collection is &lt;code&gt;chargedTotal&lt;/code&gt;. Those names make it much harder to accidentally display the rounded charge as if it were the restaurant’s original total.&lt;/p&gt;

&lt;p&gt;I would keep this policy visible in any test cases too. A normal split should satisfy &lt;code&gt;perPerson * people === total&lt;/code&gt; up to floating-point precision, while a round-up split should satisfy &lt;code&gt;perPerson * people === chargedTotal&lt;/code&gt; and report the difference in &lt;code&gt;rounding&lt;/code&gt;. Testing those relationships is more useful than testing only one sample bill.&lt;/p&gt;

&lt;p&gt;I turned this explicit rounding model into a small free tool: &lt;a href="https://begoodtool.com/tip-split-calculator/en" rel="noopener noreferrer"&gt;Tip &amp;amp; Bill Split Calculator&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>vue</category>
      <category>finance</category>
    </item>
    <item>
      <title>How to Generate Bingo Cards Without Accidentally Reusing the Same Words</title>
      <dc:creator>Joe Lin</dc:creator>
      <pubDate>Wed, 14 Oct 2026 13:00:06 +0000</pubDate>
      <link>https://dev.to/begoodtool/how-to-generate-bingo-cards-without-accidentally-reusing-the-same-words-50k0</link>
      <guid>https://dev.to/begoodtool/how-to-generate-bingo-cards-without-accidentally-reusing-the-same-words-50k0</guid>
      <description>&lt;p&gt;Generating a bingo card looks like filling a grid, but the details show up as soon as users request different sizes, custom words, free centers, and multiple cards. I built the generator around a normalized list, a Fisher–Yates shuffle, and a separate play-state model so printable cards and clickable games do not fight each other.&lt;/p&gt;

&lt;h2&gt;
  
  
  Validate the content before building cells
&lt;/h2&gt;

&lt;p&gt;Custom text is turned into a trimmed list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;wordList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nx"&gt;wordText&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\r?\n&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;neededWords&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;totalCells&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;boardSize&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;boardSize&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;freeCenter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;totalCells&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;totalCells&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The count changes when the free-center option changes. &lt;code&gt;generate&lt;/code&gt; refuses to create a word card if there are fewer entries than &lt;code&gt;neededWords&lt;/code&gt;. That is better than silently placing blank cells and discovering the problem only after downloading a card.&lt;/p&gt;

&lt;h2&gt;
  
  
  Shuffle copies, not the source array
&lt;/h2&gt;

&lt;p&gt;The source uses a small Fisher–Yates implementation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;shuffle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;slice&lt;/code&gt; is important. A generator should not reorder the user’s original word list just because it is preparing one card. Fisher–Yates gives every remaining position a random candidate under &lt;code&gt;Math.random&lt;/code&gt;; it is not cryptographic randomness, but it is appropriate for a casual game.&lt;/p&gt;

&lt;p&gt;Number cards use a column pool:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbersPerColumn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;numbersPerColumn&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;start&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;numbersPerColumn&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;cols&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;shuffle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The classic five-column card has fifteen numbers per column. Larger boards use a pool equal to the board size. That is a product rule, not a universal bingo standard, so it belongs in the implementation rather than being implied by a generic “random grid” helper.&lt;/p&gt;

&lt;h2&gt;
  
  
  A free cell is data with behavior
&lt;/h2&gt;

&lt;p&gt;While creating cells, the center is marked and labeled:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isFree&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="nx"&gt;freeCenter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;row&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;center&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;col&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;cells&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;isFree&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nf"&gt;t&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bingoCardGenerator.freeText&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cols&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;col&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="nx"&gt;row&lt;/span&gt;&lt;span class="p"&gt;]),&lt;/span&gt;
  &lt;span class="na"&gt;marked&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;isFree&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;free&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;isFree&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Later, &lt;code&gt;toggleMark&lt;/code&gt; refuses to toggle free cells and records every other click in &lt;code&gt;history&lt;/code&gt;. That history powers undo without trying to infer intent from the current grid.&lt;/p&gt;

&lt;h2&gt;
  
  
  Winning lines are generated from the board size
&lt;/h2&gt;

&lt;p&gt;The win checker creates horizontal rows, vertical columns, and two diagonals, then tests every cell in a line. &lt;code&gt;connectedLineCount&lt;/code&gt; counts completed lines and &lt;code&gt;isCardWon&lt;/code&gt; compares it to the configured target. This supports a five-line game without hard-coding a 5×5 scan, and the maximum target is &lt;code&gt;size * 2 + 2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The limitation is that “random” does not mean unique. Multiple generated cards can share words or number patterns, especially with small custom lists. Word cards require enough distinct input entries, but the tool does not promise globally unique cards across a batch. Very large boards also create a lot of cells and a lot of work for browser-side PNG/ZIP downloads.&lt;/p&gt;

&lt;h2&gt;
  
  
  Accessibility and unusual board sizes are implementation details
&lt;/h2&gt;

&lt;p&gt;The generator creates column labels with spreadsheet-style letters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createColumnLabels&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;B&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;N&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;G&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;O&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;label&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;label&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromCharCode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;65&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;26&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;26&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The special five-column labels preserve the familiar B-I-N-G-O convention. Larger boards use A, B, …, Z, then AA and beyond, so an accessible cell label can still identify a position even when the visual card has no standard letter heading.&lt;/p&gt;

&lt;p&gt;The source also computes contrast text color from a six-digit hex color. That is a helpful default for custom themes, but it is not a complete accessibility audit: color contrast for every text/background combination, print output, and user-selected word length still needs testing. A free center is represented as a marked cell from the beginning, which means the play-mode win logic naturally includes it without a special case in every line scan.&lt;/p&gt;

&lt;p&gt;The download path is another boundary. Browser canvas and ZIP libraries can handle ordinary batches, but memory use grows with card count and board size. A phone generating many 31×31 image cards may hit a practical limit long before the JavaScript algorithm is mathematically wrong.&lt;/p&gt;

&lt;p&gt;The separate &lt;code&gt;mode&lt;/code&gt;, &lt;code&gt;cards&lt;/code&gt;, and &lt;code&gt;playMode&lt;/code&gt; refs are useful here. Generating a new batch can reset the active card and leave the printable data intact; entering play mode does not need to regenerate anything. That separation also makes it possible to download a generated card without marking cells, which is the expected behavior for a printable worksheet.&lt;/p&gt;

&lt;p&gt;There is a similar separation between a card’s &lt;code&gt;cells&lt;/code&gt; and its &lt;code&gt;history&lt;/code&gt;. The cell array describes the current board, while history describes the sequence of manual marks. That means undo can remove the most recent action without trying to reconstruct the whole board from a random seed. It also means a free cell can remain marked when the rest of the card is reset, because &lt;code&gt;resetCard&lt;/code&gt; restores each cell to its original &lt;code&gt;free&lt;/code&gt; status.&lt;/p&gt;

&lt;p&gt;For me, that is the main design lesson: generation and play are related features, but they are not the same state transition. Keeping them separate makes the printable workflow predictable and leaves room for future game rules without changing the code that creates a valid card.&lt;/p&gt;

&lt;p&gt;I turned this generator into a small free tool: &lt;a href="https://begoodtool.com/bingo-card-generator/en" rel="noopener noreferrer"&gt;Bingo Card Generator&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>vue</category>
      <category>games</category>
    </item>
  </channel>
</rss>
