部分源码==》https://github.com/calamus0427/commonJS
翻转字符串算法挑战
function reverseString(str) { str = str.split("").reverse().join("") return str; } reverseString("hello");
阶乘算法挑战
function factorialize(num) { let sum = num ; if(num == 0){ return 1 ; }else{ while(num > 1 ){ sum *= (num-1) ; num -- ; } } return sum; } factorialize(0);
回文算法挑战
function palindrome(str) { str = str.replace(/[^a-zA-Z\d]/g, ""); return str.toLowerCase() == str .split("") .reverse() .join("") .toLowerCase(); } palindrome("0_0 (: /-\ :) 0-0");
寻找最长的单词算法挑战
function findLongestWord(str) { str = str.split(" "); let len = str[0].length ; for(let i = 0 ; i < str.length ; i++){ if(len <= str[i].length){ len = str[i].length ; } } return len; } findLongestWord("The quick brown fox jumped over the lazy dog");
设置首字母大写算法挑战
function titleCase(str) { str = str.split(" "); for(let i = 0 ; i < str.length ; i++){ str[i] =str[i].substring(0, 1).toUpperCase() + str[i].substring(1).toLowerCase(); } str = str.join(" "); return str; } titleCase("sHoRt AnD sToUt");
寻找数组中的最大值算法挑战
function largestOfFour(arr) { let newArr = [] ; for(let i = 0 ; i < arr.length ; i ++){ let max = arr[i][0]; for(let j = 0 ; j < arr[i].length ; j++){ if(max <= arr[i][j]){ max = arr[i][j]; } } newArr.push(max); } return newArr; } largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
确认末尾字符算法挑战
function confirmEnding(str, target) { // "Never give up and good luck will find you." // -- Falcor console.log(str,"|||",str.substring(str.length-target.length)) // return str.substring(str.length-target.length) == target; return str.endsWith(target); } confirmEnding("Bastian", "n");
重复操作算法挑战
function repeat(str, num) { // repeat after me if(num < 0){ return "" }else{ let newStr = "" ; for(let i = 0 ; i < num ; i++){ newStr += str ; } return newStr; } } repeat("abc", 3);
字符串截取算法挑战
function truncate(str, num) { // Clear out that junk in your trunk if(str.length <= num ){ return str; }else{ if(num <=3 ){ return str.substring(0,num)+"..."; } else{ return str.substring(0,num-3)+"..."; } } } truncate("Peter Piper picked a peck of pickled peppers", 14);
数组分割算法挑战
function chunk(arr, size) { // Break it up. var index = 0; var newArray = []; while(index < arr.length) { newArray.push(arr.slice(index, index += size)); } return newArray; } chunk(["a", "b", "c", "d"], 2);
数组截断算法挑战
function slasher(arr, howMany) { // it doesn't always pay to be first return arr.slice(howMany); } slasher([1, 2, 3], 2);
数组查询算法挑战
function mutation(arr) { let first = arr[0].toLowerCase(); let target = arr[1].toLowerCase().split("") ; for(let i = 0 ; i < target.length ; i++){ if(first.indexOf(target[i]) < 0 ){ return false ; } } return true ; } mutation(["zyxwvutsrqponmlkjihgfedcba", "qrstu"]);
删除数组中特定值算法挑战
function bouncer(arr) { // Don't show a false ID to this bouncer. return arr.filter(params => Boolean(params)); } function isBad(params){ var result = Boolean(params); return result; } bouncer([7, "ate", "", false, 9]);
去除数组中任意多个值算法挑战
function destroyer(arr) { var args = []; for(var i = 1; i < arguments.length; i++){ //将待摧毁的值放入一个数组中,赋值给变量args args.push(arguments[i]); } var newArr=arr.filter(function(item){ //两个数组去重; return args.indexOf(item) === -1; }); return newArr; } destroyer([1, 2, 3, 1, 2, 3], 2, 3);
数组排序并插入值算法挑战
function where(arr, num) { // Find my place in this sorted array. arr.push(num); arr = arr.sort(function(x,y){ //将num扔进arr中之后排序; return x-y; }); console.log(arr); return arr.indexOf(num); } where([3, 10, 5], 3);
位移密码算法挑战
function rot13(str) { // LBH QVQ VG! var newArr=[]; for(var i=0;i<str.length;i++){ var numbers=str.charCodeAt(i); //使用charCodeAt()方法取得每个字符的Unicode值,并保存在变量numbers中; if(numbers<65||numbers>90){ newArr.push(String.fromCharCode(numbers)); }else if(numbers>77){ newArr.push(String.fromCharCode(numbers-13)); }else{ newArr.push(String.fromCharCode(numbers+13)); } } //大写A-Z字母对应的Unicode值为65-90;通过判断,利用fromCharCode()将Unicode值又转换为字符; return newArr.join(""); } // Change the inputs below to test rot13("SERR PBQR PNZC");
本文首发于Calamus的博客。