-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.lox
45 lines (39 loc) · 839 Bytes
/
example.lox
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
fun fizzBuzz(n) {
if (n == 0) return;
if (n % 3 == 0 and n % 5 == 0) {
print "FizzBuzz";
} else if (n % 3 == 0) {
print "Fizz";
} else if (n % 5 == 0) {
print "Buzz";
} else {
print n;
}
return fizzBuzz(n - 1);
}
class FizzBuzzer {
init(n) {
this.n = n;
}
doSth() {
for (var i = 1; i <= this.n; i = i + 1) {
var output = "";
if (this.isFizz(i)) output = "Fizz";
if (this.isBuzz(i)) output = output + "Buzz";
if (!output) output = output + i;
print output;
}
}
isFizz(n) {
return !(n % 3);
}
isBuzz(n) {
return !(n % 5);
}
}
var fizzBuzzer = FizzBuzzer(15);
print "Recursive:";
fizzBuzz(15);
print "";
print "Iterative:";
fizzBuzzer.doSth();