Skip to content

Commit e20b24a

Browse files
authored
Create 1006-clumsy-factorial.js
1 parent 0fa7eee commit e20b24a

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

1006-clumsy-factorial.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @param {number} N
3+
* @return {number}
4+
*/
5+
const clumsy = function(N) {
6+
const ops = ["*", "/", "+", "-"];
7+
const arr = [];
8+
arr.push(N);
9+
for (let i = N - 1, idx = 0; i > 0; i--, idx++) {
10+
let op = ops[idx % 4];
11+
let arrIdx = arr.length - 1 < 0 ? 0 : arr.length - 1;
12+
switch (op) {
13+
case "*":
14+
arr[arrIdx] *= i;
15+
break;
16+
case "/":
17+
arr[arrIdx] = Math.floor(arr[arrIdx] / i);
18+
break;
19+
case "+":
20+
arr[0] += i;
21+
break;
22+
case "-":
23+
arr.push(i);
24+
break;
25+
}
26+
}
27+
28+
let res = arr[0];
29+
for (let i = 1; i < arr.length; i++) {
30+
res -= arr[i];
31+
}
32+
return res;
33+
};

0 commit comments

Comments
 (0)