# Discrepancy in Array Addition

Discussion in 'Javascript' started by Michael Winter, Oct 29, 2006.

1. ### Michael WinterGuest

Randy Webb wrote:

[snip]
You're adding the index to the total, not the value from the array:

k += myArray[currentBoard];

[snip]

Mike

Michael Winter, Oct 29, 2006

2. ### ASMGuest

Randy Webb a écrit :
the r control varies from approx 597 to 645
normal feature :
adding a unknown random number to itself while smaller than ...

the newArray length would have to reflect this r (length = 33 + r)

function Random(x) { return Math.floor(x*Math.random()) }
myArray = [4,4.25,4.5,4.75,5,5.25,5.25,
5.5,5.5,5.75,5.75,6,6,6.25,
6.25,6.5,6.5,6.75,6.75,7,7,
7.25,7.25,7.5,7.5,7.75,7.75,
8,8,8.25,8.5,8.75,9]
var m = myArray.length;

var CtrolArray = new Array();
maxTotal = 10000;
k = 0;
r = 0;
document.write('newArray = [')
while (k <= maxTotal){
var separator = k==(maxTotal)? '' : ',';
var currentBoard = Random(33);
document.write(myArray[currentBoard] + separator);
CtrolArray[CtrolArray.length] = currentBoard;
k = k+currentBoard
r++
}
document.write('<br>Original length = '+m+'<br>');
document.write('<br>k = '+k+'<br>');
document.write('<br>r = '+r+'<br>');
document.write('myArray Length = '+myArray.length+'<br>');
document.write('Control Length = '+CtrolArray.length+'<br>');
document.write('Last entry in myArray =
'+myArray[CtrolArray[CtrolArray.length-1]]+' - its index =
'+CtrolArray[CtrolArray.length-1]+'<br>');
document.write('Last control k = '+CtrolArray[k]+'<br>');

ASM, Oct 29, 2006

3. ### Randy WebbGuest

This code is supposed to create an array that has enough entries to add
up to 10000 using an Array of numbers. The middle numbers in the Array
are weighted towards the middle.

function Random(x) { return Math.floor(x*Math.random()) }
myArray = [4,4.25,4.5,4.75,5,5.25,5.25,
5.5,5.5,5.75,5.75,6,6,6.25,
6.25,6.5,6.5,6.75,6.75,7,7,
7.25,7.25,7.5,7.5,7.75,7.75,
8,8,8.25,8.5,8.75,9]

maxTotal = 10000;
k = 0;
r = 0;
document.write('newArray = [')
while (k <= maxTotal){
var currentBoard = Random(33)
document.write(myArray[currentBoard] + ',')
k = k+currentBoard
r++
}
document.write('0];')

//document.write('<br>' + k)
//The above line was to make sure I was getting
//the maxTotal.
//The goal is to be able to create a random array
//where the elements of the array add up to maxTotal
//or over 10000 by one element.

If I uncomment the document.write line I am getting a number just larger
than maxTotal. Perfectly acceptable. Then, if I take the array it
creates and copy/paste it into a new file, then loop through the array
adding entries I don't get the same result.

newArray = [........];
r = 0;
for (i=0;i<newArray.length;i++){
r = r + newArray
}
document.write(r)

What I expected to get would be the same results as k in the first
document.write statement but I don't. It is 5:30AM and my eyes are
crossed trying to find what I am missing that explains why they aren't
adding up the same...

If someone will kindly point out what I am missing I will probably think
"Geez, Randy, you are stupid for missing that" but I would be grateful

Randy Webb, Oct 30, 2006
4. ### Randy WebbGuest

Michael Winter said the following on 10/29/2006 6:19 AM:
I found that about 6:30 this morning and typed a reply and never sent
it. But thank you and I will say it:

"Geez, Randy, you are stupid for missing that!"

Thanks Michael.

Randy Webb, Oct 31, 2006