-
Notifications
You must be signed in to change notification settings - Fork 0
/
TakeCTXDoWork.js
59 lines (55 loc) · 1.87 KB
/
TakeCTXDoWork.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* @param context {WebtaskContext}
*/
module.exports = function (ctx, done) {
if (ctx.data.number <= 30 && ctx.data.number >= 0)
if (ctx.data.algorithm == "fib")
done(null, 'The ' + ctx.data.number + ' nth fibbonacci number is ' + fib(ctx.data.number));
else if (ctx.data.algorithm == "fact")
done(null, 'The ' + ctx.data.number + ' nth factorial number is ' + fact(ctx.data.number));
else if (ctx.data.algorithm == "fibSeq")
done(null, 'The fibbonacci sequence for ' + ctx.data.number + " iterations is " + fibSeq(ctx.data.number));
else if (ctx.data.algorithm == "factSeq")
done(null, 'The factorial sequence for ' + ctx.data.number + " iterations is " + factSeq(ctx.data.number));
else
done(null, 'No Args Given, or no valid args given, ending');
else if (ctx.data.number > 30 || ctx.data.number < 0)
done(null, 'Please use a number >=0 and <= 30');
else
done(null, 'No Args Given, or no valid args given, ending');
}
function fib(ThisInt)
{
if (ThisInt == 0) return ThisInt;
else if (ThisInt == 1) return ThisInt;
else return fib( ThisInt - 1) + fib( ThisInt - 2 );
};
function fact(ThisInt)
{
if (ThisInt == 0 || ThisInt == 1) return ThisInt;
else return ThisInt * fact( ThisInt - 1);
};
function fibSeq(ThisInt)
{
var sequence = "";
for (var i = 0; i <= ThisInt; i++) {
var output = fib(i);
if (i == ThisInt)
sequence = sequence + output;
else
sequence = sequence + output + ",";
}
return sequence;
};
function factSeq(ThisInt)
{
var sequence = "";
for (var i = 0; i <= ThisInt; i++) {
var output = fact(i);
if (i == ThisInt)
sequence = sequence + output;
else
sequence = sequence + output + ",";
}
return sequence;
};