What are some ways to calculate the average of data values in a Vue v-simple-table?

I am working with a v-simple-table.

The "TotalAverage" value represents the total average of "ggFinalgrade".

How can I retrieve this value?

View current image

The image I want to display

The initial value is 20

calculated as (30+20+10)/3=20

The second value is 25

found by (30+20)/2=25

My code snippet:

<template><div><v-card tile>
  <v-card-title>{{ fixed.CardTitle }}</v-card-title>
  <v-simple-table><thead><tr>
        <th class="text-left" width="25%">{{ fixed.CourseSectionsName }}</th>
        <th class="text-left" width="35%">{{ fixed.GradeItemsItemname }}</th>
        <th class="text-center" width="10%">{{ fixed.QuizAttemptsTimefinish }}</th>
        <th class="text-center" width="10%">{{ fixed.QuestionAttemptsResponsesummary }}</th>
        <th class="text-center" width="10%">{{ fixed.GradeGradesFinalgrade }}</th>
        <th class="text-center" width="10%">{{ fixed.TotalAverage }}</th>
      </tr></thead><tbody>
      <tr v-for="grade in grades" :key="grade.name">
        <td class="text-left ">
          {{ grade.csName }}</td>
        <td class="text-lefe">
          <div class="my-3" v-for="group in grade.group" :key="group.name">
            <a :href="group.url" style="text-decoration:none">
              {{ group.giItemname }}</a></div></td>
        <td class="text-center">
          <div class="my-3" v-for="qzaTimefinish in grade.qzaTimefinish" :key="qzaTimefinish.name">
            {{ qzaTimefinish }}</div></td>
        <td class="text-center">
          <div class="my-3" v-for="qaResponsesummary in grade.qaResponsesummary" :key="qaResponsesummary.name">
            {{ qaResponsesummary }}</div></td>
        <td class="text-center">
          <div class="my-3 text-center" v-for="group in grade.group" :key="group.name">
            {{ group.ggFinalgrade }}</div></td>
        <td class="text-center">{{ TotalAverage }}</td>
        </tr></tbody>
</v-simple-table></v-card></div></template>
<script>
export default { data() { return {
  fixed: {
    CardTitle: "Course 1",
    CourseSectionsName: "Content",
    GradeItemsItemname: "Quiz Paper",
    QuizAttemptsTimefinish: "Date",
    QuestionAttemptsResponsesummary: "Instructor",
    GradeGradesFinalgrade: "Outcome",
    TotalAverage: "Overall Average"
  },
  grades: [
    {
      csName: "Content 1",
      group: [{
          giItemname: "Quiz 1-1",url: "",ggFinalgrade: 30},
        {
          giItemname: "Quiz 1-2",url: "",ggFinalgrade: 20},
        {
          giItemname: "Quiz 1-3",url: "",ggFinalgrade: 10}],
      qzaTimefinish: ["0913", "0913", "1415"],
      qaResponsesummary: ["Instructor 1", "Instructor 3", "Instructor 4"]},
    {
      csName: "Content 2",
      group: [{
          giItemname: "Quiz 2-1",url: "",ggFinalgrade: 30},
        {
          giItemname: "Quiz 2-2",url: "",ggFinalgrade: 20}],
      qzaTimefinish: ["0913", "1415"],
      qaResponsesummary: ["Instructor 1", "Instructor 2"]
    }]};}};</script>

Answer №1

i. start by adding the method,

ii. calculate the average next and then return it.

iii. lastly, display the totalAverage.

totalAverage(grades) {
   var avg = 0;
   for(var i = 0; i < grades.length; i++){
       avg += Number(grades[i].ggFinalgrade);
   }
   return avg / grades.length;    
},

swap {{TotalAverage }} with {{ totalAverage( grade.group) }}

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

Changing the size of a responsive navigation bar with React and adjusting it based on the window.scrollY position switches between collapsed and un

I'm struggling to create a responsive navbar that automatically adjusts its size once it surpasses the height of the navbar (currently set at 80px). However, when I scroll to around the 80px mark, it starts flickering between the collapsed and expande ...

The paths generated by running npm build are incorrect

After building my Vue Cli 3 project using 'npm run build', I noticed that the 'index.html' file within the new 'dist/' folder has incorrect paths, such as: <link href=/css/app.35dee36a.css <link href=/js/app.826dde09.js ...

What is the best way to create reusable Javascript code?

