freeCodeCamp初级算法答案(个人做法)
2016-04-19 本文已影响812人
MakingChoice
1、回文
function Stack(){
this.dataStore=[];//数组实现的栈
this.top=0;
this.push=push;
this.pop=pop;
this.peek=peek;
this.clear=clear;
this.length=length;
}
function push(element){
this.dataStore[this.top++]=element;
}
function peek(){
return this.dataStore[this.top-1];
}
function pop(){
return this.dataStore[--this.top];
}
function clear(){
this.top=0;
}
function length(){
return this.top;
}
function palindrome(str) {
// Good luck!
var word1="";
var word2="";
var stack=new Stack();
var word=str.toLowerCase();
word=word.replace(/\s+/g,"");
word=word.replace(/[,]/g,"");
word=word.replace(/[.]/g,"");
word=word.replace(/[_]/g,"");
word=word.replace(/[(]/g,"");
word=word.replace(/[-]/g,"");
word=word.replace(/[)]/g,"");
word=word.replace(/[/]/g,"");
word=word.replace(/[\\]/g,"");
for(var i=0;i<word.length;i++){
stack.push(word[i]);
}
while(stack.length()>0){
word1+=stack.pop()
}
console.log(word);
console.log(word1)
if(word1===word){
return true
}else{
return false
}
}
console.log(palindrome("0_0 (: /-\ :) 0-0"))
2、找到最长的字符串长度/Find the Longest Word in a String
function findLongestWord(str) {
var arryA=str.split(" ");
var len=0;
for(var i=0;i<arryA.length;i++){
if(arryA[i].length>len){
len=arryA[i].length;
}
}
return len;
}
findLongestWord("The quick brown fox jumped over the lazy dog");
3、把一句话的首字母大写/Title Case a Sentence
function titleCase(str) {
var word=str.toLowerCase();
var arryA=word.split(" ");
for(var i=0;i<arryA.length;i++){
arryA[i]=arryA[i].replace(arryA[i].charAt(0),arryA[i].charAt(0).toUpperCase());
}
return arryA.join(" ");
}
console.log(titleCase("I'm a little tea pot"));
4、查找数组的最大值/Return Largest Numbers in Arrays
function largestOfFour(arr) {
// You can do this!
var arryA=[];
for(var i=0;i<arr.length;i++){
var num=0;
for(var j=0;j<arr[i].length;j++){
if(arr[i][j]>num){
num=arr[i][j];
}
}
arryA.push(num);
}
return arryA;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
5、验证某个字符串是否已某个字符结尾
function confirmEnding(str, target) {
// "Never give up and good luck will find you."
// -- Falcor
var len1=target.length;
var len2=str.length;
if(str.substr(len2-len1,len1)===target){
return true;
}else{
return false;
}
}
confirmEnding("Bastian", "n");
6、重复字符串
function repeatStringNumTimes(str, num) {
// repeat after me
var word=str;
var string="";
for(var i=0;i<num;i++){
string+=word;
}
return string;
}
repeatStringNumTimes("abc", 3);
ES6中有String.protype.repeat()并且有支持各个浏览器的方法。
7、剪切字符串,truncateString(str,num)
function truncateString(str, num) {
// Clear out that junk in your trunk
var word="";
var word1="";
if(str.length>num&&num>3){
word1=str.slice(0,num-3);
word=word1+"...";
}else if(num<=3){
word=str.slice(0,num)+"...";
}else if(str.length<=num){
word=str;
}
return word;
}
truncateString("A-tisket a-tasket A green and yellow basket", 11);
8、按照特定规则剪切数组
chunkArrayInGroups(arr, size) 安装size的大小来剪切arr
function chunkArrayInGroups(arr, size) {
// Break it up.
var arrA=arr;
var arrB=[];
var lastArr;
var num=arrA.length%size;
if(num===0){
for(var i=0;i<(arrA.length-num)/size;i++){
arrB.push(arrA.slice(i*size,(i+1)*size));
}
return arrB;
}else{
for(var j=0;j<(arrA.length-num)/size;j++){
arrB.push(arrA.slice(j*size,(j+1)*size));
}
lastArr=arrA.slice(arrA.length-num,arrA.length);
arrB.push(lastArr);
return arrB;
}
}
chunkArrayInGroups(["a", "b", "c", "d"], 2);
9、从特定位置剪切数组,返回剪切位置以后的数组/Slasher Flick
function slasher(arr, howMany) {
// it doesn't always pay to be first
var len=arr.length;
var arrA=arr;
var arrB=[];
if(len>howMany){
arrB=arrA.slice(howMany,len);
}else{
return arrB;
}
return arrB;
}
slasher([1, 2, 3], 2);
10、判断一个字符串是否有另一个字符串的全部字母
function mutation(arr) {
var word1=arr[0].toLowerCase();
var word2=arr[1].toLowerCase();
var message=true;
for(var i=0;i<word2.length;i++){
if(word1.indexOf(word2[i])<0){
message=false;
}
}
return message;
}
mutation(["hello", "hey"]);
11、通过Array.prototype.filter()来判断数组中是否有 Falsy values( false, null, 0, "",undefined, and NaN.)注意部分浏览器不支持
function bouncer(arr) {
var word;
var isFalse= function (num) {
if(new Boolean(num).toString()){
return num;
};
};
word=arr.filter(isFalse);
return word;
}
bouncer([7, "ate", "", false, 9]);
12、在指定数组中去除指定元素 Array.filter()
function destroyer(arr) {
var elemToDestroy = [];
for(var i = 1; i < arguments.length; i++){
elemToDestroy.push(arguments[i]);
}
var survived = arguments[0].filter(function(element, index){
var toReturn = true;
for(var i = 0; i < elemToDestroy.length; i++){
if (element === elemToDestroy[i]){
toReturn = false;
}
}
return toReturn;
});
return survived;
}
console.log(destroyer([1, 2, 3, 1, 2, 3], 2, 3))
13、查找某一个数组中元素的位置
function getIndexToIns(arr, num) {
// Find my place in this sorted array.
var arrA=arr.sort(function(a,b){
return a - b;
});
console.log(arrA)
var index=0;
for(var i=0;i<arrA.length;i++){
if(arrA[i]<num){
index=i+1;
}else if(arrA[i]===num){
index=i;
}
}
return index;
}
getIndexToIns([40, 60], 50);
14、凯撒密码ROT-13
function rot13(str) { // LBH QVQ VG!
return (str + '').replace(/[a-z]/gi, function (s) {
return String.fromCharCode(s.charCodeAt(0) + (s.toLowerCase() < 'n' ? 13 : -13))
})
}
// Change the inputs below to test
console.log(rot13("SERR PBQR PNZC"));