JavaScript 中返回 NaN 的函数

发布于 2025-01-10 01:19:52 字数 993 浏览 0 评论 0原文

我正在尝试编写一个函数来计算学生对象列表中的平均学生年龄,但是当我运行代码时,该函数将 NaN 作为输出打印出来,

function average_age(){
    let total = 0;
    students.forEach(s => {
        total += Number(s.age);
    });
    return  total/size
}
console.log("Average age is : " + average_age())

这就是我构造数组的方式(我从用户那里获得了输入

const size = 5
let students = [size]

for (let i=1; i<=5; i++){
    let name = prompt("Enter student's name: ")
    let gender = prompt ("Enter student's gender: ")

    students.push({
        name: name,
        gender: gender,
        age:Math.round(Math.random() * (35 - 17 + 1) + 1),
        grade:Math.round(Math.random() * (100 + 1))
    })
}

//display student info
students.map(s =>{
    console.log("Name: " + s.name);
    console.log("gender: " + s.gender);
    console.log("age: " + s.age);
    console.log("grade: " + s.grade);
    console.log();
})

)尝试计算学生年龄的总数(删除除法运算)以检查问题是否是除法,但我仍然得到 NaN 作为输出

I am trying to write a function that calculates the average student age in a list of student objects, but when I run the code the function prints NaN as an output

function average_age(){
    let total = 0;
    students.forEach(s => {
        total += Number(s.age);
    });
    return  total/size
}
console.log("Average age is : " + average_age())

this is how i constructed the array ( i got the input from the user)

const size = 5
let students = [size]

for (let i=1; i<=5; i++){
    let name = prompt("Enter student's name: ")
    let gender = prompt ("Enter student's gender: ")

    students.push({
        name: name,
        gender: gender,
        age:Math.round(Math.random() * (35 - 17 + 1) + 1),
        grade:Math.round(Math.random() * (100 + 1))
    })
}

//display student info
students.map(s =>{
    console.log("Name: " + s.name);
    console.log("gender: " + s.gender);
    console.log("age: " + s.age);
    console.log("grade: " + s.grade);
    console.log();
})

i tried to calculate the total of student age (removing the divide operation) to check if the problem was the division but i still got NaN as an output

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素罗衫 2025-01-17 01:19:52

假设学生数组采用以下格式(收集输入后):

let students = [
  { name: "aaaa" , gender: "male"  , age: 17, grade: 63 },
  { name: "bbbb" , gender: "male"  , age: 20, grade: 70 },
  { name: "yyyy" , gender: "female", age: 18, grade: 45 },
  { name: "zzzz" , gender: "female", age: 18, grade: 70 },
  { name: "xxxx" , gender: "male"  , age: 20, grade: 83 },
];

一种可能的解决方案如下:

let students = [
  { name: "aaaa" , gender: "male"  , age: 17, grade: 63 },
  { name: "bbbb" , gender: "male"  , age: 20, grade: 70 },
  { name: "yyyy" , gender: "female", age: 18, grade: 45 },
  { name: "zzzz" , gender: "female", age: 18, grade: 70 },
  { name: "xxxx" , gender: "male"  , age: 20, grade: 83 },
];
//------------------------------
function average_age(students){
let total = 0;
students.forEach(s => total +=s.age);
return total/students.length
}
//------------------------------
console.log("Average age is : " + average_age(students));

Assuming the students array is in the format below (after you collected the inputs):

let students = [
  { name: "aaaa" , gender: "male"  , age: 17, grade: 63 },
  { name: "bbbb" , gender: "male"  , age: 20, grade: 70 },
  { name: "yyyy" , gender: "female", age: 18, grade: 45 },
  { name: "zzzz" , gender: "female", age: 18, grade: 70 },
  { name: "xxxx" , gender: "male"  , age: 20, grade: 83 },
];

One possible solution is the following:

let students = [
  { name: "aaaa" , gender: "male"  , age: 17, grade: 63 },
  { name: "bbbb" , gender: "male"  , age: 20, grade: 70 },
  { name: "yyyy" , gender: "female", age: 18, grade: 45 },
  { name: "zzzz" , gender: "female", age: 18, grade: 70 },
  { name: "xxxx" , gender: "male"  , age: 20, grade: 83 },
];
//------------------------------
function average_age(students){
let total = 0;
students.forEach(s => total +=s.age);
return total/students.length
}
//------------------------------
console.log("Average age is : " + average_age(students));

在你怀里撒娇 2025-01-17 01:19:52

您可以检查下面的 3 种方法来解决您的问题:

const students = [
  { id: 1, name: "john", age: 25 },
  { id: 2, name: "jack", age: 31 },
  { id: 3, name: "joe", age: 26 },
  { id: 4, name: "jamal", age: 21 },
];

// Your approach:
function average_age() {
  let total = 0;
  let numberOfStudents = students.length;
  
  students.map(student => {
    total += Number(student.age);
  });
  
  return total / numberOfStudents;
}

// Second approach:
function average_age_second() {
  let total = 0;
  let numberOfStudents = students.length;
  
  students.forEach(student => {
    total += Number(student.age);
  });
  
  return total / numberOfStudents;
}

// Third approach: 
function average_age_third() {
  let numberOfStudents = students.length;
  
  const total = students.reduce((partialSum, student) => partialSum + student.age, 0);
  
  return total / numberOfStudents;
}   

console.log("Average age is : " + average_age());
console.log("Average age is : " + average_age_second());
console.log("Average age is : " + average_age_third());

You can check 3 approaches down there for your problem:

const students = [
  { id: 1, name: "john", age: 25 },
  { id: 2, name: "jack", age: 31 },
  { id: 3, name: "joe", age: 26 },
  { id: 4, name: "jamal", age: 21 },
];

// Your approach:
function average_age() {
  let total = 0;
  let numberOfStudents = students.length;
  
  students.map(student => {
    total += Number(student.age);
  });
  
  return total / numberOfStudents;
}

// Second approach:
function average_age_second() {
  let total = 0;
  let numberOfStudents = students.length;
  
  students.forEach(student => {
    total += Number(student.age);
  });
  
  return total / numberOfStudents;
}

// Third approach: 
function average_age_third() {
  let numberOfStudents = students.length;
  
  const total = students.reduce((partialSum, student) => partialSum + student.age, 0);
  
  return total / numberOfStudents;
}   

console.log("Average age is : " + average_age());
console.log("Average age is : " + average_age_second());
console.log("Average age is : " + average_age_third());

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文