So I was making this code mostly for fun. Basically it allows the user to enter in the first four values in an array as well as a number that multiplies all of the values in an array by a given number. After that is done the program calls another function to either calculate what the middle number is in an odd array or it finds the average of the two middle numbers in an even array and return that value to the user. It is supposed to do this for the array before it is multiplied as well as to the array after it has been multiplied by the user's number. The code runs fine when it is used on the original array as well as on the modified array when the number of values is odd within the array. It runs into trouble however, when the modified array is even. For some reason instead of returning the average of the two middle numbers it gives NaN for the value. This only happens on the array after it is modified and I was wondering why that was? Here are two copies of my code. The first with an odd number in the array and the second has an even number. Any help would be appreciated. Thanks!
<!DOCTYPE HTML>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>Add Middle Array</title>
<script type="text/javascript">
function collectdata() {
var list = [0, 0, 0, 0, 5];
list[0] = parseFloat(document.getElementById("pokemon").value);
list[1] = parseFloat(document.getElementById("digimon").value);
list[2] = parseFloat(document.getElementById("transformers").value);
list[3] = parseFloat(document.getElementById("workers").value);
var multiply = parseFloat(document.getElementById("multi").value);
var outcome = multiplier(list, multiply);
var spaceoutcome = outcome;
for (i = 0; i < outcome.length; i++) {
spaceoutcome[i] = " " + spaceoutcome[i];
}
document.getElementById("output1").innerHTML = spaceoutcome;
var sumofmiddle = sum(list);
document.getElementById("output2").innerHTML = sumofmiddle;
var sumofmodmid = sum(outcome);
document.getElementById("output3").innerHTML = sumofmodmid;
}
function multiplier(L, M) {
var money = [];
for (i = 0; i < L.length; i++) {
money[i] = (L[i] * M);
}
return money;
}
function sum(L){
var outcome = 0;
if (L.length % 2 == 0)
{
outcome = ((L[L.length / 2] + L[((L.length / 2) - 1)]) / 2);
}
else
{
outcome = L[((L.length - 1) / 2)];
}
return outcome;
}
</script>
</head>
<body>
Please enter in a list of four values to be muliplied with the final value always being 5. <br>
<input type="text" id="pokemon"><br>
<input type="text" id="digimon"><br>
<input type="text" id="transformers"><br>
<input type="text" id="workers"><br><br>
Please enter the multiplier.<br><br>
<input type="text" id="multi"><br><br>
<button type="button" onclick="collectdata()">Click here to multiply values</button>
<div id="output1"></div><br><br>
Bonus! Here is the average of the middle numbers of the arrays!<br>
<div id="output2"></div><br><br>
Final Surprise! Here is the average of the multiplied modified numbers!<br>
<div id="output3"></div>
</body>
</html>
Here is the even numbered array that is malfunctioning.
<!DOCTYPE HTML>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>Add Middle Array</title>
<script type="text/javascript">
function collectdata() {
var list = [0, 0, 0, 0, 5, 6];
list[0] = parseFloat(document.getElementById("pokemon").value);
list[1] = parseFloat(document.getElementById("digimon").value);
list[2] = parseFloat(document.getElementById("transformers").value);
list[3] = parseFloat(document.getElementById("workers").value);
var multiply = parseFloat(document.getElementById("multi").value);
var outcome = multiplier(list, multiply);
var spaceoutcome = outcome;
for (i = 0; i < outcome.length; i++) {
spaceoutcome[i] = " " + spaceoutcome[i];
}
document.getElementById("output1").innerHTML = spaceoutcome;
var sumofmiddle = sum(list);
document.getElementById("output2").innerHTML = sumofmiddle;
var sumofmodmid = sum(outcome);
document.getElementById("output3").innerHTML = sumofmodmid;
}
function multiplier(L, M) {
var money = [];
for (i = 0; i < L.length; i++) {
money[i] = (L[i] * M);
}
return money;
}
function sum(L){
var outcome = 0;
if (L.length % 2 == 0)
{
outcome = ((L[L.length / 2] + L[((L.length / 2) - 1)]) / 2);
}
else
{
outcome = L[((L.length - 1) / 2)];
}
return outcome;
}
</script>
</head>
<body>
Please enter in a list of four values to be muliplied with the final value always being 5. <br>
<input type="text" id="pokemon"><br>
<input type="text" id="digimon"><br>
<input type="text" id="transformers"><br>
<input type="text" id="workers"><br><br>
Please enter the multiplier.<br><br>
<input type="text" id="multi"><br><br>
<button type="button" onclick="collectdata()">Click here to multiply values</button>
<div id="output1"></div><br><br>
Bonus! Here is the average of the middle numbers of the arrays!<br>
<div id="output2"></div><br><br>
Final Surprise! Here is the average of the multiplied modified numbers!<br>
<div id="output3"></div>
</body>
</html>