String.js(前回の手直しを含む)
String.prototype.cut = function(n){
var result = new Array();
var len = this.length;
var num = (!n) ? 1 : n;
if(num<len) {
var i = 0;
while(i<len){
var str = this.substr(i,num);
result.push(str);
i += num;
}
return result;
} else {
return;
}
}
String.prototype.sort = function(){
return this.split('').sort().join('');
}
String.prototype.reverse = function(){
return this.split('').reverse().join('');
}
String.prototype.repeat = function (n,sep) {
var result = [];
var rn = (n)? n : 2;
for(var i=0;i<rn;i++){
result.push(this);
}
var s = (!sep) ? "" : sep;
return result.join(s);
}
String.prototype.tails = function () {
var result = new Array(this);
var strArry = this.split('');
var i = 0;
while(i<this.length) {
strArry.shift();
result.push(strArry.join(''));
i++;
}
return result;
}
実行例
js> browser = 'opera'
opera
js> browser.cut()
o,p,e,r,a
js> browser.cut(3)
ope,ra
js> browser.cut(6)
js> browser.sort()
aeopr
js> browser.reverse()
arepo
js> browser.repeat(5,":")
opera:opera:opera:opera:opera
js> browser.repeat()
operaopera
js> browser.repeat(3)
operaoperaopera
js> browser.tails()
opera,pera,era,ra,a,
Number.js
Number.prototype.abs = function() {
return (this < 0) ? -this : this;
}
Number.prototype.nList = function(){
var narry = [];
for(var i=0,len=this;i<len;i++) {
narry.push(i+1);
}
return narry;
}
Number.prototype.yakusu = function() {
var result = [];
var narry = this.nList();
for(var i=0,len=narry.length;i<len;i++) {
if(this % narry[i] == 0) {
result.push(narry[i]);
}
}
return result;
}
実行例
js> a=10
10
js> a.nList()
1,2,3,4,5,6,7,8,9,10
js> a.yakusu()
1,2,5,10
js> b=-10
-10
js> b.abs()
10