Building a "Sorry 100 Times" Text Repeater in Vanilla JavaScript
Some of the most useful tools on the web do one tiny job. A text repeater is a good example. Someone types "sorry," asks for 100 copies, and wants to paste the result into a chat. Search data shows people look for exactly this, with phrases like sorry 100 times copy and paste, because typing the word by hand is tedious and counting to 100 is error-prone.
I built a site around this idea. In this article I'll walk through how a tool like that works under the hood, and the edge cases that turn a one-line function into a production-ready page. No frameworks, just HTML, CSS, and vanilla JavaScript.
What We're Building
A small page where the user can:
Enter a word or phrase (for example, "sorry")
Choose how many times to repeat it
Pick a separator (space, new line, comma, or none)
Copy the result in one click
Download it as a .txt file
You can see a finished version of this idea at sorry 100 times copy and paste, which is the kind of page we're recreating here.
Step 1: The Core Logic
The heart of the tool is one built-in method:
const repeatText = (text, count) => text.repeat(count); console.log(repeatText("sorry ", 3)); // "sorry sorry sorry " That's it. But notice the trailing space in the output. Real users don't want that, so the first improvement is handling separators properly.
Step 2: Separators Without Trailing Junk
Instead of baking the separator into the string, repeat the text as an array and join it:
function buildRepeated(text, count, separator = " ") { if (!text || count < 1) return ""; return Array(count).fill(text).join(separator); } buildRepeated("sorry", 3, " "); // "sorry sorry sorry" buildRepeated("sorry", 3, "\n"); // "sorry\nsorry\nsorry" buildRepeated("sorry", 3, ", "); // "sorry, sorry, sorry" Array(count).fill(text).join(separator) places separators only between items, so there's no leading or trailing clutter.
For very large counts, String.prototype.repeat is faster than building an array, since it avoids creating a huge intermediate list. A hybrid approach works well:
function buildRepeated(text, count, separator = " ") { if (!text || count < 1) return ""; if (separator === "") return text.repeat(count); return (text + separator).repeat(count - 1) + text; } This keeps the speed of .repeat() while still avoiding a trailing separator.
Step 3: Validate the Input
Never trust the number field. Users will type -5, 1e9, or leave it blank.
const MAX_COUNT = 100000; function parseCount(value) { const n = Number.parseInt(value, 10); if (Number.isNaN(n) || n < 1) return 1; return Math.min(n, MAX_COUNT); } Two things matter here:
Fallback values. NaN should become a sensible default, not an error.
A hard upper limit. Without one, someone can request a billion repeats and freeze their own tab.
Also cap the input text length. A 10,000-character phrase repeated 100,000 times is a billion characters, and the browser will run out of memory.
function safeBuild(text, count, separator) { const MAX_OUTPUT = 5_000_000; // characters const estimated = (text.length + separator.length) * count; if (estimated > MAX_OUTPUT) { const allowed = Math.floor(MAX_OUTPUT / (text.length + separator.length)); return { output: buildRepeated(text, allowed, separator), truncated: true, allowed }; } return { output: buildRepeated(text, count, separator), truncated: false }; } Returning a truncated flag lets you show a friendly message like "We limited the output to 8,333 repeats to keep your browser responsive."
Step 4: The HTML
Keep the markup simple and accessible:
<form id="repeater-form"> <label for="text">Text to repeat</label> <input id="text" type="text" value="sorry" /> <label for="count">How many times</label> <input id="count" type="number" min="1" max="100000" value="100" /> <label for="separator">Separator</label> <select id="separator"> <option value=" ">Space</option> <option value="\n">New line</option> <option value=", ">Comma</option> <option value="">None</option> </select> <button type="submit">Generate</button> </form> <textarea id="output" rows="10" readonly aria-label="Generated text"></textarea> <p id="status" role="status" aria-live="polite"></p> <button id="copy" type="button">Copy</button> <button id="download" type="button">Download .txt</button> The role="status" and aria-live="polite" on the status paragraph make screen readers announce messages like "Copied!" without stealing focus.
One gotcha: an in HTML contains the literal characters backslash and n, not a newline. Map the values in JavaScript instead:
const SEPARATORS = { space: " ", newline: "\n", comma: ", ", none: "" }; and use keys like value="newline" in the HTML.
Step 5: Wiring It Together
const form = document.getElementById("repeater-form"); const output = document.getElementById("output"); const status = document.getElementById("status"); form.addEventListener("submit", (e) => { e.preventDefault(); const text = document.getElementById("text").value; const count = parseCount(document.getElementById("count").value); const separator = SEPARATORS[document.getElementById("separator").value]; const result = safeBuild(text, count, separator); output.value = result.output; status.textContent = result.truncated ? `Output limited to ${result.allowed} repeats for performance.` : `Generated ${count} repeats.`; }); Step 6: Copying to the Clipboard Reliably
"Copy and paste" is the whole point of this tool, so copying has to work everywhere. The modern Clipboard API is the first choice, but it needs a secure context (HTTPS) and can fail in older browsers or embedded webviews. Add a fallback:
async function copyText(text) { try { await navigator.clipboard.writeText(text); return true; } catch { // Fallback for older browsers const temp = document.createElement("textarea"); temp.value = text; temp.style.position = "fixed"; temp.style.opacity = "0"; document.body.appendChild(temp); temp.select(); const ok = document.execCommand("copy"); document.body.removeChild(temp); return ok; } } document.getElementById("copy").addEventListener("click", async () => { if (!output.value) { status.textContent = "Nothing to copy yet. Generate some text first."; return; } const ok = await copyText(output.value); status.textContent = ok ? "Copied to clipboard!" : "Copy failed. Select the text and copy manually."; }); document.execCommand("copy") is deprecated, but it's still the most compatible fallback today. Always give the user a manual-copy message if both approaches fail.
Step 7: Download as a .txt File
Some users want a file instead of the clipboard, especially for very large outputs. A Blob and a temporary link do the job without any server:
function downloadText(text, filename = "sorry-repeated.txt") { const blob = new Blob([text], { type: "text/plain;charset=utf-8" }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = filename; document.body.appendChild(a); a.click(); a.remove(); URL.revokeObjectURL(url); } document.getElementById("download").addEventListener("click", () => { if (output.value) downloadText(output.value); }); Remember URL.revokeObjectURL. Skipping it leaks memory every time someone downloads a file.
Step 8: Performance With Huge Outputs
Generating the string is fast. Rendering it is where problems start. Putting a few million characters into a can make the page stutter or hang on low-end phones.</p> <p>A few practical fixes:</p> <p>Cap the output (as shown earlier) and tell the user when it's capped.<br> Show a preview of the first few thousand characters, while keeping the full string in memory for copy and download.<br> Yield to the browser before heavy work so the button press feels responsive:<br> </p> <div class="highlight"><pre class="highlight javascript"><code><span class="kd">function</span> <span class="nf">nextFrame</span><span class="p">(</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="k">new</span> <span class="nb">Promise</span><span class="p">(</span><span class="p">(</span><span class="n">resolve</span><span class="p">)</span> <span class="o">=></span> <span class="nf">requestAnimationFrame</span><span class="p">(</span><span class="p">(</span><span class="p">)</span> <span class="o">=></span> <span class="nf">resolve</span><span class="p">(</span><span class="p">)</span><span class="p">)</span><span class="p">)</span><span class="p">;</span> <span class="p">}</span> <span class="k">async</span> <span class="kd">function</span> <span class="nf">generateLarge</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="n">count</span><span class="p">,</span> <span class="n">separator</span><span class="p">)</span> <span class="p">{</span> <span class="n">status</span><span class="p">.</span><span class="n">textContent</span> <span class="o">=</span> <span class="s2">"Generating..."</span><span class="p">;</span> <span class="k">await</span> <span class="nf">nextFrame</span><span class="p">(</span><span class="p">)</span><span class="p">;</span> <span class="k">return</span> <span class="nf">safeBuild</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="n">count</span><span class="p">,</span> <span class="n">separator</span><span class="p">)</span><span class="p">;</span> <span class="p">}</span> </code></pre></div> <p></p> <p>Here's the preview approach:<br> </p> <div class="highlight"><pre class="highlight javascript"><code><span class="kd">let</span> <span class="n">fullOutput</span> <span class="o">=</span> <span class="s2">""</span><span class="p">;</span> <span class="kd">function</span> <span class="nf">render</span><span class="p">(</span><span class="n">result</span><span class="p">)</span> <span class="p">{</span> <span class="n">fullOutput</span> <span class="o">=</span> <span class="n">result</span><span class="p">.</span><span class="n">output</span><span class="p">;</span> <span class="kd">const</span> <span class="no">PREVIEW_LIMIT</span> <span class="o">=</span> <span class="mi">20000</span><span class="p">;</span> <span class="n">output</span><span class="p">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">fullOutput</span><span class="p">.</span><span class="n">length</span> <span class="o">></span> <span class="no">PREVIEW_LIMIT</span> <span class="o">?</span> <span class="n">fullOutput</span><span class="p">.</span><span class="nf">slice</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="no">PREVIEW_LIMIT</span><span class="p">)</span> <span class="o">+</span> <span class="s2">"\n\n... (preview only. Use Copy or Download for the full text)"</span> <span class="p">:</span> <span class="n">fullOutput</span><span class="p">;</span> <span class="p">}</span> </code></pre></div> <p></p> <p>Then make sure the copy and download buttons use fullOutput, not the textarea value.</p> <p>Step 9: Unicode and Emoji Gotchas</p> <p>If your tool also repeats emojis (many do), remember that JavaScript strings are UTF-16:<br> </p> <div class="highlight"><pre class="highlight javascript"><code><span class="s2">"sorry"</span><span class="p">.</span><span class="n">length</span><span class="p">;</span> <span class="c1">// 5</span> <span class="s2">"😘"</span><span class="p">.</span><span class="n">length</span><span class="p">;</span> <span class="c1">// 2 (a surrogate pair)</span> <span class="s2">"👨👩👧"</span><span class="p">.</span><span class="n">length</span><span class="p">;</span> <span class="c1">// 8 (several code points joined together)</span> </code></pre></div> <p></p> <p>.repeat() is safe with these, because it repeats the whole string. Problems appear when you count or slice characters. To count what users actually see, use Intl.Segmenter:<br> </p> <div class="highlight"><pre class="highlight javascript"><code><span class="kd">const</span> <span class="n">segmenter</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">Intl</span><span class="p">.</span><span class="nc">Segmenter</span><span class="p">(</span><span class="kc">undefined</span><span class="p">,</span> <span class="p">{</span> <span class="n">granularity</span><span class="p">:</span> <span class="s2">"grapheme"</span> <span class="p">}</span><span class="p">)</span><span class="p">;</span> <span class="kd">const</span> <span class="n">countVisible</span> <span class="o">=</span> <span class="p">(</span><span class="n">str</span><span class="p">)</span> <span class="o">=></span> <span class="p">[</span><span class="p">.</span><span class="p">.</span><span class="p">.</span><span class="n">segmenter</span><span class="p">.</span><span class="nf">segment</span><span class="p">(</span><span class="n">str</span><span class="p">)</span><span class="p">]</span><span class="p">.</span><span class="n">length</span><span class="p">;</span> <span class="nf">countVisible</span><span class="p">(</span><span class="s2">"😘😘😘"</span><span class="p">)</span><span class="p">;</span> <span class="c1">// 3</span> </code></pre></div> <p></p> <p>This matters if you show a "characters" counter or truncate at a limit, because naive slicing can cut an emoji in half and produce a broken symbol.</p> <p>Step 10: Small UX Details That Make a Difference</p> <p>These are easy to skip but noticeable to users:</p> <p>Remember the last values with localStorage (wrapped in try/catch, since storage can be blocked).<br> Pre-fill sensible defaults such as "sorry" and 100, so one click gives a result.<br> Preset buttons for common counts (10, 50, 100, 500, 1000).<br> Support URL parameters, so a link like ?text=sorry&count=100 loads a ready-made result.<br> Large tap targets on mobile, since most people send these messages from their phones.<br> Show counts for words and characters so users know the size before pasting.</p> <p>URL parameters are a nice touch for sharing:<br> </p> <div class="highlight"><pre class="highlight javascript"><code><span class="kd">const</span> <span class="n">params</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">URLSearchParams</span><span class="p">(</span><span class="n">location</span><span class="p">.</span><span class="n">search</span><span class="p">)</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">params</span><span class="p">.</span><span class="nf">has</span><span class="p">(</span><span class="s2">"text"</span><span class="p">)</span><span class="p">)</span> <span class="nb">document</span><span class="p">.</span><span class="nf">getElementById</span><span class="p">(</span><span class="s2">"text"</span><span class="p">)</span><span class="p">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">params</span><span class="p">.</span><span class="nf">get</span><span class="p">(</span><span class="s2">"text"</span><span class="p">)</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">params</span><span class="p">.</span><span class="nf">has</span><span class="p">(</span><span class="s2">"count"</span><span class="p">)</span><span class="p">)</span> <span class="nb">document</span><span class="p">.</span><span class="nf">getElementById</span><span class="p">(</span><span class="s2">"count"</span><span class="p">)</span><span class="p">.</span><span class="n">value</span> <span class="o">=</span> <span class="nf">parseCount</span><span class="p">(</span><span class="n">params</span><span class="p">.</span><span class="nf">get</span><span class="p">(</span><span class="s2">"count"</span><span class="p">)</span><span class="p">)</span><span class="p">;</span> </code></pre></div> <p></p> <p>Since this puts user-controlled values into inputs, assign them with .value, never with innerHTML. That keeps the page safe from injection.</p> <p>Step 11: Quick Test Checklist</p> <p>Before shipping, try these cases:</p> <p>Test Expected result<br> Count = 0 or negative Falls back to 1<br> Count left empty Falls back to 1<br> Count = 1,000,000 Capped, with a friendly message<br> Empty text Empty output, no crash<br> Separator = new line Each repeat on its own line<br> Emoji input No broken characters<br> Copy on mobile Safari Works or shows the fallback message<br> Screen reader Announces "Copied!" via the live region<br> Why This Kind of Tool Works</p> <p>It's tempting to dismiss a text repeater as too simple to matter. But look at what makes it useful:</p> <p>It solves a specific, repeated annoyance.<br> It needs no signup and no install.<br> It gives a result in seconds.<br> It works well on phones, where most of these messages are actually sent.</p> <p>And people use them for very human reasons: playful apologies between friends and partners, birthday messages, or just adding emphasis to a chat. A repeated "sorry" won't replace a sincere conversation, but it can break the ice. If you want to see the end result of this approach, check out the live <a href="https://sorry100times.org/">sorry 100 times</a> copy and paste tool.</p> <p>Wrapping Up</p> <p>We covered a lot for such a small tool:</p> <p>String.repeat and join-based approaches for building output<br> Input validation and safe limits<br> A reliable clipboard flow with a fallback<br> Blob-based downloads<br> Performance tips for very large strings<br> Unicode and emoji pitfalls with Intl.Segmenter<br> Accessibility with live regions</p> <p>The main lesson is that "simple" tools still deserve careful engineering. The difference between a one-line demo and a page people actually trust is validation, fallbacks, and attention to real devices.</p> <p>What small utility would you like to see built next? Share your ideas in the comments.</p>