简体   繁体   English

在函数Javascript中调用函数

[英]Calling function inside a function Javascript

Hi everyone, I'm learning JS and trying to practice using a function inside a function. 大家好,我正在学习JS,并尝试练习在函数内部使用函数。 So, I've come up with my own problem to code. 因此,我想出了自己的问题来进行编码。 I understand that there's an easier way to do this and the problem could've been written better, but for the purpose of practicing, this is what I've come up with: 我知道有一种更简便的方法可以解决此问题,而本来可以写得更好,但是出于练习的目的,这是我想出的:

John is 13 years old. 约翰13岁。 Write a function to find out what year John was born, and what school type he is attending BASED ON his birth year -- high school or middle school. 编写一个函数,根据约翰的出生年份(高中或中学),找出约翰出生的年份和所上的学校类型。

Middle school = 2003 - 2004 中学= 2003-2004
High school = 1999 - 2002 高中= 1999-2002

function calculateBirthYearJohn(ageJohn) {
    var currentYear = 2017;
    var birthYearJohn = currentYear - ageJohn;
    return birthYearJohn;
}

function defineSchoolTypeJohn(birthYear) {
    var birthYear = calculateBirthYearJohn(ageJohn);
    if (birthYear >= 2003 & <= 2004) {
        console.log('John goes to Middle School.');
    } else if (birthYear >= 1999 & <= 2002) {
        console.log('John goes to High School.');
    }
}

Console will say "John goes to Middle School" if he's born between 2003 and 2004; 如果他在2003年到2004年之间出生,Console会说“约翰上初中”。 "John goes to High School" if he's born between 1999 and 2002. 如果他出生于1999年至2002年之间,则“约翰上高中”。

Blank result -- no error. 结果为空白-没有错误。

I've made a newbie mistake somewhere; 我在某处犯了新手错误; can someone please point me in the right direction? 有人能指出我正确的方向吗? Much appreciated! 非常感激!

Couple problems. 夫妻问题。

line 8: ageJohn == undefined 第8行: ageJohn == undefined

lines 9 and 11: In JavaScript, && is the Logical AND operator (two ampersands). 第9和11行:在JavaScript中, &&逻辑AND运算符(两个&符号)。 One singular ampersand is the Bitwise AND operator. 按位与运算符是一个奇数“&”号。 Also, comparison only works between two variables at a time, hence, your attempt to chain is invalid. 另外,一次只能在两个变量之间进行比较,因此,您尝试进行链接无效。

See below for a working example. 请参见下面的工作示例。

 function calculateBirthYear(ageJohn) { const currentYear = 2017 const birthYear = currentYear - ageJohn return birthYear } function defineSchoolType(ageJohn) { const birthYear = calculateBirthYear(ageJohn) if (birthYear >= 2003 && birthYear <= 2004) console.log('John goes to Middle School.') else if (birthYear >= 1999 && birthYear <= 2002) console.log('John goes to High School.') else console.log('John doesn\\'t go to Middle School or High School') } defineSchoolType(13) // Middle School. defineSchoolType(15) // High School. defineSchoolType(20) // Neither. 

You have: 你有:

var birthyear = calculateBirthYear(ageJohn);

but you have not defined ageJohn anywhere. 但您尚未在任何地方定义ageJohn Up in calculateBirthYear , you gave it as the argument, but that is not the same as a variable that can be accessed anywhere. calculateBirthYear ,您将其作为参数提供,但这与可以在任何地方访问的变量不同。 I think you meant to write: 我想你打算写:

function defineSchoolTypeJohn(ageJohn) {
    var birthYear = calculateBirthYear(ageJohn);
    ...

Now, you have defined ageJohn as the argument to the function. 现在,您已经将ageJohn定义ageJohn函数的参数。

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

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