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.
1 parent b0f3f52 commit 4a199b1Copy full SHA for 4a199b1
javascript/2485-find-the-pivot-integer.js
@@ -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