Having trouble extracting a number from an array and encountering a "NaN" error

Encountering an issue with calculating the total grades in the class, as I am receiving an 'NAN' error. Attempted to use parseInt() and .value on students[i].grade but still facing difficulties because of my inexperience.

var students= [{name:"David", grade:80}, 
               {name:"Vinoth", grade:77}, 
               {name:"Divya", grade:88}, 
               {name:"Ishitha", grade:95},
               {name:"Thomas", grade:68}];


var average;

for(i = 0; i < students.length; i++){
    console.log(students[i].grade);
    average += Number(students[i].grade.value);
    console.log(average);
}

The output from console log appears as follows:

80 NaN 77 NaN 88 NaN 95 NaN 68 NaN

Answer №1

1) Ensure to set the initial value of average: var average = 0;

2) Avoid using .value as it is not a valid property.

average += Number(students[i].grade);

3) If all accessed properties are numbers, there is no need to unnecessarily convert number values into numbers using Number().

This code snippet accomplishes the task effectively:

var students= [{name:"David", grade:80}, 
           {name:"Vinoth", grade:77}, 
           {name:"Divya", grade:88}, 
           {name:"Ishitha", grade:95},
           {name:"Thomas", grade:68}];

var average = 0;

for(i = 0; i < students.length; i++){
    console.log(students[i].grade);
    average += Number(students[i].grade);
    console.log(average);
}

console.log("average: ", average/students.length);

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

Verifying code inserted into a form field

I'm looking to include a textarea on my website for users to input and save an entire script. My main concern is the possibility of users trying to insert malicious code into this textarea field. This is what the script format will look like, with t ...

Iterate through the array and add each number to a separate array

I am currently facing an issue with the code snippet provided below. var array = [1, 3, 2] var newArray = [] getNewArray() { for (let i = 0; i < array.length; i++) { for (let x = 0; x < array[i]; x++) { this.newArray.pus ...

The error message "Trying to use b.replace as a function when it is not"

Hey there! I've encountered an uncaught type error in my ajax file console, even though everything seems to be functioning correctly... Here is the HTML code: <div id="deletepropertybutton"><a href = "editproperty.php?property_id=<?php e ...

Change the value of a single element in an array using a component in ReactJS

I'm attempting to pass an array value to a specific index in one component and then assign a desired value from the child component. I need it to work this way because I'm developing a survey app where the number of questions can vary. This is j ...

Guide on creating a Sequelize query to retrieve all tasks linked to a specific user_id

I'm currently developing my first to-do list application using node express ejs and sequelize with sqlite Below is my sqlite.js file which contains the database schemas: const Sequelize = require("sequelize"); const sequelize = new Sequelize({ dia ...

Unable to modify external variable within the subscribe function in Angular using Typescript

I have a code snippet below where I am trying to query the backend to check if an email is already registered or not. However, no matter what email address I pass to the verifyEmail function, the "registered" variable always remains false. How can I upda ...

Potential PHP/Javascript Concurrency Issue

As a newcomer to the world of web programming, I am still learning about Javascript, PHP, Ajax, and more. Despite my efforts to find a simple solution to a particular issue through Google searches, I have hit a roadblock. It seems like there might be a rac ...

Can you stop a JavaScript event using another event?

Can you prevent a JavaScript event in the queue from being executed by another event? Situation: I have two ASP.Net controls, Age - TextBox Save - Button There are two JavaScript validation functions, ValidateAge() - verifies if the age is between ...

Creating Dynamic Interfaces with HTML and JQuery: Transferring Information Between Pages

I'm currently working on an application that involves HTML and JQuery, with no PHP involved. The project consists of two main pages - index.html and register.html. On the index.html page, users are prompted to input a card number before clicking submi ...

Resolving the "Error: Cannot update a React state on an unmounted component" issue

I encountered a console error while attempting to navigate to a new page within my web application. Here's the error message I received: Warning: A React state update was attempted on an unmounted component, which is essentially a no-op. However, t ...

Implementing a Modal Based on Valid Input

I'm currently developing a journal book app using Flask and Bootstrap 5. One of the features I'm working on is a deletion feature, where users can input the number of records they want to delete. I want this feature to display a confirmation moda ...

Create a circle-shaped floor in THREE.JS that mimics the appearance of a shadow

I am working on creating a circle mesh with a gradient material that resembles a shadow effect. Specifically, I want the circle to have a black center and fade to almost white at the edges. Currently, I have been able to create a circle and position it co ...

Tips for assigning information from a react hook within a function or event

Currently, I am in the process of learning how to create hooks in order to efficiently reuse data that needs to be modified across different components. For my project, I am utilizing Material UI's Tabs and require the use of a custom hook called use ...

The error message "data.map is not a function" is thrown in the getStatic

I am currently working on a blog project using Strapi in conjunction with Next.js My goal is to create dynamic pages by utilizing [id].js within the pages/posts/[id].js structure However, I've encountered an issue while attempting to map through the ...

"Troubleshooting a Animation Problem in the Latest Version of

I have discovered an issue with animations on the latest version of Firefox Quantum. Upon loading a page with certain animated elements set to display: none;, when a script changes it to .display = "block";, some parts of the animation may be missed or no ...

Transfer me to Mobile, please

Is it possible to redirect users to mobil.navn.dk when they log on from a mobile device such as an iPhone, iPad, or other smartphones? If accessing the site from a browser, users should not be able to log in on navn.dk. I've attempted to achieve this ...

Modifications to the selected input do not impact the current state of the model

Declare 3 select inputs with hierarchical data to be chosen: <select data-ng-model="current.manufacturer" data-ng-options="c.name for c in manufactures"></select> <select data-ng-model="current.mark" data-ng-options="c.name for c in current ...

Retrieve all visible rows using the AngularJS UI-scroll feature

As of now, I have integrated angular-ui/ui-scroll to dynamically load items into a table. Is there a method available to retrieve the visible items that are currently being rendered on the UI using ui-scroll? I have been utilizing adapter.topVisible and ...

The Kendo window does not trigger the resize event when it is restored after being minimized

According to the Telerik documentation, the Kendo window restore function should trigger a resize event. While this works as expected when the window is maximized and then restored, it does not call the resize event when the window is minimized and then ...

make a pledge without using the constructor or keywords for resolving or rejecting

Can a promise be created without using the constructor new promise() or =>resolve & =>rejected? Let's explore an alternative method below. export function getItById(id) { return fetch(`path`, { method: 'GET', }).then(rslt = ...