Reputation: 3543
I want some help to modify or create a function to do a simple task;
Here is my function:
function arra(sum, length){
var array = [];
for(let i = 0; i < length; i++) {
array.push(0);
}
//console.log(array)
for(let i = 0; i < sum; i++){
array[i] ++
}
return array;
}
console.log(arra(0, 7));
console.log(arra(3, 7));
console.log(arra(7, 7));
console.log(arra(10, 7));
Here first, we create an array with a length
, then we push 0
to each element of it. so the array will be : [0, 0, 0, 0 ,0 ,0 ,0]
Then based on the value of sum
I want to add 1 from the last element of the array
to the beginning. The problem is I want to come back to the first element and keep adding 1 again if there is enough sum
.
So if I execute this: arra(3, 7);
I would have : [0, 0, 0, 0 ,1 ,1, 1]
if I execute this: arra(7, 7)
I would have: [1, 1, 1, 1 ,1 ,1 ,1]
But the problem is when I execute this: arra(10, 7)
I get : [1, 1, 1, 1, 1, 1, 1, NaN, NaN, NaN]
that is unexpected . I want to have : [1, 1, 1, 1 ,2 ,2 ,2]
Another problem is I can't add to array from the last element to the first...
Edit: How can fix it guys?
Upvotes: 3
Views: 1445
Reputation: 386680
You could calculate the min and max values for dispersing and get the length of the right part as well.
function getArray(sum, length) {
var max = Math.ceil(sum / length),
min = Math.floor(sum / length),
l = sum - min * length;
return Array.from({ length }, (_, i) => length - i > l ? min : max);
}
console.log(...getArray(0, 7));
console.log(...getArray(3, 7));
console.log(...getArray(7, 7));
console.log(...getArray(10, 7));
console.log(...getArray(20, 7));
Upvotes: 2
Reputation: 834
You can just start from the end and reset back to end when you reach 1 the first element.
function arra(sum, length){
var array = [];
for(let i = 0; i < length; i++) {
array.push(0);
}
let x = length -1;
while(sum > 0) {
if(x < 0) { // go back to the end
x = length -1;
}
array[x]++;
x--;
sum--;
}
return array;
}
console.log(arra(0, 7));
console.log(arra(3, 7));
console.log(arra(7, 7));
console.log(arra(10, 7));
Upvotes: 1
Reputation: 2694
You get [1, 1, 1, 1, 1, 1, 1, NaN, NaN, NaN]
this due to you initialize the array with the value of length, in this case you are calling arra(10,7)
for(let i = 0; i < length; i++) {
array.push(0);
}
Here you are getting an array of 7 elements initialized to 0
[0,0,0,0,0,0,0]
Next you execute:
for(let i = 0; i < sum; i++){
array[i] ++
}
You get 1 in the first 7 elements you initialized to 0, but the next 3 you get NaN
Why?
Due to array[i]
is undefined
and; undefined
with the operator ++
coerces to NaN
.
If you want to create a circular array:
let array = null;
function circularArray(sum, length) {
array = Array(length).fill(0);
for (let count = 0; count < sum; count++) {
array[count % array.length]++;
}
}
circularArray(10, 7);
console.log(array.reverse());
Upvotes: 4
Reputation: 92460
If you want to "wrap around" you can index the array with the modulus of the length: array[i%length]
. If you want to start at the other end, you can subtract from the array[length - 1 - i % length]
— it wasn't clear to me which order you wanted, but the same idea works either way:
function arra(sum, length){
var array = Array(length).fill(0)
//console.log(array)
for(let i = 0; i < sum; i++){
array[length - 1 - i % length] ++
}
return array;
}
console.log(arra(0, 7));
console.log(arra(3, 7));
console.log(arra(7, 7));
console.log(arra(10, 7));
Upvotes: 6