Skip to content

Commit 4a199b1

Browse files
committed
Create 2485-find-the-pivot-integer.js
1 parent b0f3f52 commit 4a199b1

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Diff for: javascript/2485-find-the-pivot-integer.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
var pivotInteger = function (n) {
6+
7+
// initialize function sum with parameter start and end to find sum between start and end and return num
8+
function sum(start, end) {
9+
let num = 0;
10+
for (let i = start; i <= end; i++) {
11+
num += i;
12+
}
13+
return num;
14+
}
15+
16+
// loop through the 1 to n
17+
for (let i = 1; i <= n; i++) {
18+
19+
// if sum of 1 to i and sum of i to n is equal then return i
20+
if (sum(1, i) == sum(i, n)) {
21+
return i;
22+
}
23+
}
24+
25+
return -1; // otherwise return -1
26+
};

0 commit comments

Comments
 (0)