Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9307177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:20:13+00:00 2026-06-19T00:20:13+00:00

Here is my code so far for my school project (using Murach’s JavaScript and

  • 0

Here is my code so far for my school project (using Murach’s JavaScript and DOM Scripting by Ray Harris). The chapter is only about Arrays and does not cover Prototypes, but I wanted to try it out based on Internet tutorials and references:

 /*
Operation

    This application stores the last name, first name, and score for 
    one or more students and it calculates the average score for all of the scores
    that have been entered. When the user clicks on the Clear button, this 
    application clears the score data from this application. When the user clicks 
    on the Sort button, this application sorts the data in alphabetical order by 
    last name.

Specifications

    The program should use one or more arrays to store the data.
    Assume that the user will enter valid data.
*/
var $ = function (id) 
{ 
    return document.getElementById(id); 
}

/*
Array prototype object extension for averaging the contents

"Adding a method to the built-in Array object to extract the average 
of any numerical values stored in the array is therefore a useful 
addition to that object." http://javascript.about.com/library/blaravg.htm
*/
Array.prototype.average = function () 
{
    var avg = 0;
    var count = 0;
    for (var i = 0; i<this.length; i++) 
    {
       //never gets here:
        alert(i + ": " + this[i]);
        var e = +this[i];
        if(!e && this[i] !== 0 && this[i] !== '0') 
        {
            e--;
        }
        if (this[i] == e) 
        {
            avg += e;
            count++;
        }
    }   
    return avg / count;
}

var addScore = function ()
{
    studentScores[$('last_name').value + ', ' + $('first_name').value] = $('score').value;
    update();
}

var clearScore = function ()
{
    for (var i in studentScores)
    {
        studentScores[i] = '';
    }
    update();
}

var sortScore = function ()
{
    scores.sort();
    update();
}

var update = function ()
{
    var result = '';
    for (var i in studentScores)
    {
        result += (i + ': ' + studentScores[i] + '\n');
    }
    $('scores').value = result;
    $('average_score').value = studentScores.average().toFixed(1);
}

window.onload = function ()
{
    //a variable is initialized inside a function without var, it will have a global scope:
    studentScores = [];
    $('add_button').onclick = addScore;
    $('sort_button').onclick = sortScore;
    $('clear_button').onclick = clearScore;
    $('last_name').focus();
}

When the code enters the “update()” function (end of the “addScore()” function) and accesses the array,
it populates the “literal” code from the Prototype into the text area (and fails to find the average on the next line):

I don’t have enough rep points to post the image, but here is my output (there are no errors in the Chrome JS Console):

lowe, doug: 82
average: function () 
{
    var avg = 0;
    var count = 0;
    for (var i = 0; i<this.length; i++) 
    {
        //never gets here:
        alert(i + ": " + this[i]);
        var e = +this[i];
        if(!e && this[i] !== 0 && this[i] !== '0') 
        {
            e--;
        }
        if (this[i] == e) 
        {
            avg += e;
            count++;
        }
    }   
    return avg / count;
}

Any help appreciated (best practice or algorithm suggestions welcome)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T00:20:14+00:00Added an answer on June 19, 2026 at 12:20 am

    Change this:

    studentScores = []
    

    to this:

    studentScores = {}
    

    …so that you’re using an Object instead of an Array.

    Your for loop in average() is just iterating numeric indices instead of the non-numeric keys you created.

    Create your average() method as a standalone function like the others, and pass studentScores to it to calculate the average, and then use for-in instead of for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my code so far: #include<iostream> #include<string> #include<fstream> using namespace std; int main() {
Ok. here's the operations i successfully code so far thank's to your help: Adittion:
I'm a total newb to LINQ. Here is the code I have so far:
Here is what I got so far. Please read the comment in the code.
I have a string that is args[0] . Here is my code so far:
Here's my code so far... if (isset($_POST['projectName'])) { $projectName = $_POST['projectName']; $dueDate = $_POST['dueDate'];
Here is my code so far: - (IBAction) startApproximiteLevel:(id)sender { [getBackgroundLevel startAnimation:self]; float sample1;
Here is my code so far: $friends = $facebook->api('/me/friends'); print_r($friends); I want to print
Here is my code so far: public class PostfixCalculator { private Stack<Float> stack; private
Here's my code so far (in the draw rect): // Drawing code here. NSLog(@%@,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.