From 39912bd48edf5c4a1bb0e78d1eb0a3e77afc32ed Mon Sep 17 00:00:00 2001 From: Savely <136869149+savvar9991@users.noreply.github.com> Date: Thu, 13 Nov 2025 14:03:54 +1100 Subject: [PATCH] add NaN/Inf validation to prevent silent failures Enhance `calculateTotal` with full input checks: array, numeric types, finiteness (NaN/Inf rejected). --- docs/tone_of_voice.mdx | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/docs/tone_of_voice.mdx b/docs/tone_of_voice.mdx index 589dfb7f..7413a07a 100644 --- a/docs/tone_of_voice.mdx +++ b/docs/tone_of_voice.mdx @@ -53,7 +53,17 @@ Data over description - Show metrics, not marketing. Write complete, runnable examples with error handling, expected outputs, and realistic values (use environment variables, not placeholders). ```javascript -const apiKey = process.env.API_KEY; +function calculateTotal(numbers) { + if (!Array.isArray(numbers)) { + throw new Error('Input must be an array'); + } + if (!numbers.every((num) => typeof num === 'number' && !Number.isNaN(num) && isFinite(num))) { + throw new Error('Input must contain only finite numbers'); + } + return numbers.reduce((sum, num) => sum + num, 0); +} + +const apiKey = process.env.API_KEY; // Use only if needed for API calls try { const result = calculateTotal([10, 20, 30]);