基礎(chǔ)算法

// 反轉(zhuǎn)字符串
/*2016-6-18 By沐浴星光*/
function reverseString(str) {
 
return str.split("").reverse().join("");//將字符串轉(zhuǎn)化成數(shù)組,反轉(zhuǎn)后在轉(zhuǎn)化成字符串
}
reverseString("hello");
.................................................................................................
//數(shù)組分塊
/*2016-6-19 By沐浴星光*/
function chunkArrayInGroups(arr, size) {
  // Break it up.
  var newArr=[];
  var index=0;
  while(index<arr.length){
    newArr.push(arr.slice(index,index+=size));
  }
  return newArr;
}

console.log(chunkArrayInGroups(["a", "b", "c", "d"], 2));
..........................................................................................
//數(shù)組截頭存尾
/*2016-6-19 By沐浴星光*/
function slasher(arr, howMany) {
  arr.splice(0,howMany);
  return arr;
}
slasher([1, 2, 3], 2);
.........................................................................................
//轉(zhuǎn)變
/*2016-6-19 By沐浴星光*/
function mutation(arr) {
    var first=arr[0].toLowerCase();
    var second=arr[1].toLowerCase();
  for (var i = 0; i < second.length; i++) {
    if(first.indexOf(second[i])===-1)
        return false;
  }
  return true;
}

mutation(["hello", "hey"]);
...................................................................................
//移除數(shù)組中的假值
/*2016-6-19 By沐浴星光*/
/*
console.log(new Boolean(false)==false);
console.log(new Boolean(null)==false);
console.log(new Boolean(0)==false);
console.log(new Boolean("")==false);
console.log(new Boolean(undefined)==false);
console.log(new Boolean(NaN)==false);
//以上表達(dá)式都返回true
*/
function bouncer(arr) {
    var newArr=arr.filter(function(item,index,arr){
        return new Boolean(item)!=false;
    });
    
    return newArr;
}

bouncer([7, "ate", "", false, 9]);
.........................................................................................
//查找并刪除數(shù)組中特定值
/*2016-6-19 By沐浴星光*/
function destroyer(arr) {
    var arg=Array.prototype.slice.call(arguments,1);
    var newArr=arr.filter(function(item,index,arr){
        return arg.indexOf(item)==-1;
    });
  return newArr;
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);
.................................................................................
//定位數(shù)據(jù)插入的位置
/*2016-6-19 By沐浴星光*/
function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
  var index=0;
  arr.sort(function(value1,value2){
    return value1-value2;
  });
  console.log(arr);
  while(num>arr[index++]){
  }
  return index-1;
}

getIndexToIns([40, 60], 50);
...................................................................................
//凱撒密碼
/*2016-6-19 By沐浴星光*/
function rot13(str) { // LBH QVQ VG!
  var newStr;
  var charCodeArr=[];//存儲(chǔ)所有轉(zhuǎn)換后的字符編碼
  var charCode;
  for (var i = 0; i < str.length; i++) {
    charCode=str.charCodeAt(i);
    if(charCode>=65&&charCode<=90){
      charCodeArr.push((charCode-13)>=65?(charCode-13):(90-65-13+charCode+1));//90-[13-(charCode-65)]+1
    }else{
        charCodeArr.push(charCode);
    }
  }
  
  return String.fromCharCode.apply(null,charCodeArr);
}

// Change the inputs below to test
rot13("SERR PBQR PNZC");
.................................................................................
//數(shù)字階乘
/*2016-6-19 By沐浴星光*/
function factorialize(num) {
    if (num<=1) {
        return 1;//0的階乘為0,同時(shí)也是遞歸出口
    }
  return num*factorialize(num-1);//使用遞歸
}

