Codecademy howOld语法错误,否则

时间:2019-05-28 14:45:34

标签: javascript

作为一个编码菜鸟,我总是在Codecademy的Javascript练习中苦苦学习语法。这是我在没有互联网搜索帮助的情况下无法解决的第一个问题-如果在此已经解决我的歉意。

我通常要用分号代替它,但这次不是(?)。我试图容纳3个结果:

  1. 某人会在将来输入一年,然后告诉他们他们在那年的年龄。

  2. 有人在他们出生之前一年输入一次,我给他们直到他们出生之前的年份。

  3. 有人在他们出生后的一年输入,当时我给他们他们的年龄。

我花了太多时间移动花括号和半冒号,我想我只是看不到错误。

const howOld = (age, year) =>

 {
  if 
    (year > 2019);
    let calculatedAge = ( year - 2019 + age );
  {
  return `You will be ${calculatedAge } in the year ${year}.`;

  } else if 
    (year < (2019-age));
    let calculatedAge = ( 2019 - year + age ); 
  {
  return `The year ${year } was ${calculatedAge } years before you were 
born.`;

  } else 
    let calculatedAge = ( year - 2019 + age );
  { 
  return `You were ${calculatedAge} in the year ${year}. `; 
  }

};

console.log(howOld(47,2000))

我的'else if'语句出现语法错误。

3 个答案:

答案 0 :(得分:5)

这应该做

    const howOld = (age, year) =>

 {
  if(year > 2019)
  {
    let calculatedAge = ( year - 2019 + age );
    return `You will be ${calculatedAge } in the year ${year}.`;

  } 
  else if(year < (2019-age))
  {  
    let calculatedAge = ( 2019 - year + age ); 
    return `The year ${year } was ${calculatedAge } years before you were born.`;
  } 
  else
  {
    let calculatedAge = ( year - 2019 + age ); 
    return `You were ${calculatedAge} in the year ${year}. `; 
  }

};

console.log(howOld(47,2000))

为解释您的错误,您的if语句错误 而不是拥有

if(condition){do}

您有

if(condition);do{do}

答案 1 :(得分:2)

您可以在if / else语句周围进行一些更改。您很亲近,只是您的条件和方括号有点混乱。

您要确保使用以下语法:

if (condition)
{
    // do something
} else if (condition)
{
    // do something else
}

尝试以下解决方案:

const howOld = (age, year) =>

{
  if (year > 2019)
  {
      let calculatedAge = ( year - 2019 + age );
      return `You will be ${calculatedAge } in the year ${year}.`;
  } else if (year < (2019-age))
  {
      let calculatedAge = ( 2019 - year + age ); 
      return `The year ${year } was ${calculatedAge } years before you were born.`;
  } else
  {
      let calculatedAge = ( year - 2019 + age );
      return `You were ${calculatedAge} in the year ${year}. `; 
  }

};

console.log(howOld(47,2000))

答案 2 :(得分:1)

您的代码中有一些小错误,这些错误来自于您的独特风格。如果我们对代码进行标准化,我们将看到分号过多,并且括号不正确。

const howOld = (age, year) => {
  if (year > 2019);
    let calculatedAge = ( year - 2019 + age );
  {
  return `You will be ${calculatedAge } in the year ${year}.`;

  } else if (year < (2019-age));
    let calculatedAge = ( 2019 - year + age );
  {
  return `The year ${year } was ${calculatedAge } years before you were born.`;

  } else
    let calculatedAge = ( year - 2019 + age );
  {
  return `You were ${calculatedAge} in the year ${year}. `;
  }

};

正确的版本:

const howOld = (age, year) => {
    if (year > 2019){
      let calculatedAge = ( year - 2019 + age );
      return `You will be ${calculatedAge } in the year ${year}.`;
    } else if (year < (2019-age)) {
      let calculatedAge = ( 2019 - year + age );
      return `The year ${year } was ${calculatedAge } years before you were born.`;
    } else {
      let calculatedAge = ( year - 2019 + age );
      return `You were ${calculatedAge} in the year ${year}.`;
    }
  };
console.log(howOld(47,2000))