【问题标题】:Unable to get an alert to launch无法获得启动警报
【发布时间】:2019-11-30 20:21:19
【问题描述】:

方向 在 index.html 和 script.js 文件中,添加以下内容:

Link your JavaScript file to your HTML file.
Create a function named numberCalculation
    Within the function, create two variables:
        The first variable should be named number1
            Set it to 45
        The second variable should be named number2
            Set it to 78
    Create another variable named multiplyNumbers and set it to number1 times number2
    Create an if statement that checks to see if multiplyNumbers is less than 2000
        If it is, add an alert that says "I wish it was a bigger number"
    If the variable multiplyNumbers is not less than 2000, alert "That's more like it!"
Below the definition of the above function, add a call to it: numberCalculation(). As a result, when the page is loaded, it should produce an alert with one of the above messages.

我知道它很明显我只是没有注意到它

script.js:

function numberCalculation(){
  var number1=45;
  var number2=78;
  var multiplyNumbers= number1*number2;  

  if (multiplyNumbers<2000);{
   alert("I wish it was a bigger number");

   if (multiplyNumbers>2000);
    alert("That's more like it!");
  } 
  numberCalculation();
} 

我应该得到“这更像是它!”警报,但我没有得到任何提示

【问题讨论】:

  • 您的开发工具中应该出现错误。 (F12) 一旦你修复它,你很快就会遇到第二个类似的错误。
  • 如果此代码实际运行没有问题,您会感到惊讶 - 您还应该删除您放在 if 条件和相应代码块之间的 ;
  • 2000 年会发生什么?

标签: javascript html function var


【解决方案1】:

您在定义中调用 numberCalculation() 函数。试试这个:

function numberCalculation(){
  var number1=45;
  var number2=78;
  var multiplyNumbers= number1*number2;  

  if (multiplyNumbers<2000) {
   alert("I wish it was a bigger number");
  }

  else if (multiplyNumbers>2000){
   alert("That's more like it!");
  } 
}

numberCalculation();

你在函数中的语法也有点不对。

【讨论】:

  • 我明白了,谢谢!这是我第一次做js 好像总是犯这样的小错误
  • 坚持下去,你会明白的。
【解决方案2】:

由于if 行中的分号;,您编写的代码基本上如下

function numberCalculation(){
  var number1=45;
  var number2=78;
  var multiplyNumbers= number1*number2;  

  if (multiplyNumbers<2000);

  alert("I wish it was a bigger number");

  if (multiplyNumbers>2000);

  alert("That's more like it!");

  numberCalculation();
} 

等于

function numberCalculation(){
  var number1=45;
  var number2=78;
  var multiplyNumbers= number1*number2;  

  if (multiplyNumbers<2000) {

  }

  alert("I wish it was a bigger number");

  if (multiplyNumbers>2000) {

  }

  alert("That's more like it!");

  numberCalculation();
}

除此之外,numberCalculation() 需要从该函数外部调用,否则该函数将永远不会被调用。

【讨论】:

  • 为什么投反对票?您是否曾经在控制台中输入过if (false); { console.log('test') }
猜你喜欢
  • 2014-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-11
  • 2019-10-01
相关资源
最近更新 更多