JavaScript

Moderators: None (Apply to moderate this forum)
Number of threads: 2061
Number of posts: 5164

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Issue Averaging Array Posted by quinnvanorder on 13 Nov 2011 at 12:35 PM
Hello all, I have a project I am working on, and I am so close to finishing. Unfortunately this last bit has snagged me. Using this code, it appears that instead of actually adding each numerical value together, it is adding each new value to the end of the string. thus 5+8=58 ect... I would appreciate any pointers.

Below is the javascript code, and I have attached all of the files used in the application

var score_list = [];
var full_list = [];

var $ = function (id) {
    return document.getElementById(id);
}


var add_click = function () {
    var last = $("last_name").value
	var first = $("first_name").value
	var score1 = $("score_input").value
	score_list.push(score1);
	full_list.push(last + ", " + first + ": " + score1);
	update_full_list(); 
// Beginning of issue
	var Sum = 0
	var Average 
	// Find the lowest & highest
	for (var i = 0; i < score_list.length; i++) {
   	Sum=Sum + score_list[i]  //as i understand it, this should be adding all of the array up in one value instead of just stringing them together
   	Average = Sum/score_list.length
	}
	alert(Sum); // I used this to show me what value it was getting for sum, and it displays the strung together numbers instead of the sum I want
//end test code	
	
	}

var clear_click = function () {
full_list.splice(0,999999);
update_full_list();
}

var last_click = function () {
full_list.sort();
update_full_list(); 
}
	
var update_full_list = function () {
        var list = "";
        for ( var i in full_list ) {
            list += (parseInt(i)+1) + ": " + full_list[i] + "\n";
        }
        $("score_list").value = list;
   
}
	
window.onload = function () {
    $("add_score").onclick = add_click;
	$("clear_scores").onclick = clear_click;
	$("last_sort").onclick = last_click;
    $("last_name").focus();
}



Attachment: student_scores.zip (1866 Bytes | downloaded 74 times)



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.