# js初级脚本算法

## 翻转字符串算法挑战

function reverseString(str){
str = str.split('').reverse().join('');
return str;
}
reverseString("hello");

## 阶乘算法

function factorialize(num){
var b = 1;
for( var i=1;i<=num;i++){
b = i * b;
}
return b;
}
facrorialize(5);

## 回文算法

function palindrome(str){
str = str.replace(/[\ |\~|\|\!|\@|\#|\\$|\%|\^|\&|\*|$$|$$|\-|\_|\+|\=|\||\\|$|$|\{|\}|\;|\:|\"|\'|\,|\<|\.|\>|\/|\?]/g,"").toLowerCase();
var len = Math.floor(str.length/2);
for(var i=0;i<len;i++){
if(str[i] != str[str.length-i-1]){
return false;
}
}
return true;
}
palindrome("race Car");

## 寻找最长的单词算法挑战

function findLongestWord(str){
var max = 0;
var len = str.length;
for(var i = 0; i < len; i++){
var length = str[i].length;
if( length > max){
max = str[i].length
}
}
return max;
}
findLongestWord("The quick brown fox jumped over the lazy dog");

## 设置首字母大写算法

function titleCase(str){
str = str.toLowerCase().split(' ');
var len = str.length;
for(var i = 0; i < len; i++){
str[i] = str[i].slice(0,1).toUpperCase() + str[i].slice(1);
};
str = str.join(' ')
return str;
}
titleCase("I'm a little tea pot");

## 寻找数组中的最大值算法

function largestOfFour(arr){
var len = arr.length;
var array = [];
for(var i = 0; i < len; i++){
length = arr[i].length;
var max = 0;
for(var j = 0; j < length; j++){
if(arr[i][j] > max){
max = arr[i][j];
}
}
array.push(max);
max = 0;
};
return array;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

## 确认末尾字符算法

function confirmEnding(str,target){
if(str.endWidth(target)){
return true;
}else{
return false;
}
}
confirmEnding("Bastian", "n");

## 重复操作算法

function repeat(str,num){
var string = '';
for(var i = 0; i <= num; i++){
string = string + str;
}
return string;
}
confirmEnding("abc", "5");

## 字符串截取算法

function truncate(str,num){
var len = str.length;
if( num <=3 ){
if(len > num){
str = str.substring(0,num)+'...';
}else{
str = str.substring(0,num);
}
}else if(num > 3){
if(length > num){
str = str.substring(0,num-3)+'...';
}else{
str = str.substring(0,num);
}
}
return str;
}
truncate("A-tisket a-tasket A green and yellow basket", 11);

## 数组分割算法

function chunk(arr,size){
var arr1 = [];
for(var i = 0; i < arr.length; i+=size){
var brr = arr.slice(i,i+size);
arr1.push(brr);
}
return arr11;
}
chunk(["a", "b", "c", "d"], 2);

chunk([1,2,3,4,5],2)=[[1,2],[3,4],[5]];

## 数组截断算法

function slasher(arr, howMany) {
var len = arr.length;
if( howMany > length ){
arr = [];
}else{
arr = arr.slice(howMany);
}
return arr;
}
slasher([1, 2, 3], 2);

## 数组查询算法

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

["hello", "hey"]应该返回false，因为字符串"hello"并不包含字符"y"。

## 删除数组中特定值算法

function bouncer(arr) {
var len = arr.length;
for(var i=len-1;i>=0;i--){
if( Boolean(arr[i]) == false ){
arr.splice(i,1);
}
}
return arr;
}
bouncer([7, "ate", "", false, 9]);

## 去除数组中任意多个值算法

function destroyer(arr) {
var len = arr.length;
var length = arguments.length;
for(var i = 1; i<length;i++){
for(var j=len-1;j>=0;j--){
if(arr[j] == arguments[i]){
arr.splice(j,1);
}
}
}
return arr;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);

## 数组排序并插入值算法

function where(arr, num) {
arr.sort();
var len = arr.length;
for(var i=0;i<len;i++){
if( arr[i] > num ){
arr.splice(i,0, num);
return arr;
}
}
arr.push(num);
return arr;
}
where([2,7,4,5], 50);

## 位移密码算法

function rot13(str) {
var arr = [];
var len = str.length;
str = str.toUpperCase();
for(var i=0;i<len;i++){
if( str[i].charCodeAt()>=65 && str[i].charCodeAt()<=77 ){
var num = str[i].charCodeAt()+13;
}else if( str[i].charCodeAt()>77 && str[i].charCodeAt()<=90 ){
var num = str[i].charCodeAt()-13;
}else{
num = str[i];
}
arr.push(String.fromCharCode(num));
}
str = arr.join('');
return str;
}
rot13("SERR PBQR PNZC");`

+ 订阅