在事实之后为变量赋值

问题描述 投票:-2回答:2

我正在学习和搞乱内置的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')}
}
javascript if-statement
2个回答
0
投票

你有两个问题:

  1. 要仅在第一个答案为“否”时显示第二个提示(newInterest),请将其移至if (interestlevel == "no")代码块中。
  2. 您的变量大小写并不总是匹配。上述interestlevel声明中的if应更改为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?')
}

这并不重要,但是对于更大的脚本,你可以用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();

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 */
© www.soinside.com 2019 - 2024. All rights reserved.