【问题标题】:JavaScript Beginner Syntax ErrorsJavaScript 初学者语法错误
【发布时间】:2016-03-30 02:12:33
【问题描述】:

我用纯代码编写了这个程序,但我对 Javascript 的了解还不够,无法正确掌握所有语法。我是一个初学者,请帮助我更正我的代码。它目前根本没有运行。我们仍在循环,这真的给我带来了麻烦。这是针对大学课程的。 (我知道格式不正确,这就是我寻求帮助的原因)

function main() {
alert("Welcome to the program");
var fatGrams = getFatGrams(fatGrams);
var calories = getCalories(fatGrams,calories);
var percentage = caloriesPercentage(fatGrams,calories); 
displayPercentage(percentage); 
}

function getFatGrams(fatGrams) { 
fatGrams = prompt("Enter the number of fat grams in your food item");
while(fatGrams < 0){
  alert("Error, the fat grams cannot be less than 0.");
  fatGrams = prompt("Enter the new number of fat grams.");
 }
return fatGrams;
}

function getCalories(fatGrams,calories) {
calories = prompt("Enter the number of calories in your food item.");
while (calories < 9 || calories > (fatGrams * 9)){
  alert("Error, the calories cannot be less than 9 or exceed the fat    grams * 9");
  calories = prompt("Enter the new number of calories");
}  
return calories;
}

function caloriesPercentage(fatGrams,calories){
percentage = (fatGrams * 9) / calories;
alert("The amount of calories that come from fat is, " + percentage);
return percentage;
}

function displayPercentage(percentage){
  if(percentage < 0.3){
  alert("The food is low in fat.");
 }
  else{
 alert("The food is not too low in fat.");
 }
}

main(); 
alert("End of program"); 

【问题讨论】:

  • function getFatGrams(fatGrams);main中删除function关键字
  • displayPercentage 有一个空的else 块是否有原因...?
  • 不,没有理由。该循环是否应该以“end if”结束?
  • JS中没有if.. then应该是if(){}else{}
  • if (percentage &lt; 0.3) { alert("The food is low in fat."); } 这就是您所需要的。不需要elseend if,正如@me_digvijay 提到的,没有then

标签: javascript loops if-statement


【解决方案1】:

代码中的错误:

  1. 调用函数不需要function 关键字。在调用函数之前删除关键字function,只需将函数调用为getFatGrams(fatGrams);

  2. 您没有将参数传递给function caloriesPercentage。改成caloriesPercentage(fatGrams,calories);

  3. while 循环的表达式应以while(fatGrams &lt; 0) 括在括号中。

  4. OR 应写为||

  5. 使用+ 连接两个字符串(或一个字符串和一个变量)

    "The amount of calories that come from fat is, " + percentage
    
  6. while 的表达式也应该用括号括起来,如 if(percentage &lt; 0.3) 并且不需要 thenif 的大括号应该在else 之前闭合。

    if(percentage < 0.3){
        alert("The food is low in fat.");
    }
    else {
        alert("The food is not too low in fat.");
    }
    
  7. 将值从prompt 赋值给变量

    fatGrams = prompt("Enter the new number of fat grams.");
    

您的最终代码应如下所示:

function main() {
  alert("Welcome to the program");
  var fatGrams;
  var calories;
  var percentage;
  getFatGrams(fatGrams);
  getCalories(fatGrams,calories);
  caloriesPercentage(fatGrams,calories); 
  displayPercentage(percentage); 
}

function getFatGrams(fatGrams) { 
  fatGrams = prompt("Enter the number of fat grams in your food item");
  while(fatGrams < 0){
    alert("Error, the fat grams cannot be less than 0.");
    fatGrams = prompt("Enter the new number of fat grams.");
  }
  return fatGrams;
}

function getCalories(fatGrams,calories) {
  calories = prompt("Enter the number of calories in your food item.");
  while (calories < 9 || calories > (fatGrams * 9)){
    alert("Error, the calories cannot be less than 9 or exceed the fat    grams * 9");
    calories = prompt("Enter the new number of calories");
  }
  return calories;
}

function caloriesPercentage(fatGrams,calories){
  percentage = (fatGrams * 9) / calories;
  alert("The amount of calories that come from fat is, " + percentage);
  return percentage;
}

function displayPercentage(percentage){
  if(percentage < 0.3){
    alert("The food is low in fat.");
  }
  else{
    alert("The food is not too low in fat.");
  }
}

main(); 
alert("End of program"); 

【讨论】:

  • 非常感谢您解释符号应该是什么以及它们的作用,这正是我所需要的。然而,我仍然无法让它运行......
【解决方案2】:

欢迎来到精彩的编程世界!

看起来您已经掌握了基础知识,让我解释一下您在使用 JavaScript 时遇到的一些问题。我将使用一些大词(许多其他有经验的开发人员也会习惯它们!)。

函数声明与函数调用

