简体   繁体   English

是否存在诸如if不匹配或else循环之类的问题。 我需要帮助使此JavaScript脚本正常工作

[英]Is there something like an if mismatch or an else loop. I need help making this JavaScript Script work

How would i get a response according to a mismatch, or possibly like an Else loop but this doesn't appear to work since i get it even if i answer the question correctly, and if maybe i could do something like and if mismatch or something. 我如何根据不匹配获得响应,或者可能像其他循环一样,但这似乎不起作用,因为即使我正确回答了问题,我仍然可以得到响应,并且也许我可以做类似和不匹配之类的操作。 I am new to JavaScript, so i cannot solve this myself. 我是JavaScript新手,所以我自己无法解决这个问题。

var PromptAnswer3 = prompt("Well, what sports do you like to play?", "");
    while (PromptAnswer3 === null | PromptAnswer3 === "") 
    {
    alert("You didn't answer");
    PromptAnswer3 = prompt("Well, what sports do you like to play?", "");
    }
if (PromptAnswer3.match(/^(tennis|bowling|baseball|soccer|basketball|football|cricket|hockey|ice|hockey|rugby|track|golf|horseracing|water polo|swimming|biking|gymnastics|cheer-leading|skiing|snowboarding|iceskating|lacrosse|surfing|archery|boxing|kayaking|diving|spelunking|fencing|karate|judo|rockclimbing|softball|volleyball|wrestling|badminton|javelin throwing|synchronized swimming|yoga|ringette)!?$/i)) 
{
alert("Haha yeah, " + PromptAnswer3 "is pretty awesome.");
}
  while (PromptAnswer3)
{
  alert("I don't think that is a sport.");
  PromptAnswer3 = prompt("Well, what sports do you like to play?", "");
}

Edit [should possibly be added as an answer] 编辑[应该添加为答案]

This is what i did to make it work. 这就是我所做的,以使其正常工作。 It is simple and easy. 这很简单。 I followed some of the peoples advice on the answers and put the if statement in the loop, while (PromptAnswer3), and put an else and a break in the if so that it wouldn't loop after the first time. 我遵循了人们的一些建议,将if语句放入了while(PromptAnswer3)循环中,并在else中插入了else和break,以便第一次后不会循环。

else {
    var PromptAnswer3 = prompt("Well, what sports do you like to play?", "");
    while (PromptAnswer3 === null | PromptAnswer3 === "") 
    {
        alert("You didn't answer");
        PromptAnswer3 = prompt("Well, what sports do you like to play?", "");
    }
    while (PromptAnswer3)
{
if (PromptAnswer3.match(/^(tennis|bowling|baseball|soccer|basketball|football|cricket|hockey|ice|hockey|rugby|track|golf|horseracing|water polo|swimming|biking|gymnastics|cheer-leading|skiing|snowboarding|iceskating|lacrosse|surfing|archery|boxing|kayaking|diving|spelunking|fencing|karate|judo|rockclimbing|softball|volleyball|wrestling|badminton|javelin throwing|synchronized swimming|yoga|ringette)!?$/i)) 
{
    alert("Haha yeah, " + PromptAnswer3 + "is pretty awesome.");
    break;
    }
    else {
      alert("I don't think that is a sport.");
      PromptAnswer3 = prompt("Well, what sports do you like to play?", "");
    }
    }
}

First off: 首先:

alert("Haha yeah, " + PromptAnswer3 "is pretty awesome.");

should have an additional plus, no? 应该有一个额外的好处,不是吗?

alert("Haha yeah, " + PromptAnswer3 + "is pretty awesome.");

That jumps out at me. 那让我惊讶。

Also, your second while(PromptAnswer3) should be an else. 另外,第二个while(PromptAnswer3)应该是else。 So maybe something like this: 所以也许是这样的:

var PromptAnswer3 = prompt("Well, what sports do you like to play?", "");
while (PromptAnswer3 === null | PromptAnswer3 === "") {
    alert("You didn't answer");
    PromptAnswer3 = prompt("Well, what sports do you like to play?", "");
    if (PromptAnswer3.match(/^(tennis|bowling|baseball|...)!?$/i)) {
      alert("Haha yeah, " + PromptAnswer3 "is pretty awesome.");
    } else {
      alert("I don't think that is a sport.");
      PromptAnswer3 = prompt("Well, what sports do you like to play?", "");
    }
}

I can't tell when you want to exit, but this looks like it never exits. 我无法告诉您何时要退出,但这似乎永远都不会退出。 Maybe that is your intent. 也许那是您的意图。 Those are things that look odd to me, but I did not test it. 这些对我来说看起来很奇怪,但是我没有对其进行测试。

I think you just need to add an else to your if 我认为您只需要在if添加一个else

if ([comparison is true]) {
    // you have a match 
} else {
    // you do not
}
var PromptAnswer3;
while( !(PromptAnswer3 = prompt("Well, what sports do you like to play?", ""))) {
    // your regex may no work properly
    if(!/^(rugby|track|...)$/i.test(PromptAnswer3)) {
        alert("Haha yeah, " + PromptAnswer3 + "is pretty awesome.");
    }else {
        alert("I don't think that is a sport.");
        PromptAnswer3 = null;
    }
}
  • Your indentation style is hard to read, IMO IMO,您的缩进样式很难看懂
  • Did you really want to use a bitwise or in your first if statement? 您是否真的要在第一个if语句中使用按位运算 ( | ) |
  • use test to test if a regular expression matches a string. 使用test测试正则表达式是否与字符串匹配。 Don't use match for this. 请勿为此使用match
  • Just invert the logic in your while statements. 只需反转while语句中的逻辑即可。

     var PromptAnswer3 = prompt("Well, what sports do you like to play?", ""); while (!PromptAnswer3) { alert("You didn't answer"); PromptAnswer3 = prompt("Well, what sports do you like to play?", ""); } while (!/^(tennis|bowling|baseball|soccer|basketball|football|cricket|hockey|ice|hockey|rugby|track|golf|horseracing|water polo|swimming|biking|gymnastics|cheer-leading|skiing|snowboarding|iceskating|lacrosse|surfing|archery|boxing|kayaking|diving|spelunking|fencing|karate|judo|rockclimbing|softball|volleyball|wrestling|badminton|javelin throwing|synchronized swimming|yoga|ringette)!?$/i.test(PromptAnswer3)) { alert("I don't think that is a sport."); PromptAnswer3 = prompt("Well, what sports do you like to play?", ""); } alert("Haha yeah, " + PromptAnswer3 "is pretty awesome."); 
var answer = true,
    sportsRegex = /many|options/i,
    sport = true;
do {
    var question = "Well, what sports do you like to play?";
    if (!answer)
        question = "You didn't answer\n" + question;
    else if (!sport)
        question = "I don't know that as a sport.\n" + question;
    answer = prompt(question,"");
    sport = answer.match(sportsRegex);
 } while (!sport)
 sport = sport[0]; // depends on your regex
 alert("Haha yeah, " + sport + " is pretty awesome.");

Such Questions are always a nice usecase for the do-while construct. 这样的问题对于do-while构造始终是一个很好的用例。

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

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