事实后,将值分配给变量



我只是在学习并与内置的Chrome Console在一起。请原谅我的结构。我可以问这些初学者的问题更好吗?

有两种变体我无法工作:

如果我对学习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')}
}

您在这里有两个问题:

  1. 只有第一个答案是"否",将第二个提示符(newInterest)显示到if (interestlevel == "no")代码块。

  2. 您的变量情况并不总是匹配的。上述if中的interestlevel应将其更改为interestLevel

请参阅此示例:

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?')
}

在这里并不重要,但是有了较大的脚本,您可以用else if语句替换第二个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();

仅当人回答 no 之前。

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 */

最新更新