Javascript 使用for循环检查用户是否输入一周或周末

Javascript 使用for循环检查用户是否输入一周或周末,javascript,for-loop,while-loop,Javascript,For Loop,While Loop,如果这个问题觉得多余,很抱歉,但是我还没有用这个方法找到答案。它不会在提供的链接上得到回答。 我一直在尝试制作一个Javascript程序来检查用户的输入是一周还是一周。它必须使用for或do while循环来完成 我想让我的程序检查用户输入是否在数组中,如果在数组中,程序应该能够判断这是工作日还是周末 我的问题是:它总是返回工作日,即使我把周六或周日 这被标记为Javascript,但适用于任何语言,因为它是基本的东西 这是到目前为止我的密码 var input = prompt("Enter

如果这个问题觉得多余,很抱歉,但是我还没有用这个方法找到答案。它不会在提供的链接上得到回答。 我一直在尝试制作一个Javascript程序来检查用户的输入是一周还是一周。它必须使用for或do while循环来完成

我想让我的程序检查用户输入是否在数组中,如果在数组中,程序应该能够判断这是工作日还是周末

我的问题是:它总是返回工作日,即使我把周六或周日

这被标记为Javascript,但适用于任何语言,因为它是基本的东西

这是到目前为止我的密码

var input = prompt("Enter a day of the week");
var day = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"];

for (var i = 0; i <= day.length; i++)
{
    if(input >= day[5]){
        console.log("It´s weekend!"); {break;}
    }else
    {
        console.log("It´s a working day");

    }
}
var输入=提示(“输入一周中的某一天”);
var日=[“周一”、“周二”、“周三”、“周四”、“周五”、“周六”、“周日”];
对于(var i=0;i=day[5]){
log(“这是周末!”){break;}
}否则
{
日志(“这是一个工作日”);
}
}

我认为您不必为循环使用
。以下代码应该可以工作:

var input = prompt("Enter a day of the week");
var workingday = ["monday", "tuesday", "Wensday", "thursday", "friday"];
var weekend = ["saturday", "sunday"]

if (workingday.indexOf(input) != -1) {
     console.log("It´s a working day!");
} else if (weekend.indexOf(input) != -1) {
     console.log("It´s weekend!");
} else {
     console.log("Invalid input!");
}
顺便说一下,你把“Wensday”拼错了。 要对
循环使用

for (var i = 0; i < day.length; i++)  {
    if (day[i] == input)  {
        console.log((i >= 5) ? "It's weekend!" : "It's a working day");
    }
}
for(变量i=0;i=5)?“这是周末!”:“这是工作日”);
}
}

我认为您不必为
循环使用
。以下代码应该可以工作:

var input = prompt("Enter a day of the week");
var workingday = ["monday", "tuesday", "Wensday", "thursday", "friday"];
var weekend = ["saturday", "sunday"]

if (workingday.indexOf(input) != -1) {
     console.log("It´s a working day!");
} else if (weekend.indexOf(input) != -1) {
     console.log("It´s weekend!");
} else {
     console.log("Invalid input!");
}
顺便说一下,你把“Wensday”拼错了。 要对
循环使用

for (var i = 0; i < day.length; i++)  {
    if (day[i] == input)  {
        console.log((i >= 5) ? "It's weekend!" : "It's a working day");
    }
}
for(变量i=0;i=5)?“这是周末!”:“这是工作日”);
}
}

您的输入是一个字符串,您的天数是一个数组,因此您不能在if语句中进行大于的比较

相反,您需要查找输入匹配的当天的索引(编号)。已经有一个名为查找数组中项的索引的数组方法。然后可以查看该天数是否大于或等于周末的天数

var dayNumber = days.indexOf(input);
if (dayNumber >= 5) {
  // it's the weekend
}
for (var dayNumber = 0; dayNumber < days.length; dayNumber++) {
  // check if the input matches the current index of the array
  if (input == days[dayNumber]) {
    // if it does, check if the day number is on the weekend
    if (dayNumber >= 5) {
      // it's the weekend
    }
  }
}
但是,如果必须使用for循环,则可以模拟
indexOf()
的功能。它通过在每个循环中增加一个数字
dayNumber
,然后使用该数字索引到
days
数组中来工作。然后,您可以将该索引处的值与
输入进行比较。如果它匹配,你知道它是什么天数,并且可以看到它是否大于或等于周末的天数

var dayNumber = days.indexOf(input);
if (dayNumber >= 5) {
  // it's the weekend
}
for (var dayNumber = 0; dayNumber < days.length; dayNumber++) {
  // check if the input matches the current index of the array
  if (input == days[dayNumber]) {
    // if it does, check if the day number is on the weekend
    if (dayNumber >= 5) {
      // it's the weekend
    }
  }
}

您的输入是一个字符串,天数是一个数组,因此您不能在if语句中进行大于的比较

相反,您需要查找输入匹配的当天的索引(编号)。已经有一个名为查找数组中项的索引的数组方法。然后可以查看该天数是否大于或等于周末的天数

var dayNumber = days.indexOf(input);
if (dayNumber >= 5) {
  // it's the weekend
}
for (var dayNumber = 0; dayNumber < days.length; dayNumber++) {
  // check if the input matches the current index of the array
  if (input == days[dayNumber]) {
    // if it does, check if the day number is on the weekend
    if (dayNumber >= 5) {
      // it's the weekend
    }
  }
}
但是,如果必须使用for循环,则可以模拟
indexOf()
的功能。它通过在每个循环中增加一个数字
dayNumber
,然后使用该数字索引到
days
数组中来工作。然后,您可以将该索引处的值与
输入进行比较。如果它匹配,你知道它是什么天数,并且可以看到它是否大于或等于周末的天数

var dayNumber = days.indexOf(input);
if (dayNumber >= 5) {
  // it's the weekend
}
for (var dayNumber = 0; dayNumber < days.length; dayNumber++) {
  // check if the input matches the current index of the array
  if (input == days[dayNumber]) {
    // if it does, check if the day number is on the weekend
    if (dayNumber >= 5) {
      // it's the weekend
    }
  }
}

您输入的内容可能重复?它是数字还是全文(如星期一)我的输入总是一天,如“星期六”“星期天”“星期一”等。它总是返回工作日,即使有数字。
console.log('it's'+(/(星期六/星期日)day/.test(prompt('Enter a day of the week')?“weekend!”:“a working day”)
可能重复您的输入?是数字还是全文(如星期一)我的输入总是一天,如“星期六”“星期天”“星期一”等。它总是返回工作日,即使有数字。
console.log('s'+(/(星期六/星期日)/.test(prompt('Enter a day of the week')?“weekend!”:“a working day”)