Lately, I've adopted a new approach of encapsulating my functions within Objects like this: var Search = { carSearch: function(color) { }, peopleSearch: function(name) { }, ... } While this method greatly improves readability, the challeng ...

Save the value entered into an input field using JavaScript and store it in a PHP variable

For a question, I'm developing a dynamic text field where the answer will be displayed based on the user's selection from the answer field. Once the user submits their answer, the PHP code saves it in a PHP variable named $ans1. However, when the ...

Tips on triggering another action before a loop completes, involving AJAX

I've been struggling with this issue for quite some time now and I'm out of ideas. Can anyone offer assistance, please? All I want is to reload the current page after adding items to the cart. I've tried various methods like using count, pr ...

Alter the style type of a Next.js element dynamically

I am currently working on dynamically changing the color of an element based on the result of a function //Sample function if ("123".includes("5")) { color = "boldOrange" } else { let color = "boldGreen" } Within my CSS, I have two clas ...

What could be causing an error when trying to use a computed property?

Currently utilizing Vuex. Within the Getter Foo function, I am returning two values within an array: return ["Try Again"] or return ["Data result", data]. In the computed property, I am checking the array length and returning based on ...

Retrieve JSON data using AngularJS

Can someone guide me on how to make a GET request to my API endpoint and handle the JSON response in my code? Sample Controller.js Code: oknok.controller('listagemController', function ($scope, $http) { $scope.init = function () { ...

unable to fix slideshow glitch after multiple cycles

I've encountered an issue with my custom JavaScript picture scroll. After 3-4 photo changes, the script crashes the page unexpectedly. I'm not sure what is causing this problem! Can someone provide assistance? Below is the code snippet: <di ...

Adjust the contents of an HTTP POST request body (post parameter) upon activation of the specified POST request

Is there a way to intercept and modify an HTTP Post Request using jQuery or JavaScript before sending it? If so, how can this be achieved? Thank you. ...

Is the input in the array? How to find out?

I am currently facing an issue with a script that I have created to count array elements matching a user input. Strangely, all if statements in the script seem to be returning false. I have verified that both the array element and the input value are stri ...

Error message in my Angular project: Invalid Target Error

Out of nowhere, I encountered an invalid target error while running my Angular project with the command: npm start An unhandled exception occurred: Invalid target: {"project":"agmbs","target":"build","configur ...

Python script using selenium webdriver to interact with an accordion container and expand its contents (round

After successfully creating a scraper, I encountered an issue where the data I needed to scrape was hidden and required manual expansion before scraping. Upon inspecting the webpage source code, I found that the data was located within 3 different accordio ...

The Zip file generated in memory is experiencing corruption

I'm encountering an issue while attempting to serve a zip file generated in memory by Flask to a JavaScript front-end. However, the downloaded file appears corrupted and I am unsure of what mistake I may be making. @app.route('/route') def ...

Enhancing the performance of your code by optimizing it using jQuery when a checkbox is either checked or

Looking for ways to optimize a jquery function that toggles a URL parameter based on checkbox status. Any suggestions on how to streamline this code? $('#mapControl').live('click', function(){ var thisUrl = $(location).attr(' ...

Creating custom directives in AngularJS can be a rewarding and powerful

When working with two directives, a situation arose where it was necessary to clear the value in Directive A by using a button located in Directive B. However, when attempting to do this using typical DOM manipulation tools like jQuery, an error occurred w ...

Counting duplicate values associated with the same key in a JSON array using JavaScript/NodeJS

Hello everyone, I could really use some assistance in solving this issue. If this has already been asked before, please direct me to the original question. Currently, I am working with a JSON array of elements. For example: var data = [{"key":"Item1"},{ ...

Error message: Invalid credentials for Twitter API authentication

My attempts to post a tweet seem to be failing for some reason. I suspect that the issue might be related to the signature string, but from following Twitter's instructions on signing requests, everything appears correct. Here is the code snippet I ...

Innovative functionality for adding and deleting elements in JQuery/JavaScript

Is there a way to dynamically add and remove items when the Append/Clear buttons are clicked using this code snippet? $("#btn1").click(function(){ $("ul").prepend("<li>List Item</li>"); }); $("#btn2").click(function(){ $("ul").remove ...

In JavaScript, I'm facing an issue where I can't seem to use ' ' for line breaks for some reason. It's not functioning as expected, and I'm

It seems that I have been struggling with writing a new line in my code using "\n". Despite multiple attempts, the desired outcome has not been achieved. Below is my source code along with a screenshot of the result: var ary3 = new Array('seve ...