JavaScript初学者语法错误

JavaScript初学者语法错误,javascript,loops,if-statement,Javascript,Loops,If Statement,我用普通代码编写了这个程序,但我对Javascript了解不够,无法正确使用所有语法。我是一个刚开始编程的人,请帮我更正我的代码。它目前根本没有运行。我们仍然在重复循环,这真的给我带来了麻烦。这是给大学班的。(我知道这个格式不正确,这就是我寻求帮助的原因) 函数main(){ 警惕(“欢迎参加该计划”); var fatGrams=getFatGrams(fatGrams); var卡路里=获取卡路里(脂肪克,卡路里); var百分比=卡路里百分比(脂肪克,卡路里); 显示百分比(百分比); }

我用普通代码编写了这个程序,但我对Javascript了解不够,无法正确使用所有语法。我是一个刚开始编程的人,请帮我更正我的代码。它目前根本没有运行。我们仍然在重复循环,这真的给我带来了麻烦。这是给大学班的。(我知道这个格式不正确,这就是我寻求帮助的原因)

函数main(){
警惕(“欢迎参加该计划”);
var fatGrams=getFatGrams(fatGrams);
var卡路里=获取卡路里(脂肪克,卡路里);
var百分比=卡路里百分比(脂肪克,卡路里);
显示百分比(百分比);
}
函数getFatGrams(fatGrams){
脂肪克数=提示(“输入食物中脂肪克数”);
而(fatGrams<0){
警报(“错误,脂肪克不能小于0”);
fatGrams=提示(“输入新的脂肪克数”);
}
返回fatGrams;
}
功能获取卡路里(脂肪克、卡路里){
卡路里=提示(“输入食物中的卡路里数”);
而(卡路里<9 | |卡路里>(脂肪克*9)){
警告(“错误,卡路里不能低于9或超过脂肪克*9”);
卡路里=提示(“输入新的卡路里数”);
}  
返回热量;
}
功能卡路里百分比(脂肪克、卡路里){
百分比=(脂肪克*9)/卡路里;
警惕(“来自脂肪的热量为,”+百分比);
回报率;
}
函数显示百分比(百分比){
如果(百分比<0.3){
警惕(“食物脂肪含量低”);
}
否则{
警惕(“食物的脂肪含量不是太低。”);
}
}
main();
警报(“程序结束”);
代码中的错误:

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

  • 您没有将参数传递给
    函数卡路里百分比
    。将其更改为
    卡路里百分比(脂肪克,卡路里)

  • while
    循环的表达式应作为
    while(fatGrams<0)
    括在paradensis中

  • 应写成
    |

  • 使用
    +
    连接2个字符串(或一个字符串和一个变量)

  • while
    的表达式也应包含在paradensis中,如(百分比<0.3)
    且无
    则需要
    if
    的花括号应在
    else
    之前关闭

    if(percentage < 0.3){
        alert("The food is low in fat.");
    }
    else {
        alert("The food is not too low in fat.");
    }
    
  • 您的最终代码应该如下所示:

    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"); 
    
    函数main(){
    警惕(“欢迎参加该计划”);
    var-fatGrams;
    var卡路里;
    风险值百分比;
    getFatGrams(fatGrams);
    获取卡路里(脂肪克,卡路里);
    卡路里百分比(脂肪克,卡路里);
    显示百分比(百分比);
    }
    函数getFatGrams(fatGrams){
    脂肪克数=提示(“输入食物中脂肪克数”);
    而(fatGrams<0){
    警报(“错误,脂肪克不能小于0”);
    fatGrams=提示(“输入新的脂肪克数”);
    }
    返回fatGrams;
    }
    功能获取卡路里(脂肪克、卡路里){
    卡路里=提示(“输入食物中的卡路里数”);
    而(卡路里<9 | |卡路里>(脂肪克*9)){
    警告(“错误,卡路里不能低于9或超过脂肪克*9”);
    卡路里=提示(“输入新的卡路里数”);
    }
    返回热量;
    }
    功能卡路里百分比(脂肪克、卡路里){
    百分比=(脂肪克*9)/卡路里;
    警惕(“来自脂肪的热量为,”+百分比);
    回报率;
    }
    函数显示百分比(百分比){
    如果(百分比<0.3){
    警惕(“食物脂肪含量低”);
    }
    否则{
    警惕(“食物的脂肪含量不是太低。”);
    }
    }
    main();
    警报(“程序结束”);
    
    代码中的错误:

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

  • 您没有将参数传递给
    函数卡路里百分比
    。将其更改为
    卡路里百分比(脂肪克,卡路里)

  • while
    循环的表达式应作为
    while(fatGrams<0)
    括在paradensis中

  • 应写成
    |

  • 使用
    +
    连接2个字符串(或一个字符串和一个变量)

  • while
    的表达式也应包含在paradensis中,如(百分比<0.3)
    且无
    则需要
    if
    的花括号应在
    else
    之前关闭

    if(percentage < 0.3){
        alert("The food is low in fat.");
    }
    else {
        alert("The food is not too low in fat.");
    }
    
  • 您的最终代码应该如下所示:

    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"); 
    
    函数main(){
    警惕(“欢迎参加该计划”);
    var-fatGrams;
    var卡路里;
    风险值百分比;
    getFatGrams(fatGrams);
    获取卡路里(脂肪克,卡路里);
    卡路里百分比(脂肪克,卡路里);
    显示百分比(百分比);
    }
    函数getFatGrams(fatGrams){
    脂肪克数=提示(“输入食物中脂肪克数”);
    而(fatGrams<0){
    警报(“错误,脂肪克不能小于0”);
    fatGrams=提示(“输入新的脂肪克数”);
    }
    返回fatGrams;
    }
    功能获取卡路里(脂肪克、卡路里){
    卡路里=提示(“输入食物中的卡路里数”);
    而(卡路里<9 | |卡路里>(脂肪克*9)){
    警告(“错误,卡路里不能低于9或超过脂肪克*9”);
    卡路里=提示(“输入新的卡路里数”);
    }
    返回热量;
    }
    功能卡路里百分比(脂肪克、卡路里){
    百分比=(脂肪克*9)/卡路里;
    警惕(“来自脂肪的热量为,”+百分比);
    回报率;
    }
    函数显示百分比(百分比){
    如果(百分比<0.3){
    警惕(“食物是
    
    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"); 
    
    var myvalue = myFunction();
    
    var myinputvalue = prompt("enter value");
    
    function addTwo( number ) {
        return number + 2;
    }
    
    var addThree = function( number ) {
      return number + 3;
    };
    
    var four = addTwo( 2 );
    var five = addThree( 2 );
    
    if ( number % 2 === 0 ) {
        number *= 2;
    } else if ( number % 3 === 0 ) {
        number *= 3;
    } else {
        number += 5;
    }
    
    if ( needsCleanup ) {
        doCleanup();
    }
    
    var sum = 0;
    for ( var i = 1 ; i <= 10 ; i++ ) {
       sum += i;
    }
    // sum will be 55
    // i will be 11
    
    var sum = 0;
    var i = 1;
    while ( i <= 10 ) {
        sum += i;
        i++;
    }
    
    var salutation = 'Hello';
    var name = 'Kylie';
    var greeting = salutation + ', ' + name;
    
    // greeting will be "Hello, Kylie"