Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12485,6 +12485,41 @@ If a function is called with `undefined`, the `undefined` value is treated as a
</p>
</details>

#### 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

<details><summary><b>Answer</b></summary>
<p>

##### 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.
</p>
</details>

**[⬆ Back to Top](#table-of-contents)**

## Disclaimer
Expand Down