Skip to content

Commit af7ee9a

Browse files
authored
Include code example and output explanation in README
added question no. 87
1 parent 94276d5 commit af7ee9a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12485,6 +12485,35 @@ 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 t = [1, 2, 3];
12492+
let v = t.reduce((a, b) => a + (b % 2), 0);
12493+
12494+
(function(a) {
12495+
for (let i = 0; i < a.length; i++) {
12496+
v ^= (a[i] << i);
12497+
}
12498+
})(t);
12499+
12500+
console.log(v);
12501+
```
12502+
12503+
- 1: 5
12504+
- 2: 7
12505+
- 3: 11
12506+
- 4: 1
12507+
12508+
<details><summary><b>Answer</b></summary>
12509+
<p>
12510+
12511+
##### Answer: 3
12512+
12513+
The whole thing basically starts by counting how many odd numbers are in the array, which gives the value 2 to begin with. Then each element gets shifted left by its index, turning the numbers into slightly bigger weird versions of themselves, and each of those gets XOR-combined with the running value. By the time those three XOR hits finish, the value morphs into 11, which is exactly what the console prints.
12514+
</p>
12515+
</details>
12516+
1248812517
**[⬆ Back to Top](#table-of-contents)**
1248912518
1249012519
## Disclaimer

0 commit comments

Comments
 (0)