Find the mean of three numbers stored in an array of objects

I am currently working on a school assignment that requires me to develop a function for calculating the average mark of 3 students using an existing object in an array. Below, you can see the array and function that I have been working on as part of this assignment.

After spending the past hour researching and experimenting with different approaches, I am unable to figure out how to write the function to sum up the current marks and divide them by the array length.

Could someone please provide insight into what I might be missing or where I may have gone wrong?

let classList = [];

const enrollStudent = (firstName, lastName, age, currentMark) => {
      newStudent = {firstName, lastName, age, currentMark};
      classList.push(newStudent);
      console.log(classList);
  };

  addNewStudent("Mark", "Smith", 24, 85);
  addNewStudent("Dane", "Joe", 21, 90);
  addNewStudent("Steven", "McKnight", 23, 54);

const getClassAverage = arr => classList.reduce((newStudent.currentMark) => currentMark + currentMark, 0) / classList.length

Answer №1

let studentList = [];

const addStudentToList = (firstName, lastName, age, currentMark) => {
      newStudent = {firstName, lastName, age, currentMark};
      studentList.push(newStudent);
      console.log(studentList);
  };

  addStudentToList("Mark", "Smith", 24, 85);
  addStudentToList("Dane", "Joe", 21, 90);
  addStudentToList("Steven", "McKnight", 23, 54);

  const totalMarks = studentList.reduce( (acc, student) => { 
            return acc + student.currentMark
}, 0)


const calculateClassAverage = totalMarks / studentList.length // 76.33333333333333

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it feasible to run an "npm install" on code modifications made after the last version number update?

Hi there, I'm new to npm and have a question. Sorry for being a beginner. I am utilizing the node-dbus npm module, which is currently on version 0.2.0. However, I noticed that there have been some code changes added since the version was last updated ...

Installing ganache-cli globally with npm is unsuccessful due to sudo or permission issues

My npm version is 6.5.0 and node version is v10.10.0. I have never had any issues with npm global installs before. sudo npm install --global ganache-cli npm ERR! code 128 npm ERR! Command failed: /usr/bin/git clone --depth=1 -q -b master https://github ...

Uploading video files using XMLHttpRequest encountered an error on a particular Android device

One of our testers encountered an issue where they failed to upload a video file (.mp4) on a specific Android mobile phone. Interestingly, the same file uploaded successfully on an iPhone and another Android device. To investigate further, I attempted to ...

Challenges with fetching data from APIs in NextJs

I am currently working on a basic NextJs TypeScript application with the app router. The following code is functioning correctly: export default async function Page() { const res = await fetch("https://api.github.com/repos/vercel/next.js"); ...

Tips for modifying the properties of variables within an array using JavaScript

I have an array that holds variables used to control a specific template. divisionsListToManipulate: ['showActivitiesSection', 'hideAssignActionplanDiv', 'displayProp ...

Updating Angular components by consolidating multiple inputs and outputs into a unified configuration object

When I develop components, they often begin with numerous @Input and @Output properties. However, as I continue to add more properties, I find it beneficial to transition to utilizing a single config object as the input. For instance, consider a component ...

Updating the values of parent components in Vue.js 3 has been discovered to not function properly with composite API

Here is a Vue component I have created using PrimeVue: <template lang="pug"> Dialog(:visible="dShow" :modal="true" :draggable="false" header="My Dialog" :style="{ width: '50vw' }" ...

In what ways do node.js and libuv leverage separate threads for their operations?

During my exploration of Node.js and libuv, I decided to set up a basic node server with UV_THREADPOOL_SIZE=1. Afterward, I checked the number of threads being used by looking at pstree. For Node.js 0.10, running pstree -p | grep node resulted in: node(5 ...

Why isn't this ajax script able to successfully transmit the data?

I'm having some trouble sending a JavaScript variable to a PHP file in order to create a query. Here's the script I've been using, but unfortunately it doesn't seem to be working. Any assistance would be greatly appreciated. <select ...

Which PATH environment variable should be added in Heroku to work with ImageMagick?

After installing imagemagick and including it in my Heroku project, I encountered an error that seemed to indicate a missing PATH. In order to remedy this issue, I am currently figuring out how to add the necessary path to Heroku. The specific error messa ...

Guide on locating an element inside its parent using Puppeteer

When using cypress, I am able to locate child elements within other elements like this: cy.get('div#Login_form).within(() => { cy.get('input[name="human[email]"]').type('John') cy.get('input[name="human[password]"]&apo ...

The conditional statement for ajax is malfunctioning

I am encountering an issue with some ajax coding. The if condition is not working as expected - whenever the program runs, only the else statement gets executed even when the if statement should be satisfied. <script type="text/javascript> funct ...

JQuery Ajax call fails to retrieve any information

I have been experimenting with JQuery Ajax methods. I created a basic Ajax request to retrieve specific 'tagged' photos from Flickr. Here is the code snippet I am using: function initiateSearch() { $(function() { var tagValue ...

Using a custom filter in AngularJS allows for seamless data filtering directly from the initial dataset

My goal is to implement a custom filter that will allow me to filter data based on a search word. The scope attribute gets populated in the controller's scope as shown below: naApp.controller('naCareNewTicketCtrl', ['$scope', &apo ...

Jquery Ajax failing to retrieve a response

Here's the jQuery script I am using to fetch data from my server: $(".login_button").click(function () { var username = $(".username").val(); var userkey = $(".userkey").val(); $.ajax({ type: "GET", url: "http://192.168.0. ...

The error states that the type '() => string | JSX.Element' cannot be assigned to the type 'FC<{}>'

Can someone help me with this error I'm encountering? I am fairly new to typescript, so I assume it has something to do with that. Below is the code snippet in question: Any guidance would be greatly appreciated. const Pizzas: React.FC = () => { ...

Transitioning from a traditional CURL method to utilizing AJAX and XMLHttp

I'm currently facing a challenge converting the curl code from an API named TextRazor to AJAX XMLHttp due to limitations on the platform I am working with. Despite trying various solutions shared by the community, I have been unsuccessful in retrievin ...

Having trouble with CSS transitions in a Next.js or Tailwind application?

"use client"; import React, { useState } from "react"; import Image from "next/image"; import Link from "next/link"; const NavigationBar = () => ( <div id="navbar"> <Link href="/">Home</Link> <Link href="/about">About& ...

Unable to activate function when closing Vuetify v-alert

Is there a way to trigger a function when the Vuetify v-alert is closed? I have explored the documentation but haven't found any information on this specific functionality. In the codepen example, the dismissible attribute allows for closing the alert ...

"Utilize Vuejs to establish a binding between two objects when necessary

With the help of moment, my calendar generates 41 days. for (let x = 0; x < 42; x++) { context.add(1, 'd'); let day = { 'date': moment(context), 'events': [] }; } ...