JavaScript新手求助:实现遍历指定范围数组并替换含数字1的元素为Beep的函数
Hey there! Let's break down how to fix your function and add the extra rule you want—you're already off to a good start, just need to adjust how you check for digits and modify the array.
First, Let's Diagnose the Issues in Your Current Code
Your current approach has a couple of problems that prevent it from working as expected:
- When building
rangeArray, you're converting numbers to strings with a space (likei + " "), which makes your array hold strings instead of the mixed numbers/strings format from your example. - Using
splice(1, '1', "Beep")insideforEachis hardcoding a replacement at index 1 every time, instead of checking each element for the digit '1'.
Step 1: Implement the Basic "Beep" Replacement
Let's rewrite the rangeOfNumbers function to properly generate the array and replace any number containing '1' with "Beep". We'll use map() instead of modifying the array in place—it creates a new array with transformed values, which is cleaner and avoids bugs.
function convertToNumber(input) { let parsedInput = parseInt(input); if (!isNaN(parsedInput)) { return parsedInput; } else { return false; } } function rangeOfNumbers(start, end) { let rangeArray = []; // Generate the array of numbers from start to end for (let i = start; i <= end; i++) { rangeArray.push(i); } // Transform each element using map() return rangeArray.map(num => { const numStr = num.toString(); // Check if the number contains the digit '1' if (numStr.includes('1')) { return "Beep"; } // Return the original number if no match return num; }); } console.log(rangeOfNumbers(0, 12)); // Output: [0, "Beep", 2, 3, 4, 5, 6, 7, 8, 9, "Beep", "Beep", 12]
Step 2: Add the "Boop" Override Rule
To make numbers containing '2' replace with "Boop" (and override the "Beep" rule), we just need to adjust the order of our checks—always check for the higher-priority rule first:
function rangeOfNumbers(start, end) { let rangeArray = []; for (let i = start; i <= end; i++) { rangeArray.push(i); } return rangeArray.map(num => { const numStr = num.toString(); // Check for '2' first (higher priority) if (numStr.includes('2')) { return "Boop"; } // Then check for '1' else if (numStr.includes('1')) { return "Beep"; } return num; }); } console.log(rangeOfNumbers(0, 12)); // Output: [0, "Beep", "Boop", 3, 4, 5, 6, 7, 8, 9, "Beep", "Beep", "Boop"]
Bonus: Make Your Code More Modular
For better readability and future maintenance, you can extract the replacement logic into a separate function. This makes it easier to add more rules later (like a "Buzz" for numbers containing 3, for example):
function getReplacement(num) { const numStr = num.toString(); if (numStr.includes('2')) return "Boop"; if (numStr.includes('1')) return "Beep"; return num; } function rangeOfNumbers(start, end) { let rangeArray = []; for (let i = start; i <= end; i++) { rangeArray.push(i); } return rangeArray.map(getReplacement); }
This way, all your replacement rules live in one place, making it simple to update or extend them.
内容的提问来源于stack exchange,提问作者vitallyveg

