最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - CoderByte Letter Changes Java Script - Stack Overflow

programmeradmin3浏览0评论

The question is :

Using the JavaScript, have the function LetterChanges(str) take the str parameter being passed and modify it using the following algorithm. Replace every letter in the string with the letter following it in the alphabet (ie. c bees d, z bees a). Then capitalize every vowel in this new string (a, e, i, o, u) and finally return this modified string.

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();

The question is :

Using the JavaScript, have the function LetterChanges(str) take the str parameter being passed and modify it using the following algorithm. Replace every letter in the string with the letter following it in the alphabet (ie. c bees d, z bees a). Then capitalize every vowel in this new string (a, e, i, o, u) and finally return this modified string.

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();

basically, if return realanswer is placed before return result and LetterChanges is called with "o" i get the output undefined. But if it is called with a non vowel such as "b" it will output "c" which is correct.

now if i place return result before return realanswer it will work properly for vowels but not for other letters. thanks for the help

Share Improve this question edited Feb 27, 2019 at 13:11 user3307073 asked Feb 17, 2016 at 0:00 nkr15nkr15 1493 silver badges8 bronze badges 2
  • you've some problems with your code here but you might want to start with these two problems: 1. you need to use different variables for your nested for loops (don't use i for both for loops) 2. it doesn't make sense to have subsequent return statements as only the first return statement will execute – sfletche Commented Feb 17, 2016 at 0:12
  • The code design is flawed. You need to have two steps in code and not check for vowels in the input string as the first step. Also consider what to do with upper case letters in the input string. – traktor Commented Feb 17, 2016 at 0:22
Add a ment  | 

6 Answers 6

Reset to default 1
    function LetterChanges(str) { 

      return str
        .replace(/[a-zA-Z]/g, (x) =>  String.fromCharCode(x.charCodeAt(0)+1))
        .replace(/[aeiou]/g, (v) => v.toUpperCase());
    }
  1. The first part modifies the consonants by an increment of 1.

    • 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.

  2. The second part modifies the vowels to upper case.

    • 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 bee upper case.
 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 //
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();
  });
}
function LetterChanges(str) { 

  return str
    .split('')
    .map((c) => String.fromCharCode((c >= 'a' && c <= 'z') ? (c.charCodeAt(0)-97+1)%26+97 : (c >= 'A' && c <= 'Z') ? (c.charCodeAt(0)+1-65)%26+65 : c.charCodeAt(0)))
    .join('').replace(/[aeiou]/g, (letters) => letters.toUpperCase());

}
export const letterChange=(str)=>{

    let newStr = str.toLowerCase().replace(/[a-z]/gi, (char)=>{
        if(char==="z"){
            return "a"
        }else{
            return String.fromCharCode(char.charCodeAt()+1)
        }
    })

    let wordCap = newStr.replace(/a|e|i|o|u/gi, char => char.toUpperCase())

    return wordCap
}
 function changeLetters(str) {
  var result = "";
  for (var i = 0; i < str.length; i++) {
    var item = str[i];
    if (
      item == "a" ||
      item == "e" ||
      item == "i" ||
      item == "o" ||
      item == "u"
    ) {
      item = item.toUpperCase();
      result += item;
    } else if (item == "z") {
      letters = "a";
      result += item;
    } else {
      result += String.fromCharCode(str.charCodeAt(i) + 1);
    }
  }
  return result;
}
发布评论

评论列表(0)

  1. 暂无评论