Having trouble with the JavaScript code for a basic GPA calculator?

I am having issues with my code for a basic grade calculator. Even though I can input numbers, the final grade is not displayed on the screen. Can someone please help me identify what mistake I might be making? Please ignore the Japanese text, as it's just instructions for my students who are Japanese learners.

    <!DOCTYPE html>
        <html>
        <head>
        <meta charset="UTF-8">
        <title>English 1B</title>

        <script>
        function computeGrade() {
        var q1=document.getElementById('q1').value;
        var q2=document.getElementById('q2').value;
        var q3=document.getElementById('q3').value;
        var es1=document.getElementById('es1').value;
        var es2 =document.getElementById('es2').value;
        var es3 =document.getElementById('es3').value;
        var a=document.getElementById('a').value;
        var average = ((q1+q2+q3)/192*.25 + (es1+es2+es3)/50*.55 +     a/14*.2)*100);
        document.getElementById('average').innerHTML = "Your average is" +   average + "%.";}

        </script>
        </head>

        <body>
        <h2>Grade Calculation</h2>

        <p>Quiz 1 score: <input id="q1" type="number" onchange="computeGrade()"></p>

         <p>Quiz 2 score: <input id="q2" type="number" onchange="computeGrade()"></p>

<p>Quiz 3 score: <input id="q3" type="number" onchange="computeGrade()"></p>

<p>Elevator Speech 1 score: <input id="es1" type="number" onchange="computeGrade()"></p>

        <p>Elevator Speech 2 score: <input id="es2" type="number" onchange="computeGrade()"></p>

        <p>Elevator Speech 3 score: <input id="es3" type="number" onchange="computeGrade()"></p>

        <p>Attendance count: <input id="a" type="number" onchange="computeGrade()"></p><br>

         <p id = "average"></p>


         </body>

         </html>

Answer №1

There seems to be an extra parenthesis on line 16 of the HTML code:

// [YOUR CODE] this last parenthesis is not necessary.
var average = ((q1+q2+q3)/192*.25 + (es1+es2+es3)/50*.55 + a/14*.2)*100); 

// [YOUR CODE WITHOUT LAST PARENTHESIS] this correction resolves the issue.
var average = ((q1+q2+q3)/192*.25 + (es1+es2+es3)/50*.55 + a/14*.2)*100; 

I discovered this error by copying and pasting your snippet into an HTML file, then checking my browser's console output for error indications.

Answer №2

There was a single unexpected token found in your JavaScript code: it is recommended to remove the closing parenthesis at the end of the line where you are calculating the average.

var avg = ((q1+q2+q3)/192*.25 + (es1+es2+es3)/50*.55 + a/14*.2)*100;

I have tested your code in a CodePen for verification: http://codepen.io/anon/pen/jWMwwM

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

Styled-components does not generate a style tag as output

After creating a new project in React with Webpack, I decided to experiment with Styled Components. In my index.js file, the code is structured like this: import React from "react" import ReactDOM from "react-dom" import Page from "./site/Page" import s ...

I am experiencing issues with my React DND implementation in Next JS - can anyone help troubleshoot?

I'm encountering an issue with my React DND implementation. Although I am able to drag elements, they are not being received by the drop targets. I even tried using console.log in the drop function of useDrop but nothing is logging in the console on d ...

What is the method for inserting form control values into a QueryString within HTML code?

Struggling with passing HTML form control values into a QueryString for page redirection. While I can easily input static values into a QueryString and retrieve them using PHP's GET method, I am encountering difficulties when it comes to dynamic valu ...

How can you troubleshoot code in NextJS API routes similar to using console.log?

Incorporating nextjs API routes into my upcoming project has been a priority, and I recently encountered an issue with code execution upon sending a POST request. Debugging has proven to be challenging since I am unable to use conventional methods like co ...

Sort through an array of objects by their respective values within another array of nested objects

I am struggling to filter out positions from the positions array that are already present in the people array. Despite trying different combinations of _.forEach and _.filter, I can't seem to solve it. console.log(position) var test = _.filter(posi ...

