diff --git a/README.md b/README.md index 823af7e5..7bdd6eaf 100644 --- a/README.md +++ b/README.md @@ -12485,6 +12485,41 @@ If a function is called with `undefined`, the `undefined` value is treated as a
+#### 87. What is the output of below code? + +```javascript +const numbers = [1, 2, 3]; + +// Count how many numbers are odd +let xorAccumulator = numbers.reduce((sum, value) => { + return sum + (value % 2); +}, 0); + +// IIFE applying XOR of each element shifted by its index +(function(arr) { + for (let index = 0; index < arr.length; index++) { + xorAccumulator ^= (arr[index] << index); + } +})(numbers); + +console.log(xorAccumulator); + +``` + +- 1: 5 +- 2: 7 +- 3: 11 +- 4: 1 + ++ +##### Answer: 3 + +This question is really showcasing how JavaScript mixes array reduction with low-level bitwise tricks. The code first uses .reduce() to turn the array into a single value by counting how many elements are odd, then an IIFE immediately kicks in and loops through the array again, shifting each number left by its index and XOR-ing it into the accumulator. The whole vibe is about understanding how reduction works for summarizing arrays and how bit shifting plus XOR can transform values in a way that feels mathematical rather than typical JS. +
+