👨🏼‍💻

khriztianmoreno's Blog

Home Tags About |

Posts with tag spot-the-bug

Spot the Bug - N_02

2025-01-29
spot-the-bugchallenge

Bug hunters, assemble! We have a new challenge for you. This JavaScript code snippet is supposed to enrich a list of events with user metadata. But, is everything working as it should?const enrichEvents = (events) => { const metadata = { usr_789: { tier: "premium", joined: "2023-04-01" }, }; const enriched = events.map(event => { id: event.id, time: event.time, user: metadata[event.userId], value: event.properties.totalValue } ); return enriched; }; const events = [ { id: "evt_123", time: "2024-12-10T10:00:00Z", userId: "usr_789", properties: { totalValue: 99.99, }, }, ]; console.log(enrichEvents(events));Your mission:Find the bug: There's a small error preventing the code from working correctly. Can you spot it?Explain it: Once you've found the issue, explain why you think it's a bug and what the correct solution would be.Suggest improvements: If you see anything that could be optimized or improved in the code, feel free to share your suggestions!Accept the challenge and show off your debugging skills!<details> <summary>🚀 Solution 👩🏻‍💻🐛👨🏼‍💻</summary>Most likely your IDE would catch this one for you, but there is a syntax error. The map function is an arrow function that implicitly returns the result of the expression inside the block. In this case, the block is not a valid expression, so the code will throw a syntax error. To fix this, you can wrap the object in parentheses to make it an expression.const enrichEvents = (events) => { const metadata = { usr_789: { tier: "premium", joined: "2023-04-01" }, }; const enriched = events.map((event) => ({ id: event.id, time: event.time, user: metadata[event.userId], value: event.properties.totalValue, })); return enriched; }; const events = [ { id: "evt_123", time: "2024-12-10T10:00:00Z", userId: "usr_789", properties: { totalValue: 99.99, }, }, ]; console.log(enrichEvents(events));</details>Profile@khriztianmoreno �

Spot the Bug - N_01

2025-01-10
spot-the-bugchallenge

Do you consider yourself a code detective? ️‍👩🏻‍💻🐛👨🏼‍💻 We have a little mystery to solve. This JavaScript snippet seems to work perfectly, but hides a rather peculiar bug.Can you figure out what's going on and why the result is not as expected?Take the challenge and put your programming skills to the test!function reverseString(str) { return str.split("").reverse().join(""); } const reversedString = reverseString("Hello, 👋!"); console.log(reversedString);Remember, the satisfaction of solving a problem yourself is priceless. Try to research and solve it before you see the answer.<details> <summary>🚀 Solution 👩🏻‍💻🐛👨🏼‍💻</summary>This bug occurs because the split method treats the string as an array of 16-bit units, not as an array of characters resulting the unexpected output: !�� ,olleH.By using Array.from(str) or [...str] the string is split into an array of actual characters, respecting the surrogate pairs.Using Array.from:function reverseString(str) { return Array.from(str).reverse().join(""); } const reversedString = reverseString("Hello, 👋!"); console.log(reversedString);Using the spread operator:function reverseString(str) { return [...str].reverse().join(""); } const reversedString = reverseString("Hello, 👋!"); console.log(reversedString);</details>I hope this has been helpful and/or taught you something new!Profile@khriztianmoreno �