提示用户输入然后使用开关检查输入

时间:2018-04-22 15:20:23

标签: javascript arrays loops switch-statement prompt

我被困在JavaScript实验室的一部分,我需要:

  • 提示用户输入
  • 然后使用switch语句根据输入的内容显示警告
  • 然后检查其文字而不是数字

我的老师说要把它放在一个循环中,到目前为止我已经使用了所有这些问题,但我只是让自己更加困惑。

这是我的代码:

<script>
        //array to compare input to
        var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

        do
        {
            //prompt user for input
            var response = prompt("Choose a day of the week: ");

                //loop through all array elements
                for(i in days)
                {
                        //choices that I will use to compare input to
                        switch(response)
                            {
                                case days[0]:
                                case days[1]:
                                case days[2]:
                                case days[3]:
                                case days[4]:
                                case days[5]:
                                case days[6]:
                                    alert("You have chosen day: " + days[i]);
                                    break;
                                default:
                                    alert("That is not a choice!");
                            }
                    }
            }//condition while input doesn't equal exactly array element prompt user again
        while(response !=== days[i]);
    </script> 

4 个答案:

答案 0 :(得分:0)

var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

    do
    {
        //prompt user for input
        var response = prompt("Choose a day of the week: ");

            //loop through all array elements

                    //choices that I will use to compare input to
                    switch(response)
                        {
                            case days[0]:
                            case days[1]:
                            case days[2]:
                            case days[3]:
                            case days[4]:
                            case days[5]:
                            case days[6]:
                                alert("You have chosen day: " + days[days.indexOf(response)]);
                                break;
                            default:
                                alert("That is not a choice!");
                        }

        }//condition while input doesn't equal exactly array element prompt user again
    while(response !== days[i]);

有两处更正

  1. 一个不需要for循环,使用
  2.   

    的indexOf

    以天为单位获取输入日索引的方法

    1. 第二个是关系运算符的错误使用!===应该是!==。

答案 1 :(得分:0)

您最好删除for-in循环。现在你有2个循环do-while,只需要一个循环。将数组与响应进行比较的更好方法是使用indexOf并在匹配时中断循环。

&#13;
&#13;
//array to compare input to
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

do {
  //prompt user for input
  var response = prompt("Choose a day of the week: ");

  //loop through all array elements
  //choices that I will use to compare input to
  switch (response) {
    case days[0]:
    case days[1]:
    case days[2]:
    case days[3]:
    case days[4]:
    case days[5]:
    case days[6]:
      alert("You have chosen day: " + response);
      break;
    default:
      alert("That is not a choice!");
  }
} //condition while input doesn't equal exactly array element prompt user again
while (days.indexOf(response) === -1);
&#13;
&#13;
&#13;

这是一个品味问题,但您也可以通过删除switch语句并在while循环中运行它来清理它:

&#13;
&#13;
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

while(true) {
  var response = prompt("Choose a day of the week: ");
  
  var found = days.find(function(day) {
    return response === day;
  });

  if (found) {
    alert("You have chosen day: " + response);
    
    break;
  }

  alert("That is not a choice!"); 
}
&#13;
&#13;
&#13;

答案 2 :(得分:0)

@alejandro-martin的好玩法....

&#13;
&#13;
import * as functions from 'firebase-functions';
import * as express from 'express';

const app = express();

app.get("/:function", (req, res) => {
    switch(req.params.function) {

        case 
            "helloWorld" : helloWorld(res)
            break;

        default : res.send("Invalid function requested.")

    }
});

app.get("/users/:id", (req, res) => {
    res.send("User with ID: " + req.params.id)
});

function helloWorld(res) {
    res.send("Hello world!");
}

exports.api = functions.https.onRequest(app);
&#13;
&#13;
&#13;

答案 3 :(得分:0)

var daysList = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
isResponseValid = false;

do {
  var response = prompt('Choose a day of the week');
  if ( /^[0-9]*$/.test(response) ) {
    alert('You have key in a number, please key in a text');
  }
  else if( daysList.indexOf(response) >= 0){
    alert('You have chosen ' + response);
    isResponseValid = true;
  } else {
    alert('That is not a choice');
  }
} while (isResponseValid == false);

if语句的第一行是测试文本。在验证响应不是数组的一部分后,测试数字没有意义。

流程应为:

  1. 测试号码,如果是号码,则提醒用户他们必须键入文本。

  2. 如果是文本,请测试文本是否在数组中。

  3. 如果测试失败,请提醒用户输入不是一个选择。
  4. 这里没有使用开关盒,因为它对我来说似乎没什么意义。但上面的答案提供了如何在代码中使用它们的示例。

    添加了一个额外的var来测试响应是否有效,但我想使用响应没有额外的好处!== days [i]。 :)

相关问题