Upon installation of Next.js, an error in the globals.css file quickly emerged, yet remarkably, it did not disrupt the code in

Here is the link to the repository: here. Once I created the next.js environment using the command "npx create-next-app@latest ./" and ran "npm run dev", I encountered the following error message:- ../../../#React Projects/My projects/causs/styles/global. ...

Is it causing issues having the same version of jQuery included multiple times?

Within my HTML file, referred to as x.html, I've included other HTML files as well. In x.html, I have integrated the jquery library version 1.4.1. It seems that this same version of jquery is also being included from the other HTML files. Even though ...

Show a specific item when selecting an option from a dropdown menu

Hey there, I have a question regarding creating a theme for the Genesis Framework. Right now, I'm facing a challenge with one particular element. Here's the situation: I've got two drop-down lists, but I only want the second one to be visib ...

Enhancing the current Node.js, Express, MongoDB, and Socket.io stack with the integration of AngularJS

After spending some time developing a web app using Node.js, Express, MongoDB, Mongoose and Socket.io, I've successfully released version one. Looking ahead to version two, my plan is to revamp the UI completely and switch to a front-end framework lik ...

Issues verifying the selected value for the ajax request

One of the challenges I'm facing is related to a form that requires specific user selections. If the user chooses a value of 1, the form can be submitted without any additional steps. However, if they select values 2 or 3, they must then choose an opt ...

Change Scenery by Using Arrow Keys

I am trying to increase the speed of my background when the up and arrow keys are pressed. Despite my attempts, it is not working as expected. The variable this.speed controls the speed of the background. To achieve this, I implemented an if statement t ...

Is there a way to create an infinite fade in/out effect for this?

Is there a way to ensure that the method runs after the "click" event in this code snippet? The code currently fades in and out the div #shape while the start variable is true, but when I call the "start" method from the "click" event, the browser stops wo ...

"Troubleshooting: ngForm.controls returning undefined value in Angular application

Trying to test this HTML code in Angular: <form name="formCercarUsiari" #formCercarUsuari="ngForm" class="tab-content"> <input #inputNif class="form-control input-height" maxlength="100" type="text" placeholder="Indiqui NIF" name="nif" i18n-pla ...

Unable to render pages with ng-view in Angular.js

I am facing an issue with my Angular.js application where the pages are not loading when using ng-view. When I type the URL http://localhost:8888/dashboard, the pages should be displayed. Here is an explanation of my code: view/dashboard.html: <!DO ...

Tips for enabling the OnLoad Event for a React Widget

Hey there! I'm a beginner with React and I'm struggling to call a function once after the component is created. I tried adding an onLoad event in the component creation, but it's not working as expected. The function 'handleClick' ...

Hidden content from Vue router view

My goal is to have the navigation pane overlaying the content of the page, but instead, it is being appended to the bottom where the end of the navigation drawer would be. I've experimented with different variations to display data using navigation dr ...

Having trouble removing objects from a map results in having to invoke the function three times

I am facing an issue with the function findPrice where I need to call it multiple times to delete objects that match a specific price. The problem arises when dealing with large maps and two functions. Here is the JSON format: [ { _id: 5c6c408dbec3ab457c ...

Bootstrap Toggle Button with Dropdown Menu

I have a button-group with font awesome icons and a dropdown on one of them, as illustrated in the example below. I am trying to toggle the button status, but for some reason, the 'active' class is automatically removed on mouseout. It seems to ...

Enumerate the variable values that are validated using jQuery

I've made good progress with this in jsFiddle, but I can't seem to figure out why the first value isn't displaying a bullet point.. Check out my code on jsFiddle! <div id="checkboxes"> <input id="chkbx_0" type="checkbox" name= ...

Is it possible to eliminate any leftover comments from a JSON string using jQuery and Ajax?

Having an issue with a framework that generates invalid JSON String like this: /* { "myobject" : "test"} */ The problem lies in the comments before and after the json string. This is done for security purposes, as it's not recommended to return JSON ...