首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CoderByte Letter更改Java脚本

CoderByte Letter更改Java脚本
EN

Stack Overflow用户
提问于 2016-02-17 08:00:01
回答 5查看 2.4K关注 0票数 1

问题是:

使用JavaScript,让函数LetterChanges(str)接受传递的str参数,并使用以下算法修改它。将字符串中的每个字母替换为字母表中紧跟其后的字母(即C变成d,z变成a)。然后大写这个新字符串中的每个元音(a,e,i,o,u),最后返回这个修改后的字符串。

代码语言:javascript
复制
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之前,它可以正确处理元音,但不能处理其他字母。谢谢你的帮助

EN

回答 5

Stack Overflow用户

发布于 2020-01-12 09:28:45

代码语言:javascript
复制
    function LetterChanges(str) { 

      return str
        .replace(/[a-zA-Z]/g, (x) =>  String.fromCharCode(x.charCodeAt(0)+1))
        .replace(/[aeiou]/g, (v) => v.toUpperCase());
    }

  1. 第一部分以1为增量修饰辅音。

代码语言:javascript
复制
- 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.

  1. 第二部分将元音改为大写。

代码语言:javascript
复制
- 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.

票数 1
EN

Stack Overflow用户

发布于 2016-07-29 19:15:32

代码语言:javascript
复制
 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 //
票数 0
EN

Stack Overflow用户

发布于 2019-03-30 07:50:19

代码语言:javascript
复制
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();
  });
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35445380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档