关于javascript:JS-教练我想做习题1

4次阅读

共计 3060 个字符,预计需要花费 8 分钟才能阅读完成。

???? 前言

大家好呀,我是毛小悠,能够叫我二毛,在家中排行老二,是一名前端开发工程师。

本系列文章旨在通过练习来进步 JavaScript 的能力,一起欢快的做题吧。????????????

以下每道题,二毛我都有尝试做一遍。倡议限时训练,比方限定为半小时,如果半小时内想不进去,能够联合文章开端的参考答案来思考。

能够在下方评论区留言或者加我的微信:code_maomao。期待你的到来。

求关注求点赞 ????~~~????????????

???? 题目 1:无效括号

编写一个函数,该函数带有一个括号字符串,并确定括号的程序是否无效。如果字符串无效,则该函数应返回 true;如果有效,则返回 false。

例子

"()" => true
")(()))" => false
"(" => false
"(())((()())())" => true

约束条件

0 <=input.length<= 100

习题代码

function validParentheses(parens){//TODO}

???? 题目 2:简略的猪拉丁

将每个单词的第一个字母移到单词的开端,而后在单词的开端增加“ay”。标点符号放弃不变。

例子

pigIt('Pig latin is cool'); // igPay atinlay siay oolcay
pigIt('Hello world !'); // elloHay orldway !

习题代码

function pigIt(str){//Code here}

答案

???? 题目 1 的答案

思路:其实就是判断通过正则来判断左右括号是否匹配。

参考答案 1:

function validParentheses(parens){
var n = 0;
for (var i = 0; i < parens.length; i++) {if (parens[i] == '(') n++;
if (parens[i] == ')') n--;
if (n < 0) return false;
}
return n == 0;
}

参考答案 2:

// I had something that was smaller and looked cooler, but
// this is how you'd want to write an actual parser.
function validParentheses(string){var tokenizer = /[()]/g, // ignores characters in between; parentheses are
count = 0, // pretty useless if they're not grouping *something*
token;
while(token = tokenizer.exec(string), token !== null){if(token == "(") {count++;} else if(token == ")") {
count--;
if(count < 0) {return false;}
}
}
return count == 0;
}

参考答案 3:

function validParentheses(parens){
var indent = 0;
for (var i = 0 ; i < parens.length && indent >= 0; i++) {indent += (parens[i] == '(') ? 1 : -1;
}
return (indent == 0);
}

参考答案 4:

function validParentheses(parens){var re = /()/;
while (re.test(parens)) parens = parens.replace(re, "");
return !parens;
}

参考答案 5:

function validParentheses(parens){var stack = [],
i;
for (i = 0; i < parens.length; i++) {if (parens[i] === '(') {stack.push(parens[i]);
}
else if('(' !== stack.pop()) {return false;}
}
return stack.length === 0;
}

???? 题目 2 的答案

参考答案 1:

function pigIt(str){return str.replace(/(w)(w*)(s|$)/g, "$2$1ay$3")
}

参考答案 2:

function pigIt(str) {return str.replace(/w+/g, (w) => {return w.slice(1) + w[0] + 'ay';
});
}

参考答案 3:

function pigIt(str) {var arrayWord = str.split(' ');
return arrayWord.map(function(word) {var firstLetter = word.charAt(0);
return word.slice(1) + firstLetter + 'ay';
}).join(' ');
}

参考答案 4:

function pigIt(str){
//Code here
return str.replace(/b(w)(w*)b/g,"$2$1ay");
}

参考答案 5:

/*
5kyu:Simple Pig Latin
Move the first letter of each word to the end of it, then add "ay" to the end of the word. Leave punctuation marks untouched.
Examples
pigIt('Pig latin is cool'); // igPay atinlay siay oolcay
pigIt('Hello world !'); // elloHay orldway
*/
function pigIt(str){
let newStr="";
const array=str.split(" ");
for (let i of array){
let firstLetter=""; //use to store the first letter in the word
for(let j in i){if(j==='0'){firstLetter+=i[j]; //store first letter
}
else{newStr+=i[j];
}
}
newStr+=firstLetter; //put it into end of the word
newStr+=" "; //space
}
newArray=newStr.split(" ");
newArray.pop();//delete the empty space
let result="" //store the result
for(let index of newArray){if (index.length!=1){
result+=index;
result+="ay";
result+=" ";
}
else if (index.length==1){if(index=="?" ||index=="!" ||index=="."){ //use to check if the last length is not equal symbol
result+=index;
}
else{
result+=index;
result+="ay";
result+=" ";
}
}
}
if(result[result.length-1] ===" "){ //delete last space
result=result.slice(0,result.length-1);
}
return(result);
}

???? 后序

本系列会定期更新的,题目会由浅到深的逐步提高。

求关注求点赞 ????~~????????????

能够关注我的公众号: 前端毛小悠 。欢送浏览

正文完
 0