-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpascalTriangle.js
60 lines (49 loc) · 2.58 KB
/
pascalTriangle.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
Function.prototype.method = function (name, func) { // here we're augmenting Function.prototype so we make a method available to all functions
this.prototype[name] = func; // here we're assigning a function available to all the objects represented by "this", because we're augmenting it to its prototype
return this;
};
Number.method('length', function ( ) { // adding length method to number prototype
return this.toString().length;
});
var pascalTriangle = function(){
var arr = [[1], [1, 1]]; // default value (main array)
return{
calPasTri: function (level){
var n = level - arr.length; // level - the default value
for (var i = 0; i <= n ; i++){
var temp_arr= [];// Building row by row
temp_arr[0] = 1; // adding 1 at the beginning of the array
for (var j = 0, k = 1; j < arr[arr.length -1].length - 1; j++, k++){
temp_arr[k] = arr[arr.length -1][j] + arr[arr.length -1][j+1];
}
temp_arr.push(1);// adding 1 at the end of the array
arr.push(temp_arr);// pushing the temp array to a new row on the main array
}
},
getArr: function(){
return arr;
},
getStringTree: function(){
var str = "";
var white_space = " ";
for (var i = 0; i < arr.length; i++){
for (var j = 0; j < ((arr.length - arr[i].length) / 2) * (white_space.length + 1); j++){
str += " ";
}
for (var j = 0; j < arr[i].length; j++){
if ( j == 0 && i > 3){
str += arr[i][0] + white_space;
}else{
str += arr[i][j] + (arr[i][j].length() > 1 ? white_space : white_space + " "); // if number == 1 digit, then give extra space. because 2 and more digits are taking more space than 1 digit
}
}
str +="\n\n";
}
return str;
}
}
};
var pasTriObj = pascalTriangle();
pasTriObj.calPasTri(8);
console.log("The result array: ", pasTriObj.getArr());
console.log("The result string tree: \n", pasTriObj.getStringTree());