0
function LetterChanges(str) {
    var alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for (var i = 0; i < str.length; i++) {
        var index = alphabet.indexOf(str[i])
        if (/[a-zA-Z]/.test(str[i])) {
            str = str.replace(str[i], alphabet.charAt(index + 1));
        }
        if (/[aeiou]/.test(str[i])) {
            str = str.replace(str[i], alphabet.charAt(index + 26));
        }
    }
    return str;
}

当我调用时LetterChanges("hello"),它返回'Ifmmp'正确的,但是当"sent"被传递时它返回'ufOt'而不是'tfOu'. 这是为什么?

4

6 回答 6

1

str.replace()用替换替换字符串中第一次出现的匹配项。LetterChanges("sent")执行以下操作:

  • i= 0 : str.replace("s", "t"), 现在str= “帐篷”
  • i= 1 : str.replace("e", "f"), 现在str= "tfnt"
  • i= 2 : str.replace("n", "o"), 现在str= "tfot", 然后 str.replace("o", "O"), 现在str= "tfOt"
  • i= 3 : str.replace("t", "u"), 现在str= "ufOt"
  • return str
于 2013-06-30T07:35:50.127 回答
0

有几个问题。主要问题是您可能会无意中多次更改同一个字母。

s让我们看看in会发生什么sent。你先把它改成t. 但是,当涉及到更改最后一个字母时,也就是您再次t更改第一个字母,这次是从to 。tu

另一个较小的问题是信件的处理z

最后,您在第二个if中的索引减少了一个:d成为D而不是E

于 2013-06-30T07:33:39.127 回答
0

您可以使用String.replace来避免这种情况:

function LetterChanges(str) {
    return str.replace(/[a-zA-Z]/g, function(c){
        return String.fromCharCode(c.charCodeAt(0)+1);
    }).replace(/[aeiou]/g, function(c){
        return c.toUpperCase();
    });
}

但是还有一个bug:LetterChanges('Zebra')会返回'[fcsb'。我认为这不是你的意图。你将不得不处理转变。

于 2013-06-30T08:00:27.060 回答
0

试试这个:

function LetterChanges(str) {
    var alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
   var result = '';
  var temp;
    for (var i = 0; i < str.length; i++) {

        var index = alphabet.indexOf(str[i])

        if (/[a-zA-Z]/.test(str[i])) {
            //str = str.replace(str[i], alphabet.charAt(index + 1));
          temp= alphabet.charAt(index + 1);
          index = index+1;
        }
      else if(str[i] == ' '){
      temp = ' ';
      }
        if (/[aeiou]/.test(temp)) {
            temp = alphabet.charAt(index + 26);
        }

      result += temp;
    }
    return result;
}
于 2013-11-05T13:30:11.330 回答
0
var str = 'bcd12';

str = str.replace(/[a-z]/gi, function(char) { //call replace method
  char = String.fromCharCode(char.charCodeAt(0)+1);//increment ascii code of char variable by 1 .FromCharCode() method will convert Unicode values into character 
  if (char=='{' || char=='[') char = 'a'; //if char values goes to "[" or"{" on incrementing by one as "[ ascii value is 91 just after Z" and "{ ascii value is 123 just after "z" so assign "a" to char variable..  
  if (/[aeiuo]/.test(char)) char = char.toUpperCase();//convert vowels to uppercase
  return char;
});

console.log(str);
于 2015-06-12T10:41:11.727 回答
0

检查此代码示例。它没有错误。不是很直接,但就像一个魅力。干杯!

function LetterChanges(str) { 
  var temp = str;
  var tempArr = temp.split("");//Split the input to convert it to an Array
  tempArr.forEach(changeLetter);/*Not many use this but this is the referred way of using loops in javascript*/
  str = tempArr.join("");

  // code goes here  
  return str; 

}

function changeLetter(ele,index,arr) {
  var lowerLetters ="abcdefghijklmnopqrstuvwxyza";
  var upperLetters ="ABCDEFGHIJKLMNOPQRSTUVWXYZA";
  var lowLetterArr = lowerLetters.split("");
  var upLetterArr = upperLetters.split("");
  var i =0; 

  for(i;i<lowLetterArr.length;i++){
    if(arr[index] === lowLetterArr[i]){
      arr[index] = lowLetterArr[i+1];
      arr[index]=arr[index].replace(/[aeiou]/g,arr[index].toUpperCase());
      return false;
    }
    if(arr[index] === upLetterArr[i]){
      arr[index] = upLetterArr[i+1];
      arr[index]=arr[index].replace(/[aeiou]/g,arr[index].toUpperCase());
      return false;
    }
  }

}

// keep this function call here 
// to see how to enter arguments in JavaScript scroll down
LetterChanges(readline());           
于 2015-09-19T14:27:18.333 回答