问题是:
使用JavaScript,让函数LetterChanges(str)接受传递的str参数,并使用以下算法修改它。将字符串中的每个字母替换为字母表中紧跟其后的字母(即C变成d,z变成a)。然后大写这个新字符串中的每个元音(a,e,i,o,u),最后返回这个修改后的字符串。
function LetterChanges(str){
var result = "";
for(var i = 0; i < str.length; i++) {
var letters = str[i];
if (letters == "a"|| letters == "e"|| letters == "i"|| letters == "o"|| letters =="u") {
letters = letters.toUpperCase();
result+=letters;
} else if (letters == "z") {
letters = "a";
} else {
var answer = "";
var realanswer="";
for (var i =0;i<str.length;i++) {
answer += (String.fromCharCode(str.charCodeAt(i)+1));
}
realanswer += answer
}
}
return realanswer;
return result;
}
LetterChanges();
基本上,如果返回realanswer放在返回结果之前,并且用"o“调用LetterChanges,我得到的输出是未定义的。但如果使用非元音调用,如"b“,它将输出"c”,这是正确的。
现在,如果我将return result放在return realanswer之前,它可以正确处理元音,但不能处理其他字母。谢谢你的帮助
发布于 2020-01-12 09:28:45
function LetterChanges(str) {
return str
.replace(/[a-zA-Z]/g, (x) => String.fromCharCode(x.charCodeAt(0)+1))
.replace(/[aeiou]/g, (v) => v.toUpperCase());
}- Regex is isolating the characters with `[]` versus no brackets at all. `g` ensures that the regex is applied anywhere in the string, as opposed to not putting `g` which gives you the first occurrence of the search.
- You have to convert the characters in the string to their Unicode because incrementing is a math operation. `x.charCodeAt(0)` is saying at the index of 0 of the string in the argument. The increment of 1 is not within the parentheses but outside.
- This is pretty straightforward, the regex only finds the individual characters because `[]` are used, `g` for anywhere in the string. and the modifier is to make the characters become upper case.
发布于 2016-07-29 19:15:32
function LetterChanges(str) {
var lstr = "";// Took a variable to store after changing alphabet//
for(var i=0;i<str.length;i++){
var asVal = (str.charCodeAt(i)+1);// To convert string to Ascii value and 1 to it//
lstr += (String.fromCharCode(asVal));// To convert back to string from Asii value//
}
console.log("Before converting vowels :"+lstr); //Printing in console changed alphabet//
var neword =""; // variable to store word after changing vowels to uppercase//
for(i=0;i<lstr.length;i++){
var strng = lstr[i]; // Storing every letter in strng variable while running loop //
if(strng=="a"||strng=="e"||strng=="i"||strng=="o"||strng=="u"){
neword += strng.toUpperCase(); // If it a vowel it gets uppercased and added //
}
else{
neword += strng; // If not vowel , it just gets added without Uppercase //
}
}
console.log("After converting vowels :"+neword); //Printing in console the word after captilising the vowels //
}
LetterChanges("Goutham"); // Calling a function with string Goutham //发布于 2019-03-30 07:50:19
function letterChanges(str) {
let res = '';
let arr = str.toLowerCase().split('');
// Iterate through loop
for(let i = 0; i < str.length; i++) {
// Convert String into ASCII value and add 1
let temp = str.charCodeAt(i) + 1;
// Convert ASCII value back into String to the result
res += (String.fromCharCode(temp));
}
console.log(res);
// Replace only vowel characters to Uppercase using callback in the replace function
return res.replace(/[aeiou]/g, (letters) {
return letters.toUpperCase();
});
}https://stackoverflow.com/questions/35445380
复制相似问题