Sara Ree
Sara Ree

Reputation: 3543

Fill an array with ascending sequence of numbers with conditions

How do I fill an array with an ascending sequence of numbers with these conditions :

here is the code I've created :

    var length = 10;
    var array = []; 
    var sum = 10; // the sum of all elements in the array should be equal to this
        
    for (let i = 0; i < length; i++) {
        let a = 0; // the very first element of array
        // Math equations
        let last = (sum - (length / 2 * a)) * 2 / length
        let dd = (last - a) / (length - 1)
    
        sume = (dd * (i));
        array.push(sume);
    
    }

    // check to see if array elemements sum is equal to "var sum = 10"
    let reducer = (accumulator, currentValue) => accumulator + currentValue;
    let sumArray = array.reduce(reducer);
    console.log("sumArray: " + sumArray)
    
    console.log(array) // results

But the problem is instead of having round numbers (it still matches all my conditions) like this :

[0, 0, 0, 0, 1, 1, 1, 2, 2, 3]

But I get these results:

[0, 0.2222222222222222, 0.4444444444444444, 0.6666666666666666, 0.8888888888888888, 1.1111111111111112, 1.3333333333333333, 1.5555555555555554, 1.7777777777777777, 2]

Note: I calculated the last item here : let last = (sum - length / 2 * a) * 2 / length and the first one is always 0...

Upvotes: 1

Views: 319

Answers (2)

Slai
Slai

Reputation: 22896

sum / length is the middle value in the array
sum / length * 2 is the last value in the array
/ (length - 1) * index is to get percentage of the last value based on the index
and + .5 | 0 is short for Math.round

const sequence = (L, S) => [...Array(L)].map((v, i) => S / L * 2 / (L - 1) * i + .5 | 0)

const test = (L, S, arr = sequence(L, S)) => arr.reduce((a, b) => a + b) + ' : ' + arr

console.log( test(10, 10) )
console.log( test(10, 30) )
console.log( test(20, 50) )

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386883

You could round the values.

sume = Math.round(dd * i);

    var length = 10;
    var array = []; 
    var sum = 10; // the sum of all elements in the array should be equal to this
        
    for (let i = 0; i < length; i++) {
        let a = 0; // the very first element of array
        // Math equations
        let last = (sum - length / 2 * a) * 2 / length
        let dd = (last - a) / (length - 1)
    
        sume = Math.round(dd * i);
        array.push(sume);
    
    }

    // check to see if array elemements sum is equal to "var sum = 10"
    let reducer = (accumulator, currentValue) => accumulator + currentValue;
    let sumArray = array.reduce(reducer);
    console.log("sumArray: " + sumArray)
    
    console.log(array) // results

A shorter approach with rounding (it might nott work for all cases).

const add = (a, b) => a + b;

function disperse(length, sum) {
    var min = 0,
        max = sum * 2 / length,
        array = Array.from({ length }, (_, i) => Math.round(min + i * max / (length - 1)));
    return [array.reduce(add), array.join(', ')];
}

console.log(disperse(10, 10));
console.log(disperse(10, 30));
console.log(disperse(20, 50));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 2

Related Questions