简体   繁体   English

尝试从 switch 语句中的函数返回一个值 -JS-

[英]Trying to return a value from a function in a switch statement -JS-

I'm trying to make switch statements that can answer some questions that I add it to it .我正在尝试制作可以回答我添加到其中的一些问题的 switch 语句。 I want the answers end with the user's name , so if you typed in the prompt "My name is Alex" it will save Alex in the "var username ;"我希望答案以用户名结尾,所以如果你输入提示“我的名字是亚历克斯”,它会将亚历克斯保存在“var username;”中。 I want the "username" has the value even before defined the "sendUserName" function.我希望“用户名”甚至在定义“sendUserName”函数之前就具有值。

<html>
    <body>
        <script>
            var ask = prompt("Ask me anything >>").toLowerCase();

            function write(x) {
                document.write(x)
            };
            //Capitalize the first letter func :
            function capitalize(string) {
                return string.charAt(0).toUpperCase() + string.slice(1);
            }
            //..............
            var question = ask.split(" ");
            var date = new Date;
            var userName;
            //...............................
            write(userName); // <------ here the issue , undefined !
            if (question[0] === "what") {
                switch (question[1]) {
                    case "time":
                        switch (question[2]) {
                            case "is":
                                switch (question[3]) {
                                    case "it":
                                        write(date);
                                        break;
                                    default:
                                };
                                break;
                            default:
                        };
                        break;
                    case "is":
                        switch (question[2]) {
                            case "your":
                                switch (question[3]) {
                                    case "name":
                                        write("Alex !");
                                        break;
                                    default:
                                };
                                break;
                            default:
                        };
                        break;
                    default:
                        write("unknown");
                };
            } else if (question[0] === "my") {
                switch (question[1]) {
                    case "name":
                        switch (question[2]) {
                            case "is":
                                userName = capitalize(question[3]);;
                                alert("Your name is saved, " + userName);

                                function sendUserName() {
                                    return userName;
                                }
                                break;
                            default:
                        };
                        break;
                    default:
                };
            };
            sendUserName();
            write(userName); // <------- it's work here
        </script>
    </body>
</html>

In your code, the first write(userName) is called before you'd go through the if-else statements and the switches.在您的代码中,第一个write(userName)在您执行 if-else 语句和开关之前被调用。 A workaround (which would also improve the structure in my opinion) is to define a new function processor(input) and put all the logic there, then call that function with user input as the parameter before calling write() .一种解决方法(在我看来这也会改进结构)是定义一个新的函数processor(input)并将所有逻辑放在那里,然后在调用write()之前使用用户输入作为参数调用该函数。 See code below:见下面的代码:

var ask = prompt("Ask me anything >>").toLowerCase();

function write(x) { document.write(x) };
//Capitalize the first letter func :
function capitalize(string) {
  return string.charAt(0).toUpperCase() + string.slice(1);
}
//..............

var input = ask.split(" ");
var date = new Date;
var userName;
//...............................
processor(question); // the processor function is called with the value of variable question

write(userName); // <------ Now it is defined even here

function processor(input) {
  if (input[0] === "what") {

  switch (input[1]) {
    case "time":
      switch (input[2]) {
        case "is":
          switch (input[3]) {
            case "it":
              write(date);
              break;
            default:
          };
          break;
        default:
      };
      break;
    case "is":
      switch (input[2]) {
        case "your":
          switch (input[3]) {
            case "name":
              write("Alex !");
              break;
            default:
          };
          break;
        default:

      };
      break;

    default:
      write("unknown");

  };

} else if (input[0] === "my") {

  switch (input[1]) {
    case "name":
      switch (input[2]) {
        case "is":

          userName = capitalize(input[3]);;
          alert("Your name is saved, " + userName);
          break;

        default:
      };
      break;



    default:
  };
};
}

function sendUserName() {
  return userName;
}

sendUserName();

I haven't touched your code much.我没有接触过你的代码。 I just dumped all your logic inside function processor(input) and also took function sendUserName() out of it, making it global.我只是将您所有的逻辑转储到函数processor(input) ,并将函数sendUserName()取出,使其成为全局的。 You could, of course, put it back if you want, but then be aware that if you don't reach the part of the logic where that function is defined, you might hit an error by calling the function.当然,如果您愿意,您可以将其放回原处,但请注意,如果您没有到达定义该函数的逻辑部分,则可能会通过调用该函数而遇到错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM