How do I parseInt the numbers in an array to add them up. Where would I add the parseInt? SO i can then take the arrays summed values and divide by the .length value of the array. Would it be done in where the array is first stated or in the function for the sum? It has not been working for me
<script>
var studentArr = new Array();
var scoreArr = new Array();
function enter() {
var studentName = document.getElementById("inp").value;
studentArr.push(studentName);
var stuval = "";
for(i=0; i < studentArr.length; i++)
{
stuval = stuval + studentArr[i] + "<br/>";
}
document.getElementById("iop").innerHTML = stuval;
var studentScore = document.getElementById("inps").value;
scoreArr.push(studentScore);
var scoreval = "";
for(i=0; i < scoreArr.length; i++)
{
scoreval = scoreval + scoreArr[i] + "<br/>";
}
}
function summ() {
var grade;
var average;
var sum = parseInt(scoreArr.valueOf());
if (scoreArr.valueOf() == 70) {
grade = "C";
document.getElementById("opo").innerHTML = grade;
}
if (scoreArr.valueOf() == 80) {
grade = "B";
document.getElementById("opo").innerHTML = grade;
}
if (scoreArr.valueOf() == 90) {
grade = "A";
document.getElementById("opo").innerHTML = grade;
}
scoreArr.sort(function(a, b){return b-a});
document.getElementById("hop").innerHTML = "Max Score is: <br>" + scoreArr[0];
scoreArr.sort(function(a, b){return a-b});
document.getElementById("lop").innerHTML = "Min Score is: <br>" + scoreArr[0];
average = sum / scoreArr.length;
document.getElementById("aop").innerHTML = average;
}
</script>
Like this:
sum=0;
for (i=1; i<randomArray.length; i++){
sum += parseInt(randomArray[i]);
}
result = sum / randomArray.length;