From af7ee9adcbea1774a30c3f31273ef9709c16f910 Mon Sep 17 00:00:00 2001 From: Bandi Om Prakash <77910227+omprakash970@users.noreply.github.com> Date: Sat, 8 Nov 2025 16:06:15 +0530 Subject: [PATCH 1/2] Include code example and output explanation in README added question no. 87 --- README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/README.md b/README.md index 823af7e5..4cba66e4 100644 --- a/README.md +++ b/README.md @@ -12485,6 +12485,35 @@ If a function is called with `undefined`, the `undefined` value is treated as a

+#### 87. What is the output of below code? + +```javascript +const t = [1, 2, 3]; +let v = t.reduce((a, b) => a + (b % 2), 0); + +(function(a) { + for (let i = 0; i < a.length; i++) { + v ^= (a[i] << i); + } +})(t); + +console.log(v); +``` + +- 1: 5 +- 2: 7 +- 3: 11 +- 4: 1 + +
Answer +

+ +##### Answer: 3 + +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. +

+
+ **[⬆ Back to Top](#table-of-contents)** ## Disclaimer From ab1f78d197ba820a5a5e1d2ea4ef4a4bb80e2c87 Mon Sep 17 00:00:00 2001 From: Bandi Om Prakash <77910227+omprakash970@users.noreply.github.com> Date: Sun, 9 Nov 2025 08:51:06 +0530 Subject: [PATCH 2/2] Revise JavaScript code explanation in README Updated the explanation of the JavaScript code example to clarify its functionality and the output and changed variable name to more recongnisable names --- README.md | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 4cba66e4..7bdd6eaf 100644 --- a/README.md +++ b/README.md @@ -12488,16 +12488,22 @@ If a function is called with `undefined`, the `undefined` value is treated as a #### 87. What is the output of below code? ```javascript -const t = [1, 2, 3]; -let v = t.reduce((a, b) => a + (b % 2), 0); +const numbers = [1, 2, 3]; -(function(a) { - for (let i = 0; i < a.length; i++) { - v ^= (a[i] << i); +// 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); } -})(t); +})(numbers); + +console.log(xorAccumulator); -console.log(v); ``` - 1: 5 @@ -12510,7 +12516,7 @@ console.log(v); ##### Answer: 3 -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. +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.