function RotateNumber (argument) {
this.print(300)
}
RotateNumber.prototype = {
state: 'push',
print : function (n){
var arr1 = this.handleArr([], 1,n);
var arr = this.handleArr(arr1, n + 1, this.findMinLenCount(arr1) + n , " ");
var str = '';
for (var i = 0; i < arr.length; i++){
str += arr[i].toString() + '\n';
}
console.log(str);
},
fillNum : function(len,num){
var arr = num.toString().split('')
for(var i = 0;i< len - num.toString().length; i++){
arr.unshift(' ');
}
return arr.join('');
},
findMinLenIndex : function(arr, preMinLenIndex){
var minLen = arr[0].length;
var minIndex = 0;
var isAllLenEqual = this.isAllLenEqual(arr);
var isAsc = this.isArrLenAsc(arr);
if(isAllLenEqual){
return preMinLenIndex;
}else{
for(var i = 0; i < arr.length; i++){
if(isAsc){
if(minLen >= arr[i].length){
minLen = arr[i].length;
minIndex = i;
}
}else{
if(minLen > arr[i].length){
minLen = arr[i].length;
minIndex = i;
}
}
}
return minIndex;
}
},
* arr [] 已有数组
* s 数组开始数字
* n 数组结束数字
* fill 填充内容,默认为数字
*/
handleArr : function (arr, s, n, fill){
arr = arr || [];
var tempArr;
var minLenIndex = 0;
var numLen = n.toString().length;
for(var i = s; i <= n; i++){
tempArr = [];
if(arr.length <= 0){
tempArr.push(this.fillNum(numLen,i));
arr.push(tempArr);
} else {
minLenIndex = this.findMinLenIndex(arr, minLenIndex);
if(arr[minLenIndex].length > arr.length){
tempArr.push(fill || this.fillNum(numLen,i));
arr[this.state](tempArr);
this.state = this.state == 'push' ? 'unshift' : 'push';
}else{
arr[minLenIndex][this.state](fill || this.fillNum(numLen,i));
}
}
}
return arr;
},
findMinLenCount : function (arr){
var isAllLenEqual = this.isAllLenEqual(arr);
var minLen = this.getMinLen(arr);
var count = 0;
if(isAllLenEqual){
return count;
} else {
for (var i = 0; i < arr.length; i++){
if(minLen == arr[i].length){
count++;
}
}
return count;
}
},
isAllLenEqual : function(arr){
return arr[0].length == arr[arr.length - 1].length;
},
getMinLen: function(arr){
return Math.min(arr[0].length, arr[arr.length - 1].length);
},
isArrLenAsc: function(arr){
return arr[0].length < arr[arr.length - 1].length;
}
};
new RotateNumber()