Skip to content

Commit 09e31a5

Browse files
authored
Merge pull request #328 from omprakash970/patch-1
Include code example and output explanation in README
2 parents 94276d5 + ab1f78d commit 09e31a5

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12485,6 +12485,41 @@ If a function is called with `undefined`, the `undefined` value is treated as a
1248512485
</p>
1248612486
</details>
1248712487
12488+
#### 87. What is the output of below code?
12489+
12490+
```javascript
12491+
const numbers = [1, 2, 3];
12492+
12493+
// Count how many numbers are odd
12494+
let xorAccumulator = numbers.reduce((sum, value) => {
12495+
return sum + (value % 2);
12496+
}, 0);
12497+
12498+
// IIFE applying XOR of each element shifted by its index
12499+
(function(arr) {
12500+
for (let index = 0; index < arr.length; index++) {
12501+
xorAccumulator ^= (arr[index] << index);
12502+
}
12503+
})(numbers);
12504+
12505+
console.log(xorAccumulator);
12506+
12507+
```
12508+
12509+
- 1: 5
12510+
- 2: 7
12511+
- 3: 11
12512+
- 4: 1
12513+
12514+
<details><summary><b>Answer</b></summary>
12515+
<p>
12516+
12517+
##### Answer: 3
12518+
12519+
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.
12520+
</p>
12521+
</details>
12522+
1248812523
**[⬆ Back to Top](#table-of-contents)**
1248912524
1249012525
## Disclaimer

0 commit comments

Comments
 (0)