Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Coderbyte字母更改挑战-else语句错误_Javascript_String_If Statement_For Loop - Fatal编程技术网

Javascript Coderbyte字母更改挑战-else语句错误

Javascript Coderbyte字母更改挑战-else语句错误,javascript,string,if-statement,for-loop,Javascript,String,If Statement,For Loop,我在coderbyte中进行字母更改挑战,但我不明白为什么我的代码不起作用 让函数LetterChanges(str)取正在使用的str参数 通过并使用以下算法修改它 将字符串中的每个字母替换为 字母表(即c变成d,z变成a) 然后将这个新字符串中的每个元音大写(a、e、i、o、u)和 最后返回这个修改过的字符串 在最后一个else语句中,我想在“newWord”中添加任何不是字母的字符。当我传递“hello 3”这个词时,我应该得到“ifmmp 3”,而不是“Hhhhhhhhhhhhhhhhh

我在coderbyte中进行字母更改挑战,但我不明白为什么我的代码不起作用

让函数LetterChanges(str)取正在使用的str参数 通过并使用以下算法修改它

将字符串中的每个字母替换为 字母表(即c变成d,z变成a)

然后将这个新字符串中的每个元音大写(a、e、i、o、u)和 最后返回这个修改过的字符串

在最后一个else语句中,我想在“newWord”中添加任何不是字母的字符。当我传递“hello 3”这个词时,我应该得到“ifmmp 3”,而不是“Hhhhhhhhhhhhhhhhhhhheeeeeeeeeeeelllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllloooooooooooooooooooooo333333333333333333333333333333333333333333333未定义……”如果我去掉那句话,我就得到“ifmmp”没有3。我很难理解else语句为什么/如何把事情搞砸了,我该如何解决这个问题? 我是新的编码,所以任何帮助将是伟大的

    function LetterChanges(str) { 
var newWord = "";
 var alphabet = "abcdefghijklmnopqrstuvwxyz";
  for(var i = 0; i <= str.length; i++){
            if( str[i] === "z"){
        newWord += alphabet.charAt(0);
      } else if(str[i] === " "){
        newWord +=" ";
      } 

    for (var j = 0; j <= alphabet.length; j++){
        if(str[i] == alphabet.charAt(j)){
                newWord += alphabet.charAt(j+1);
            }
   // this one -------------------> else {
                                    newWord = newWord + str[i]; 
                                   } 
        }

      }
      return newWord;       
    }
function-LetterChanges(str){
var newWord=“”;
var alphabet=“abcdefghijklmnopqrstuvxyz”;
对于(var i=0;i尝试以下方法:

function LetterChanges(str) { 
    var newWord = "";
    var alphabet = "abcdefghijklmnopqrstuvwxyz";
    for(var i = 0; i < str.length; i++){

        for (var j = 0; j < alphabet.length; j++){

            if(str[i] === alphabet.charAt(j)){
                if( str[i] === "z"){
                    newWord += alphabet.charAt(0);
                } 
                else {
                    newWord += alphabet.charAt(j+1);
                }
            }
            else if (alphabet.indexOf(str[i]) === -1){
               newWord += str[i];
               break;
            }
        }
    }
    return newWord;       
}
function-LetterChanges(str){
var newWord=“”;
var alphabet=“abcdefghijklmnopqrstuvxyz”;
对于(变量i=0;i
以下是一个更有效的解决方案

function LetterChanges(str) { 
    var newWord = "";
    var alphabet = "abcdefghijklmnopqrstuvwxyz";
    for(var i = 0; i < str.length; i++){
        // if it's a 'z' do not loop through alphabet string and jump straight to the next character.
        if( str[i] === "z"){
            newWord += alphabet.charAt(0);
            continue;
        } 
        for (var j = 0; j < alphabet.length; j++){
            // if they match perform replacement 
            if(str[i] === alphabet.charAt(j)){
                newWord += alphabet.charAt(j+1);
                break;
            }
            // if the current char is not contained on the alphabet string (spaces, numbers and so on...) ---> copy it as it is
            else if (alphabet.indexOf(str[i]) === -1){
               newWord += str[i];
               break;
            }
            // if the current char is contained in the alphabet string but does not match ---> do nothing
        }
    }
    return newWord;
}
function-LetterChanges(str){
var newWord=“”;
var alphabet=“abcdefghijklmnopqrstuvxyz”;
对于(变量i=0;i则不执行任何操作
}
}
返回新词;
}
试试这个:

function LetterChanges(str) { 
    var newWord = "";
    var alphabet = "abcdefghijklmnopqrstuvwxyz";
    for(var i = 0; i < str.length; i++){

        for (var j = 0; j < alphabet.length; j++){

            if(str[i] === alphabet.charAt(j)){
                if( str[i] === "z"){
                    newWord += alphabet.charAt(0);
                } 
                else {
                    newWord += alphabet.charAt(j+1);
                }
            }
            else if (alphabet.indexOf(str[i]) === -1){
               newWord += str[i];
               break;
            }
        }
    }
    return newWord;       
}
function-LetterChanges(str){
var newWord=“”;
var alphabet=“abcdefghijklmnopqrstuvxyz”;
对于(变量i=0;i
以下是一个更有效的解决方案

function LetterChanges(str) { 
    var newWord = "";
    var alphabet = "abcdefghijklmnopqrstuvwxyz";
    for(var i = 0; i < str.length; i++){
        // if it's a 'z' do not loop through alphabet string and jump straight to the next character.
        if( str[i] === "z"){
            newWord += alphabet.charAt(0);
            continue;
        } 
        for (var j = 0; j < alphabet.length; j++){
            // if they match perform replacement 
            if(str[i] === alphabet.charAt(j)){
                newWord += alphabet.charAt(j+1);
                break;
            }
            // if the current char is not contained on the alphabet string (spaces, numbers and so on...) ---> copy it as it is
            else if (alphabet.indexOf(str[i]) === -1){
               newWord += str[i];
               break;
            }
            // if the current char is contained in the alphabet string but does not match ---> do nothing
        }
    }
    return newWord;
}
function-LetterChanges(str){
var newWord=“”;
var alphabet=“abcdefghijklmnopqrstuvxyz”;
对于(变量i=0;i则不执行任何操作
}
}
返回新词;
}

谢谢你,安吉洛,但它不起作用。如果我通过“你好”,我会得到“你好”。@mgee我“翻译了”来自Java,对我来说效果很好。我将尝试看看翻译中出现了什么错误,然后发回result@mgee我想我发现了我使用的elseif条件中的错误!==而不是===。我现在更正了它。让我知道它是如何进行的。无论如何,我的解决方案可以在效率方面得到改进。例如,您的重放方法将“z”放在内部for循环之外,这样更好sense@mgee如果这解决了你的问题,请随意回答。谢谢你,安吉洛,但它不起作用。如果我通过“你好”,我会得到“你好”。@mgee我“翻译”来自Java,对我来说效果很好。我将尝试看看翻译中出现了什么错误,然后发回result@mgee我想我发现了我使用的elseif条件中的错误!==而不是===。我现在更正了它。让我知道它是如何进行的。无论如何,我的解决方案可以在效率方面得到改进。例如,您的重放方法将“z”放在内部for循环之外,这样更好sense@mgee如果这解决了你的问题,请随意。我检查了它们,但无法确定问题。我应该检查哪些括号?请在devtools中一步一步地调试你的程序。我检查了它们,但无法确定问题。哪个括号