简体   繁体   English

新手尝试构建Javascript年龄计算器

[英]Newbie Trying to Build a Javascript Age Calculator

I'm a new self-learner and have recently taken on javascript. 我是一名新的自学者,最近使用了javascript。 I have an assignment (from an online code camp) that I just cant seem to make pass. 我有一项作业(来自在线代码训练营)似乎无法通过。 I feel like I understand the basics of it, but I can't write it in a functional way. 我觉得我了解它的基础知识,但是我不能以实用的方式编写它。 Can anyone help me here? 有人能帮我一下吗?

年龄计算器

The question is in the attached image. 问题在附件图像中。

My code looks a little something like: 我的代码看起来像这样:

function ageCalculator(name, yearOfBirth, currentYear) {
  var age = currentYear - yearOfBirth;
  return (name + "is" + age + "years old.");
  console.log(ageCalculator("Miranda", 1983, 2015));
}

I would appreciate any help! 我将不胜感激任何帮助! Thank you! 谢谢!

You're calling the function ageCalculator just after the return statement. 您在return语句之后调用ageCalculator函数。 Anything just after the return statement won't be called. return语句之后的任何内容都不会被调用。

Just pull that call outside. 只是把那个电话拉到外面。

 function ageCalculator(name, yearOfBirth, currentYear) { var age = currentYear - yearOfBirth; return (name + " is " + age + " years old."); } console.log(ageCalculator("Miranda", 1983, 2015)); 

Call the function outside the function declaration. 在函数声明之外调用函数。

 function ageCalculator(name, yearOfBirth, currentYear) { var age = currentYear - yearOfBirth; return (name + " is " + age + " years old."); } console.log(ageCalculator("Miranda", 1983, 2015)); 

Whenever you return , the function stops immediately: it'll never get to your console.log the way it is now. 每当您return ,该函数将立即停止:它永远不会以现在的方式进入console.log Call console.log and the function itself outside your function, you don't want an infinite recursive loop. 调用console.log并在函数外部调用函数本身,就不需要无限递归循环。

Also make sure to add proper spacing: 另外,请确保添加适当的间距:

 function ageCalculator(name, yearOfBirth, currentYear) { var age = currentYear - yearOfBirth; return (name + " is " + age + " years old."); } console.log(ageCalculator("Miranda", 1983, 2015)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM