JavaScript初学者求助:带奇偶标识的数字生成函数逻辑与代码调试
Hey there! Let's work through getting your even/odd generator working correctly. It looks like you're stuck on two main hurdles: handling the input format (like -e or -o at the end) and getting the core even/odd logic right. Let's break this down step by step.
Step 1: First, Parse the Input Correctly
Your input is a string like "8-e" or "6-o"—so first we need to split this into two parts: the maximum number we're checking, and whether we need even (e) or odd (o) numbers.
We can use JavaScript's split('-') method to split the string at the dash. This gives us an array with two elements: the number part (as a string) and the type. We'll convert the number part to an actual number using Number().
Example:
const input = "8-e"; const [maxNumStr, type] = input.split('-'); // maxNumStr is "8", type is "e" const maxNum = Number(maxNumStr); // converts "8" to 8
Step 2: Build the Logic to Collect Even/Odd Numbers
Next, we need to loop from 1 up to the maximum number, and collect only the numbers that match our type (even or odd):
- For even numbers: check if
i % 2 === 0(when divided by 2, there's no remainder) - For odd numbers: check if
i % 2 !== 0(there is a remainder when divided by 2)
Once we collect all the matching numbers, we join them with | to get the final output string.
Full Working Code
Here's the complete function with comments explaining each step:
const evenOdd = (inputStr) => { // Split the input string into max number and type (e/o) const [maxNumStr, type] = inputStr.split('-'); const maxNum = Number(maxNumStr); // Empty array to store our result const result = []; // Loop from 1 to maxNum (inclusive) for (let i = 1; i <= maxNum; i++) { // Check if we need even numbers and current i is even if (type === 'e' && i % 2 === 0) { result.push(i); } // Check if we need odd numbers and current i is odd else if (type === 'o' && i % 2 !== 0) { result.push(i); } } // Join the array elements with | to get the final output return result.join('|'); }; // Test the examples console.log(evenOdd("8-e")); // Output: 2|4|6|8 console.log(evenOdd("6-o")); // Output: 1|3|5 console.log(evenOdd("1-o")); // Output: 1
What Was Wrong With Your Original Code?
Let's go over the key issues in your initial code to help you understand:
- Input Handling: Your function only accepts a number, but the actual input is a string with a
-e/-osuffix. You weren't parsing this suffix to know whether to collect evens or odds. - Even/Odd Check: Your condition
number%i==0checks ifiis a divisor ofnumber(like 2 is a divisor of 8), not ifiis even. That's not the right check for even numbers—you needi % 2 === 0instead. - Loop Scope Issue: The
if(number%i!=0)block is outside yourforloop. By the time this runs,iis equal tonumber + 1(since the loop ends wheni > number), so this condition doesn't do anything useful and will cause errors. - No Result Compilation: You were logging arrays inside the loop, but not compiling a final result to return as the formatted string with
|separators.
Hope this helps you understand the problem better and get your code working!
内容的提问来源于stack exchange,提问作者Sumit Joshi

