初级脚本算法编程实战

1、翻转字符串算法挑战

function reverseString(str) {
var strarr = str.split("");
strarr = strarr.reverse();
str = strarr.join("");
return str;
}

reverseString("hello");

2、阶乘算法挑战

function factorialize(num) {
if(num <= 1) return 1;
    return num*factorialize(num-1);
}

factorialize(5);

3、回文算法挑战

function palindrome(str) {
 
  var  str1 = str.toLowerCase();  
  var reg = /[\W\_]/g;                     
  var str2 = str1.replace(reg, "");
  var str3 = str2.split("");  
  var str4 = str3.reverse(); 
  var str5 = str4.join("");  
 
  return str2 === str5;
}
palindrome("eye");

4、寻找最长的单词算法挑战

function findLongestWord(str) {
    var arr = str.split(" ");//空格为界限拆分字符串为数组["The","quick","brown","fox","jumped","over","the","lazy","dog"]
    arr.sort(function compare(x,y){ //用sort()方法对数组进行排序
        return y.length-x.length;//(按数组每项{即单词长度}降序排序)
    });
return arr[0].length;//取第一项为单词长度最长一项
}
 
findLongestWord("The quick brown fox jumped over the lazy dog");

5、设置首字母大写算法挑战

function titleCase(str) {
//把字符串所有的字母变为小写,并根据空格转换成字符数组
var arr = str.toLowerCase().split(" ");
//遍历字符数组
for(var i = 0;i < arr.length;i++){
    //把第一个字符变为大写
    arr[i] = arr[i][0].toUpperCase() + arr[i].substring(1,arr[i].length);
}
//加上空格,返回原模式的字符串
return arr.join(" ");

}


titleCase("I'm a little tea pot");

6、寻找数组中的最大值算法挑战

function largestOfFour(arr) {
var maxArr = [];
//遍历数组
for(var i = 0;i < arr.length;i++){
    for(var j = 0;j < arr[i].length;j++){
        //先把第一个数赋值给对应的最大数组maxArr中,用arr[i]中后面的数和它比较,若比它小则不变,否则替换。
        if(j == 0){
           maxArr[i] = arr[i][j]; 
        }else{
            maxArr[i] = arr[i][j] > maxArr[i]?arr[i][j]:maxArr[i]; 
        }
    }
}
return maxArr;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

7、确认末尾字符算法挑战

function confirmEnding(str, target) {

return str.endsWith(target);
}

confirmEnding("Bastian", "n");

8、重复操作算法挑战

function repeat(str, num) {
// repeat after me
if(num==0){
    return "";
}else{
        var string="";
    for (var i = 0; i 

9、字符串截取算法挑战

function truncate(str, num) {
// Clear out that junk in your trunk
 if(str.length <= num){
        return str; //给定参数大于字符串长度,返回字符串本身
    }else if(str.length > num && num <= 3){//三个点号不计入字符串长度
        return str.slice(0, num) + "...";
    }else if(str.length > num && num > 3){//三个点号计入字符串长度
        return str.slice(0, num - 3) + "...";//用slice()方法将后三个字符去掉
    }
     return str;   
    
}

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

10、数组分割算法挑战

function chunk(arr, size) {
  var rsArr = [];
  for(var i=0;i

11、数组截断算法挑战

function slasher(arr, howMany) {
var newArr = [];
if(arr.length - howMany > 0){
    for(var i = howMany;i < arr.length; i++){
        newArr.push(arr[i]);
    }
}
return newArr;

}

slasher([1, 2, 3], 2);

12、数组查询算法挑战

function mutation(arr) {
    str1 = arr[0].toLowerCase();
    str2 = arr[1].toLowerCase();
    for (var i = 0; i < str2.length; i++) {
        if (str1.indexOf(str2[i]) < 0) {
            return false;
        }
    }
    return true;
}
mutation(["hello", "hey"]);

13、删除数组中特定值算法挑战

function bouncer(arr) {
  var newArr = [];
  for(var i = 0; i < arr.length; i++){
      if(!(!arr[i] || arr[i] === "")){
      newArr.push(arr[i]);
      }
    }
    return newArr;

}

bouncer([7, "ate", "", false, 9]);

14、去除数组中任意多个值算法挑战

function destroyer(arr) {
    var argArr = arguments;
    for (var i = 1; i < argArr.length; i++) {
        arr = arr.filter(function(val) {
        return argArr[i] !== val;
        });
    }

    return arr;
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);

15、数组排序并插入值算法挑战

function where(arr, num) {
    //把参数放进数组
    arr.push(num);
    //对数组进行升序排序
    arr.sort(function(x,y){
        return x - y;
    });
    //返回参数在数组中的位置
    return arr.indexOf(num);
} 

where([40, 60], 50);

16、位移密码算法挑战

function rot13(str) { // LBH QVQ VG!
var n=0;
var num=0;
var arr=new Array();
for (var i = 0; i < str.length; i++) {
    n=str.charCodeAt(i);
    if(n>=65&&n<=90){
     if(n>=78&&n<=90){
           num=n-13;
            arr[i]=String.fromCharCode(num);
        }else{
          num=(90-(13-n+65)+1);
           arr[i]=String.fromCharCode(num);
        }
        
    }else{
        arr[i]=String.fromCharCode(n);
    }
}
var string=arr.join("");
return string;
 
}
// Change the inputs below to test
rot13("SERR PBQR PNZC");

 

你可能感兴趣的:(前端,js)