-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
136 lines (115 loc) · 3.66 KB
/
index.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* This is my code library. I have used the underscore/lowdash convention of using the underscore for naming ease. I am using the module pattern so any of these functions can be invoked using _.function_name();*/
var _ = (function(){
// this contains functions that will be exposed to the outer enviornment
var public_api = {
add: add,
adder:adder,
fizzBuzz : fizzBuzz,
fizzBuzzPlus:fizzBuzzPlus,
loopingTriangle : loopingTriangle,
loopingTrianglePlus: loopingTrianglePlus,
reverseTriangle : reverseTriangle,
reverseTrianglePlus : reverseTrianglePlus
}
//simple add function _.add(x , y)
function add(x, y){
return x + y;
}
//_.adder(x) function allow closure of the first paramater
function adder(x){
function add(y){
return x + y;
}
return add;
}
//_.fizzBizz
function fizzBuzz(){
var out = "";
var length = 100;
for (var i = 0; i < length; i++){
var j = i + 1;
out = "";
if(j % 3 === 0){
out = "fizz";
}
if(j % 5 === 0){
out = out + "Buzz";
}
console.log(out || j);
}
}
/*_.fizzBizzPlus optional parameters length = how many number you want to test, fizz and buzz are the numbers you want each to be divisible by.*/
function fizzBuzzPlus(length, fizz, buzz){
var out = "";
for (var i = 0; i < (length || 100); i++){
var j = i + 1;
out = "";
if(j % (fizz || 3) === 0){
out = "fizz";
}
if(j % (buzz || 5) === 0){
out = out + "Buzz";
}
console.log(out || j);
}
}
/* prints a triangle of #*/
function loopingTriangle(){
var hash = "#";
var out = "";
for (var i = 0; i < 6; i++){
out = out + hash;
console.log(out);
}
}
/* adds optional height and character parameters to control triangle height and the string that builds the triangle */
function loopingTrianglePlus(height , string){
var hash = (string || "#");
var out = "";
for (var i = 0; i < (height || 6); i++){
out = out + hash;
console.log(out);
}
}
/* like looping triangle but in reverse, with spaces on the left and hashes on the right*/
function reverseTriangle(){
var height = 6;
var hash ="#";
var blank = " ";
var hashArr = [];
var blankArr = [];
for(var i = 0; i < height; i++){
if (i < 1) {
hashArr.push(hash);
} else {
blankArr.push(blank);
}
}
for (var i = 0; i < height; i++){
console.log(blankArr.join("")+hashArr.join(""));
hashArr.push(hash);
blankArr.pop();
}
}
/* lets the user change the triangle height and character with high and string parameters*/
function reverseTrianglePlus(high, string){
var height = (high || 6);
var hash =(string || "#");
var blank = " ";
var hashArr = [];
var blankArr = [];
for(var i = 0; i < height; i++){
if (i < 1) {
hashArr.push(hash);
} else {
blankArr.push(blank);
}
}
for (var i = 0; i < height; i++){
console.log(blankArr.join("")+hashArr.join(""));
hashArr.push(hash);
blankArr.pop();
}
}
return public_api;
})();