We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents ee2ecd8 + 3fd28c3 commit dc0eb83Copy full SHA for dc0eb83
javascript/1486-XOR-operation-in-an-array.js
@@ -0,0 +1,14 @@
1
+/**
2
+ * @param {number} n
3
+ * @param {number} start
4
+ * @return {number}
5
+ */
6
+var xorOperation = function(n, start) {
7
+ let nums = new Array(n).fill(0); // initialize a nums which is the length of n elements with 0 value of Array using Array() nad fill()
8
+
9
+ for(let i=0; i<n; i++){ // loop through the 0 to n
10
+ nums[i] = start + (2 * i); // current element of nums is equal to sum of start and twice the i
11
+ }
12
13
+ return nums.reduce((a,b) => a^b); // return the bitwise XOR of all elements of nums using reduce()
14
+};
0 commit comments