JavaScript #188690
-
BodyBody (Text): let arr = [1, 2, 3, 4];
let result = arr.map(num => {
if (num % 2 === 0) return num * 2;
});
console.log(result);Question: What do you think Why does it behave that way? How can we fix it so the result becomes something closer to That’s what makes JavaScript interesting — small code, big puzzles! 🧩 Guidelines
|
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 6 replies
-
|
Hello! This is a great puzzle to understand how map works under the hood in JavaScript. Here are the answers to your questions:
JavaScript In your code, the if (num % 2 === 0) condition only returns a value for even numbers. When the iteration hits an odd number (like 1 and 3), the condition is not met, and the function reaches the end without a return statement. In JavaScript, when a function doesn't explicitly return anything, it returns undefined by default.
Solution A: If you want to keep the original odd numbers ([1, 4, 3, 8]) JavaScript // A cleaner way using the ternary operator: JavaScript Happy coding! 🚀 |
Beta Was this translation helpful? Give feedback.
-
|
[undefined, 4, undefined, 8]Why:
Fix (keep odd numbers, double evens): let arr = [1, 2, 3, 4];
let result = arr.map(num => (num % 2 === 0 ? num * 2 : num));
console.log(result); // [1, 4, 3, 8]If you wanted only doubled evens ( |
Beta Was this translation helpful? Give feedback.
-
|
Output:
Fix — return the number as-is for odds: let result = arr.map(num => num % 2 === 0 ? num * 2 : num);
// [1, 4, 3, 8]Or if you only want the even doubles and want to discard odds, let result = arr.filter(num => num % 2 === 0).map(num => num * 2);
// [4, 8]The key takeaway: |
Beta Was this translation helpful? Give feedback.
-
Array.map() always returns a new array of the same length as the original array. For num = 1 → 1 % 2 === 0 is false → callback returns nothing → undefined For num = 2 → true → returns 4 For num = 3 → false → undefined For num = 4 → true → returns 8 So the output will be: [undefined, 4, undefined, 8] map does not skip elements; it always fills the new array with whatever the callback returns.
If the goal is to double even numbers but leave odd numbers unchanged, you can do: let arr = [1, 2, 3, 4]; Use a ternary operator (or an else clause) to ensure every element has a value in the new array.
If instead you only want the doubled numbers and skip the rest: let arr = [1, 2, 3, 4]; filter removes the odd numbers first. map then transforms only the even numbers. |
Beta Was this translation helpful? Give feedback.
-
|
[undefined, 4, undefined, 8] |
Beta Was this translation helpful? Give feedback.
-
|
The code outputs That's classic JS quirk: implicit return only if single expression, but with if/else blocks, you need explicit returns everywhere, or it falls through to undefined. To fix it for something closer to let arr = [1, 2, 3, 4];
let result = arr.map(num => {
if (num % 2 === 0) {
return num * 2;
} else {
return num;
}
});
console.log(result); // [1, 4, 3, 8] |
Beta Was this translation helpful? Give feedback.
-
|
Very useful Man |
Beta Was this translation helpful? Give feedback.
-
|
I like it |
Beta Was this translation helpful? Give feedback.
-
|
The thing is, .map() expects a return value for every single element in the array. Since your if block only covers even numbers, the function doesn't find a return for the odd ones (1 and 3), so it defaults to undefined. To fix this and keep the array complete, you just need to return the number when it's not even: |
Beta Was this translation helpful? Give feedback.
The code outputs
[undefined, 4, undefined, 8]because.map()expects a return value for every item. Whennumis odd (1 and 3),ifcondition is false, so no return happens — which defaults toundefinedin JS arrow functions without explicit return.That's classic JS quirk: implicit return only if single expression, but with if/else blocks, you need explicit returns everywhere, or it falls through to undefined.
To fix it for something closer to
[1, 4, 3, 8](keep odds same, double evens):