这个 Coder Byte 代码有什么问题(简单挑战 #2)?
What's wrong with this Coder Byte code (easy challenge #2)?
挑战内容:
使用 JavaScript 语言,让函数 LetterChanges(str) 获取传递的 str 参数并使用以下算法对其进行修改。用字母表中跟在它后面的字母替换字符串中的每个字母(即 c 变成 d,z 变成 a)。然后将这个新字符串中的每个元音 (a, e, i, o, u) 大写,最后 return 这个修改后的字符串。
我的解决方案:
function LetterChanges(str) {
var alphabet = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z];
var vowels = [a,e,i,o,u];
var stringToArray = str.split("");
for (i=0; i<=stringToArray.length; i++){ //goes through new array one-by-one
var originalLetter = stringToArray[i] // names each letter
var alphabetPosition = alphabet.indexOf(); // finds letter position in alphabet array
var newAlphabetPosition = alphabetPosition + 1; // adds one to that position
var newLetter = alphabet[newAlphabetPosition]; // changes the old letter to the new
newLetter = stringToArray[i]; // sends new letter to its position on new array
if (newLetter.isInArray(vowels){ // finds if new letter is a vowel
newLetter = newLetter.toUpperCase(); // if yes, turn into upper case
}
str = stringToArray.toStr() //turns array back to string, and names it str
}
return str;
}
我尽量把思路说清楚,一步一步加进去。请不要使用快速方法,因为我真的想正确理解挑战背后的原因,而不是死记硬背
代码无法运行...因为一些错误:
- 我是什么?是不是忘记申报了?
- 一旦将字符串转换为数组...第一个字符从 0 开始,最后一个字符的长度为 -1。你的 for 循环条件有 i <= stringToArray.length 这将包括一个超出数组长度的字符。
- var alphabetPosition = alphabet.indexOf();索引什么?你忘了 indexOf(originalLetter) 了吗?
- 你忘记了 "z"。您需要一些逻辑才能 "wrap-around" 回到 "a"。
- isInArray isNotAFunctionYouShowedUs。实际上 Javascript 只有 "indexOf" 其中 returns -1 不匹配...除非我弄错了。
function LetterChanges(str) {
for (var i = 0; i < str.length; i++) {
var newCharCode = str.charCodeAt(i) + 1; //Get Decimal Value for character at index "i", and increment by 1.
if (newCharCode == 123) { //Pesky 'z' becomes '{'. Lets change it to 'a' instead.
newCharCode = 97; // 'a' = 97.
}
if ([97,101,105,111,117].indexOf(newCharCode)!=-1) { //Is it a vowel? (97 = a, 101 = e, 105 = i, 111= o, 117 = u)
newCharCode-=32; //Make it capital. (65 = A, 69 = E, 73 = I, 79 = O, 85 = U)
}
console.log(newCharCode);
str = str.substr(0,i) + String.fromCharCode(newCharCode) + str.substr(i+1); //Convert it back to a character, and stick it back into the string.
console.log(str);
}
return str;
}
我标记的东西有点快...只是为了好玩。 (未经测试)它只使用了 2 个变量并做了一些老式的 ascii 欺骗。
我的 CoderByte 解决方案(https://github.com/ambatiramireddy/My-CoderByte-Solutions)
function LetterChanges(str) {
var vowels = 'aeiou', result = '', changedChar;
for (i = 0; i < str.length; i++) {
var c = str.charCodeAt(i);
if (c >= 97 && c < 122)
changedChar = String.fromCharCode(c + 1);
else if (c == 122)
changedChar = String.fromCharCode(97);
else
changedChar = str[i];
if (vowels.indexOf(changedChar) != -1)
changedChar = changedChar.toUpperCase();
result += changedChar;
}
return result;
}
挑战内容:
使用 JavaScript 语言,让函数 LetterChanges(str) 获取传递的 str 参数并使用以下算法对其进行修改。用字母表中跟在它后面的字母替换字符串中的每个字母(即 c 变成 d,z 变成 a)。然后将这个新字符串中的每个元音 (a, e, i, o, u) 大写,最后 return 这个修改后的字符串。
我的解决方案:
function LetterChanges(str) {
var alphabet = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z];
var vowels = [a,e,i,o,u];
var stringToArray = str.split("");
for (i=0; i<=stringToArray.length; i++){ //goes through new array one-by-one
var originalLetter = stringToArray[i] // names each letter
var alphabetPosition = alphabet.indexOf(); // finds letter position in alphabet array
var newAlphabetPosition = alphabetPosition + 1; // adds one to that position
var newLetter = alphabet[newAlphabetPosition]; // changes the old letter to the new
newLetter = stringToArray[i]; // sends new letter to its position on new array
if (newLetter.isInArray(vowels){ // finds if new letter is a vowel
newLetter = newLetter.toUpperCase(); // if yes, turn into upper case
}
str = stringToArray.toStr() //turns array back to string, and names it str
}
return str;
}
我尽量把思路说清楚,一步一步加进去。请不要使用快速方法,因为我真的想正确理解挑战背后的原因,而不是死记硬背
代码无法运行...因为一些错误:
- 我是什么?是不是忘记申报了?
- 一旦将字符串转换为数组...第一个字符从 0 开始,最后一个字符的长度为 -1。你的 for 循环条件有 i <= stringToArray.length 这将包括一个超出数组长度的字符。
- var alphabetPosition = alphabet.indexOf();索引什么?你忘了 indexOf(originalLetter) 了吗?
- 你忘记了 "z"。您需要一些逻辑才能 "wrap-around" 回到 "a"。
- isInArray isNotAFunctionYouShowedUs。实际上 Javascript 只有 "indexOf" 其中 returns -1 不匹配...除非我弄错了。
function LetterChanges(str) {
for (var i = 0; i < str.length; i++) {
var newCharCode = str.charCodeAt(i) + 1; //Get Decimal Value for character at index "i", and increment by 1.
if (newCharCode == 123) { //Pesky 'z' becomes '{'. Lets change it to 'a' instead.
newCharCode = 97; // 'a' = 97.
}
if ([97,101,105,111,117].indexOf(newCharCode)!=-1) { //Is it a vowel? (97 = a, 101 = e, 105 = i, 111= o, 117 = u)
newCharCode-=32; //Make it capital. (65 = A, 69 = E, 73 = I, 79 = O, 85 = U)
}
console.log(newCharCode);
str = str.substr(0,i) + String.fromCharCode(newCharCode) + str.substr(i+1); //Convert it back to a character, and stick it back into the string.
console.log(str);
}
return str;
}
我标记的东西有点快...只是为了好玩。 (未经测试)它只使用了 2 个变量并做了一些老式的 ascii 欺骗。
我的 CoderByte 解决方案(https://github.com/ambatiramireddy/My-CoderByte-Solutions)
function LetterChanges(str) {
var vowels = 'aeiou', result = '', changedChar;
for (i = 0; i < str.length; i++) {
var c = str.charCodeAt(i);
if (c >= 97 && c < 122)
changedChar = String.fromCharCode(c + 1);
else if (c == 122)
changedChar = String.fromCharCode(97);
else
changedChar = str[i];
if (vowels.indexOf(changedChar) != -1)
changedChar = changedChar.toUpperCase();
result += changedChar;
}
return result;
}