[toc]數組
let x = 5;
let y = 7;
[y,x] = [x,y];
console.log(x,y);
複製代碼
String.prototype.repeatify = function(n){
return (new Array(n+1).join(this));
}
console.log('Hello'.repeatify(3)); // HelloHelloHello
複製代碼
String.prototype.computer = function(str){
let index = []
let wordLen = str.length
for(let i = 0; i < (this.length - wordLen); i++){
let sonStr = this.slice(i,i+wordLen);
if(sonStr == str){
index.push(i)
}
}
console.log(index)
console.log('"'+str+'"共出現了'+index.length+'次')
}
let str = '這是一段尋找屢次出現的詞彙的文本,根據輸入的詞彙,搜索該詞彙出現次數,並使該詞彙高亮';
str.computer('詞彙'); // [11, 22, 28, 38] "詞彙"共出現了4次
str.computer('出現'); // [8, 30] "出現"共出現了2次
str.computer('該詞彙'); // [27, 37] "該詞彙"共出現了2次
複製代碼
String.prototype.appearMax = function(obj){
let name = ''
let repeat = 0
for(let o in obj){
if(obj[o] > obj['max']){
obj['max'] = obj[o]
name = o //
repeat = obj[o]
}
}
return '出現次數最多的字符是' + name + ',共出現' + repeat + '次'
}
String.prototype.everyNumber = function(){
let obj = {
max: 0
}
let strArr = this.split("")
for(let i = 0; i < strArr.length; i++){
if(obj.hasOwnProperty(strArr[i])){
obj[strArr[i]]++
}else{
obj[strArr[i]] = 1
}
}
console.log(obj)
return this.appearMax(obj)
}
console.log('aabbfffffaaffddff'.everyNumber()) // { a: 4, b: 2, f: 9, d: 2 } 出現次數最多的字符是f,共9次
複製代碼
String.prototype.tuoFeng = function(){
let arr = this.split('-');
let sFo = arr[0];
for(let i =1; i < arr.length; i++){
let i_0 = arr[i].charAt(0).toLocaleUpperCase();
let l = arr[i].length;
let i_1 = arr[i].slice(1,l);
sFo += (i_0 + i_1);
}
console.log(sFo);
}
'get-user-by-name'.tuoFeng()
複製代碼
此爲5.1和5.2共用類:數據結構
class latinPin{
constructor() {
this.yuan = ['a','e','i','o','u']
}
test(str){ // 拉丁豬文字遊戲
if(this.yuan.indexOf(str[0]) < 0){ // 若是第一個就是輔音的話
let first = str[0];
let last = str.slice(1,str.length)
return last+'-'+first+'ay'
}
let arr = str.split('');
for(let i = 1; i< arr.length; i++){ // 以元音字母開頭的話
if(this.yuan.indexOf(str[i]) < 0){
let toEnd = str[i];
let frist = str.slice(0,i);
let last = str.slice(i+1,str.length)
return frist+last+'-'+toEnd+'ay'
}
}
}
computer(str){ // 統計字符串中每一個元音字母的個數
let arr = str.split('');
let obj ={}
for(let i = 0; i < arr.length; i++){
if(this.yuan.indexOf(arr[i]) >= 0){
if(obj.hasOwnProperty(arr[i])){
obj[arr[i]]++
}else{
obj[arr[i]] = 1
}
}
}
console.table(obj)
}
}
複製代碼
let ltp = new latinPin();
ltp.test('banana') // anana-bay
ltp.test('orange') // oange-ray
ltp.test('apple') // aple-pay
ltp.test('watermelon') // atermelon-way
複製代碼
ltp.computer('banana') // { a: 3 }
ltp.computer('orange') // { o: 1,a: 1,e: 1}
ltp.computer('apple') // { a: 1,e: 1 }
ltp.computer('watermelon') // {a: 1,e: 2,o: 1}
複製代碼
let arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
複製代碼
現需將其每隔5位截取爲一組,每一組的前兩位和後兩位對調位置:app
Array.prototype.edcba = function(){
let n = -5,m = 0
let loop = this.length / 5
let result = []
for(let i = 0; i<loop; i++){
let son = this.slice(n+=5,m+=5).reverse()
result = result.concat(son)
}
return result
}
console.log(arr.edcba()) // [5, 4, 3, 2, 1, 10, 9, 8, 7, 6, 15, 14, 13, 12, 11]
複製代碼
Array.prototype.decab = function(abc){
let a = abc.slice(0,2)
let b = abc[2]
let c = abc.slice(3,5)
return c.concat(b,a)
}
Array.prototype.arrSplit = function(){
let n = -5,m = 0
let loop = this.length / 5
let result = []
for(let i = 0; i < loop; i++){
let son = this.slice(n+=5,m+=5)
result = result.concat(this.decab(son))
}
return result
}
console.log(arr.arrSplit()) // [4, 5, 3, 1, 2, 9, 10, 8, 6, 7, 14, 15, 13, 11, 12]
複製代碼
[點擊這裏看-JS遞歸實現DOM樹狀結構](https://juejin.im/post/5da67313f265da5bab5bd257).
< !-- 待更新 --> 複製代碼