我在main() 函数中看到的第一个问题是关于何时在 JavaScript 中使用 function 关键字的困惑。您只需要在声明一个函数时使用function,或者换句话说,当您想要“创建一个新函数”时。当您声明某些内容时,您就是在创建它,而在 JavaScript 中,function 关键字是您声明(也称为创建)函数的方式。这是在 javascript 中创建“命名函数”的示例。

function addTwo( number ) {
    return number + 2;
}

这就是一个“命名函数”声明。附带说明一下,您还可以将函数创建为“表达式”(称为“函数表达式”)。应该是这样的

var addThree = function( number ) {
  return number + 3;
};

请注意,在这两种情况下,我们都使用 function 关键字创建函数。

当你想使用一个函数时,你“调用”它。这也称为“调用”函数。要调用(也称为调用)一个函数,您只需在函数名后面加上括号,并传入任何参数。函数调用对命名函数和函数表达式的工作方式相同。所以我们可以像这样调用上面的两个函数

var four = addTwo( 2 );
var five = addThree( 2 );

简单地说,只使用function关键字来制作函数。要调用或使用函数,只需将( argument, otherArg ) 放在函数名称的末尾即可。

分支语句

接下来我看到的是你缺少一些括号! JavaScript 具有 C 风格的语法,这意味着您需要在 ifelse ifswitch 语句的表达式周围加上括号。我列出的那些关键字就是我们所说的分支语句。它们允许您根据某个真值或假值有条件地执行代码,这与允许您根据真值或假值重复代码的循环语句相反。 JavaScript 中if .. else 分支语句的结构如下。

if ( number % 2 === 0 ) {
    number *= 2;
} else if ( number % 3 === 0 ) {
    number *= 3;
} else {
    number += 5;
}

对于ifelse if,您需要提供一个“表达式”来进行分支。如果ifelse if 表达式均未计算为真,则始终执行else 语句。 else 子句不是必需的,因此如果您的 else 块中没有任何代码,您可以将其删除!在您的代码中包含这样的单个 if 语句是非常好的

if ( needsCleanup ) {
    doCleanup();
}

当然可以有else if 子句而没有else 子句,else 始终是可选的。但请记住,else 子句永远不会有表达式,因此您永远不要在简单的 else 之后写 ()

循环语句

循环是一个需要一段时间才能掌握的大概念,所以不要感到沮丧或气馁!在 JavaScript 中,我们有 forwhiledo while 循环。就像if 语句一样,循环语句需要一个表达式,但在这种情况下,它们需要知道何时停止循环。我不会介绍do while 循环,因为它们很少使用并且刚开始时可能会造成混淆。简单地说,循环语句说:“继续执行这个代码块,直到表达式为假。”我发现用for 循环来说明这一点最容易。 For 循环是运行代码块特定次数的好方法。让我们看一个使用 for 循环将 1 到 10 的所有数字相加的示例。

var sum = 0;
for ( var i = 1 ; i <= 10 ; i++ ) {
   sum += i;
}
// sum will be 55
// i will be 11

for 循环包含三个由; 分隔的表达式。第一个用于初始化,仅在循环开始之前执行(var i = 1 部分)。第二个是每次迭代前检查的条件。最后一个是在每次循环执行结束时执行的增量操作。如果我们将这个 for 循环重写为 while 看起来会像这样......

var sum = 0;
var i = 1;
while ( i <= 10 ) {
    sum += i;
    i++;
}

所以你可以看到var i = 1 发生在循环开始之前。 i &lt;= 10 发生在每次循环运行其主体({} 中的部分)之前。 i++ 发生在循环的末尾,然后它会检查 i &lt;= 10 以查看它是否应该继续循环。

字符串连接

字符串连接是将两个字符串放在一起的过程。在 JavaScript 中,+ 运算符具有加法1 + 1 和字符串连接'hello' + ' world' 的双重职责。所以当你想把两个字符串放在一起时,你只需添加它们!如果字符串在变量中,那么您可以将它们“添加”在一起......

var salutation = 'Hello';
var name = 'Kylie';
var greeting = salutation + ', ' + name;

// greeting will be "Hello, Kylie"

我希望能解决一些问题,如果您有任何问题,请告诉我!

【讨论】:

  • 感谢您的热烈欢迎!到目前为止,我不是粉丝,但我尽了最大的努力。 :P
【解决方案3】:

因为这是一门课,所以我不会给你答案,而是指出问题。

在Javascript中调用一个函数并返回一个值:

var myvalue = myFunction();

您的主目录中有 function...,需要修复它。

有些函数只返回值,因此调用它们不需要参数(如上),而其他函数则需要你所做的输入的参数。

提示返回一个值,您需要捕获该值:

var myinputvalue = prompt("enter value");

参考:https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt

使用whileif等时,条件需要用括号括起来

while (mything &lt; 0){参考; https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...elsehttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while

条件中的“OR”由||表示,AND由&amp;&amp;表示

注意:您可以使用一些在线工具来解决一些问题,例如将代码粘贴到http://jshint.com/,它会告诉您许多语法问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 2019-06-18
    • 2017-11-19
    • 2018-02-17
    • 2015-06-09
    • 1970-01-01
    相关资源
    最近更新 更多