3

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?

3
  • Your problem is here: for(let i = 0; i < sum; i++){ array[i] ++ }. You're indexing your array up to the value of sum-1 instead of maximum of length-1. If you want to add to the array from the last element to the first, you can reverse your loop, for (i = last; i>= 0; i--) ... Commented Jul 7, 2019 at 20:00
  • Thanks for the comment... Can you please provide an answer... Commented Jul 7, 2019 at 20:01
  • It's not clear the order you want. Your test says: [0, 0, 0, 0 ,1 ,1, 1] your code says: [1, 1, 1, 0, 0, 0, 0] Commented Jul 7, 2019 at 20:12

4 Answers 4

6

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));

Sign up to request clarification or add additional context in comments.

Comments

4

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());

2 Comments

Thanks for the answer... I know where the problems are... But I need help to fix them... plus one...
The added function will add share the value of sum between the number of length items in the array.
2

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));

Comments

1

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.