在事件

时间:2017-12-19 11:30:52

标签: javascript if-statement

我正在学习并搞乱内置的Chrome控制台。请原谅我的结构。我可以问这些初学者问题吗?

我无法使用两种变体:

如果我对学习JavaScript不感兴趣,请输入“不”。响应,我想要一个新的提示,以显示分配newInterest变量,然后继续提醒'这很棒'。



var interestLevel = prompt('Hello, are you interested in learning JavaScript?')
if (interestLevel == "yes") {
  alert('Wonderful, welcome!')
}
if (interestLevel == "no") {
  var newInterest = prompt('Then what are you interested in?')
}




第二个应该在“是”之后结束。响应,但它实际上继续询问第二个提示,不应该是这种情况。



var interestLevel = prompt('Hello, are you interested in learning JavaScript?')
if (interestLevel == "yes") {
  alert('Wonderful, welcome!')
}
if (interestlevel == "no") {
  alert('That sucks')
}
/* press ok to make the box go away so that the next prompt below will show up */
var newInterest = prompt('Then what are you interested in?')




编辑:我根据每个人的建议修改了所有内容。我的下一个目标是找出如何使下面的功能。

var interestLevel = prompt('Hello, are you interested in learning JavaScript?')
if (interestLevel == "yes") {
  alert('Wonderful, welcome!')
}
else if (interestLevel == "no") {
  alert('That sucks')
  /* press ok to make the box go away so that the next prompt below will show up */
  var newInterest = prompt('Then what are you interested in? html? css?')
if (newInterest === "html"){alert('let\'s get started with html!')}
else if (newInterest === "css"){alert('I don\'t know css')
}
    else {alert('please input css or html')}
}

2 个答案:

答案 0 :(得分:0)

仅在此人回复时提示。

var interestLevel = prompt('Hello, are you interested in learning JavaScript?')
if (interestLevel == "yes") {
  alert('Wonderful, welcome!')
}
if (interestlevel == "no") {
  alert('That sucks')
}
/* press ok to make the box go away so that the next prompt below will show up*/
if(interestLevel == "no")
    var newInterest = prompt('Then what are you interested in?')

或者这也可以,取决于您希望如何组织代码。

var interestLevel = prompt('Hello, are you interested in learning JavaScript?')
if (interestLevel == "yes") {
  alert('Wonderful, welcome!')
}
if (interestlevel == "no") {
  alert('That sucks');
  var newInterest = prompt('Then what are you interested in?')
}
/*press ok to make the box go away so that the next prompt below will show up */

答案 1 :(得分:0)

这里有两个问题:

  1. 如果第一个答案为" no,"则仅显示第二个提示(newInterest)。将其移至if (interestlevel == "no")代码块。

  2. 您的变量大小写并不总是匹配。上述interestlevel声明中的if应更改为interestLevel

  3. 见这个例子:

    
    
    var interestLevel = prompt('Hello, are you interested in learning JavaScript?')
    if (interestLevel == "yes") {
      alert('Wonderful, welcome!')
    }
    if (interestLevel == "no") {
      alert('That sucks')
      /* press ok to make the box go away so that the next prompt below will show up */
      var newInterest = prompt('Then what are you interested in?')
    }
    
    
    

    这里并不重要,但是对于较大的脚本,您可以使用if语句替换第二个else if语句,该语句只有在第一个if时才会运行}语句返回false。这样,如果第一个条件是true,则第二个if语句根本不会被检查(因为它的结果不重要)并且可能会节省几纳秒。

    <强>更新

    如果要将此代码转换为函数,请将其包装在函数语句中,如下所示:

    function myFunction() {
      var interestLevel = prompt('Hello, are you interested in learning JavaScript?')
      if (interestLevel == "yes") {
        alert('Wonderful, welcome!')
      }
      if (interestLevel == "no") {
        alert('That sucks')
          /* press ok to make the box go away so that the next prompt below will show up */
        var newInterest = prompt('Then what are you interested in?')
      }
    }
    

    然后调用它,就像这样:

    myFunction();