例1:翻转字符串
function reverseString(str) {
var array = [];
for(var i = 0;i
例2:阶乘
function factorialize(num) {
if(num <=1 ){
return 1;
}else{
return num * factorialize(num-1);
}
}
factorialize(5); //120
factorialize(20); //2432902008176640000
factorialize(0); //1
例3:回文
function palindrome(str) {
var a =str.replace(/[\s|\.|\,|\-|\/|\\|\_|\)|\(|\:]/g,'').toLowerCase();
var b = a.split('').reverse().join('');
if(b === a){
return true;
}else{
return false;
}
}
palindrome("not a palindrome"); //false
palindrome("0_0 (: /-\ :) 0-0"); //true
palindrome("1 eye for of 1 eye."); //false
palindrome("My age is 0, 0 si ega ym."); //true
palindrome("almostomla"); //false
palindrome("nope"); //false
palindrome("never odd or even"); //true
palindrome("A man, a plan, a canal. Panama");
palindrome("race car");
例4:找出最大的单词,并返回它的长度
function findLongestWord(str) {
var arrayStr = str.split(' ');
var array = [];
arrayStr.forEach(function(e){
array.push(e.length);
});
var reverseStr = array.sort(compare);
return reverseStr[reverseStr.length-1];
}
function compare(value1,value2){
if(value1 < value2){
return -1;
}else if(value1 > value2){
return 1;
}else{
return 0;
}
}
findLongestWord("The quick brown fox jumped over the lazy dog"); //6
例5:将首字母替换成大写
function titleCase(str) {
var tmp = [];
var array = [];
str.split(' ').forEach(function(e){
tmp.push(e.toLowerCase());
});
tmp.forEach(function(e){
array.push(e.replace(e.charAt(0),function(s){
return s.toUpperCase()
}));
});
return array.join(' ');
}
titleCase("I'm a little tea pot"); //I'm A Little Tea Pot
例6:找到每个数组中最大的一位数,并将它返回
function largestOfFour(arr) {
var tmp = [];
var array = [];
for(var i = 0; i < arr.length; i++){
tmp.push(arr[i].sort(compare));
}
tmp.forEach(function(e){
array.push(e[0]);
});
return array;
}
function compare(value1,value2){
if(value1 < value2){return 1}
else if(value1 > value2){return -1}
else{return 0}
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
例7:检查字符串结尾
function confirmEnding(str, target) {
var tmp = [];
tmp = str.split(' ')
if(tmp.length === 1){
tmp = str.split('')
}
var lastWord = tmp[tmp.length-1]
lastWord = lastWord.split('').reverse()
target = target.split('').reverse()
for(var j = 0; j < target.length;j++){
if(target[j] === lastWord[j]){}else{
return false
}
}
return true
}
confirmEnding("He has to give me a new name", "me") //true
confirmEnding("He has to give me a new name", "na") //false
例8:重复输出字符串
function repeat(str, num) {
if(num <= 0){
return ''
}else if(num === 1){
return str
}else{
return str + repeat(str,num-1)
}
}
repeat("abc", -2);`//''
repeat("abc", 3); //'abcabcabc'
例9:阶段字符串
function truncate(str, num) {
if(num <= 3){
return str.substring(0,num)+'...'
}else if(num >= str.length){
return str.substring(0,num)
}else{
return str.substring(0,num-3)+'...'
}
}
truncate("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length + 2) //A-tisket a-tasket A green and yellow basket
例10:猴子吃香蕉分割数组
function chunk(arr, size) {
var array = []
for(var i = 0; i < Math.ceil(arr.length / size);i++){
var start = i * size;
var end = start +size;
array.push(arr.slice(start,end))
}
return array
}
chunk(["a", "b", "c", "d"], 2); //[["a", "b"], ["c", "d"]]
例11:截断数组
function slasher(arr, howMany) {
return arr.splice(howMany)
}
slasher([1, 2, 3], 2); //[3]
例12:比较字符串,如果第一个字符串中包含了第二个字符串的所有字母,则返回true
,否则返回false
function mutation(arr) {
for(var i = 0; i < arr[1].length; i++){
if(arr[0].toLowerCase().indexOf(arr[1][i].toLowerCase()) === -1){
return false
}
}
return true
}
mutation(["hello", "hey"]); //true
mutation(["hello", "neo"]); //false
例13:删掉数组中的假值
function bouncer(arr) {
var array = []
for(var i = 0; i < arr.length; i++){
if(arr[i]){
array.push(arr[i])
}
}
return array
}
bouncer([7, "ate", "", false, 9]); //[7,"ate",9]
例14:摧毁数组
function destroyer(arr) {
return [].slice.call(arguments).reduce((current,next) =>{
return current.filter((e) => {
return e !== next
})
})
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3); //[1,1]
例15:数组排序并找出元素索引
function where(arr, num) {
arr.push(num)
arr.sort((value1,value2) => {
if(value1 < value2){
return -1
}else if(value1 > value2){
return 1
}else{
return 0
}
})
return arr.indexOf(num)
}
where([40, 60], 50); //1
例16:凯撒密码
方法1——用数组做
function rot13(str) {
str = str.toUpperCase()
var tmp = []
var array = []
var arrayEnd = []
for(var i = 0; i < str.length; i++){
tmp.push(str[i].charCodeAt())
}
tmp.forEach((e) => {
if(e < 65||e > 90){
array.push(e)
}else if(e <= 77){
array.push(e + 13)
}else if(e <= 90){
array.push(e - 13)
}
})
array.forEach((e) => {
arrayEnd.push(String.fromCharCode(e))
})
return arrayEnd.join('')
}
rot13("SERR PBQR PNZC"); //"FREE CODE CAMP"
方法2——字符串
function rot13(str) {
str = str.toUpperCase()
var tmp
var string = ''
tmp = str.split('') //split 拆分字符串变成数组,join 合并数组变成字符串
tmp.forEach((e) => {
if(e.charCodeAt() < 65||e.charCodeAt() > 90){
string += e
}else if(e.charCodeAt() <= 77){
string += String.fromCharCode(e.charCodeAt() + 13)
}else if(e.charCodeAt() <= 90){
string += String.fromCharCode(e.charCodeAt() - 13)
}
})
return string
}
rot13("SERR PBQR PNZC"); //"FREE CODE CAMP"