factorialize(5);
............................................................................................
//回文字符串
/*2016-6-19 By沐浴星光*/
function palindrome(str) {
  
  var newStr=str.replace(/[^a-zA-Z0-9]/g,"").toLowerCase();//將非數(shù)字字母替換掉
  return newStr.split("").reverse().join("")===newStr;//比較反轉(zhuǎn)前后是否相等
}
palindrome("race car");
..............................................................................................
// 計(jì)算句子中最長(zhǎng)單詞的長(zhǎng)度
/*2016-6-19 By沐浴星光*/
Array.prototype.myMap = function (fn) {//ES5之前不支持?jǐn)?shù)組迭代方法,自定義一個(gè)迭代方法
  var ary = [];
  if (Array.prototype.map) {//如果有原生迭代map方法就直接調(diào)用
    ary = this.map(fn);
  } else {
    for (var i = 0; i < this.length; i++) {
      ary[i] = fn.apply(null, [this[i], i, this]);
    }
  }
  return ary;
}

function findLongestWord(str) {
  var arr=str.split(" ");//將句子分割成數(shù)組
  var lenArr=arr.myMap(function(item,index,array){//使用map方法返回?cái)?shù)組每項(xiàng)長(zhǎng)度
    return item.length;
  });
  return Math.max.apply(null,lenArr);//借用apply求最大值
}
findLongestWord("The quick brown fox jumped over the lazy dog");
...................................................................................................
//句子單詞首字母大寫(xiě)化
/*2016-6-18 By沐浴星光*/

Array.prototype.myMap = function (fn) {//ES5之前不支持?jǐn)?shù)組迭代方法,自定義一個(gè)迭代方法
  var ary = [];
  if (Array.prototype.map) {//如果有原生迭代map方法就直接調(diào)用
    ary = this.map(fn);
  } else {
    for (var i = 0; i < this.length; i++) {
      ary[i] = fn.apply(null, [this[i], i, this]);
    }
  }
  return ary;
}

function titleCase(str) {
    var newStr=str.toLowerCase();//將句子轉(zhuǎn)化成小寫(xiě)
    var wordArr=newStr.split(" ");//將句子分割成數(shù)組
    var newWorldArr=wordArr.map(function(item,index,arr){
        return item.charAt(0).toUpperCase()+item.substring(1);//新的數(shù)組項(xiàng)由每一項(xiàng)的第一個(gè)字母大寫(xiě)+后面的字符拼接而成
    });
return newWorldArr.join(" ");
}

titleCase("I'm a little tea pot");
.............................................................................................
//查找數(shù)組集合里每一個(gè)數(shù)組的最大值組成的數(shù)組
/*2016-6-19 By沐浴星光*/

Array.prototype.myMap = function (fn) {//ES5之前不支持?jǐn)?shù)組迭代方法,自定義一個(gè)迭代方法
  var ary = [];
  if (Array.prototype.map) {//如果有原生迭代map方法就直接調(diào)用
    ary = this.map(fn);
  } else {
    for (var i = 0; i < this.length; i++) {
      ary[i] = fn.apply(null, [this[i], i, this]);
    }
  }
  return ary;
}


function largestOfFour(arr) {
  var largestArr=arr.myMap(function(item,index,arr){
    return Math.max.apply(null,item);
  });
  return largestArr;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
....................................................................................................................
//確認(rèn)字符串以特定子串結(jié)尾
/*2016-6-19By沐浴星光*/
function confirmEnding(str, target) {
  var targetLength=target.length;
  var strLength=str.length;
  var substr=str.substr(strLength-targetLength,targetLength);
  return substr===target;
}

confirmEnding("Bastian", "n");
..............................................................................................
//重復(fù)字符串
/*2016-6-19 By沐浴星光*/
function repeatStringNumTimes(str, num) {
    var newStr="";

    for (var i = num; i >0; i--) {
        newStr+=str;
    }
    return newStr;
    
}

console.log(repeatStringNumTimes("abc", 3));
...................................................................................
//截?cái)嘧址?/*2016-6-19 By沐浴星光*/

function truncateString(str, num) {

    if(str.length<=num){
        return str;
    }
    else{
        return (num<=3)?(str.slice(0,num)+"..."):(str.slice(0,num-3)+"...");
    }

}
truncateString("A-tisket a-tasket A green and yellow basket", 11);


整理自https://github.com/FeMiner/FreeCodeCamp
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容