简体   繁体   中英

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 . 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 ;" I want the "username" has the value even before defined the "sendUserName" function.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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