Return range of numbers according to the division of total numbers by an integer

Asked

Viewed 84 times

0

Given the following situation:

I need to create a function that receives as parameter (int value and int divisor). The idea is popular an array with the TOTAL of the range passed to the function. That I could do. However, I now need to return the range of these values based on the division between the number passed as parameter and the division value. I will illustrate below:

const sort = (totalImage, totalPeople) => {
    let imageList = new Array;
    var result;
    for (let interval = 1; interval <= totalImage; interval++) {
        let intervalValue = parseInt(interval);
        imageList.push(intervalValue);
    }
    result = totalImage / totalPeople;

I tried an approach with the Slice() method, but I was not successful. From now on, I appreciate any cooperation and I hope I have eluded the problem in the best way possible.

  • Your problem is very unclear. 1º - "The idea is popular an array with the TOTAL range passed to the function." Esse intervalo é simplesmente o intervalo de valores que vai de 1 a totalImage, correto? 2º - "However, I now need to return the range of these values based on the division between the number passed as parameter and the division value". Qual a relação, exatamente, que tem entre os elementos desse array e a divisão entre os dois parâmetros da função?

    1. Exactly. 2. So the first parameter would be a total number of images. And the second parameter, for example, would be the division of the total of images by the number of people. Function call example: Sort(20, 4) output: onePerson = 1,2,3,4,5; secondPerson = 6,7,8,9,10; Thirdperson = 11,12,13,14,15; fourthPerson = 16,17,18,19,20;
  • And if the numbers are not an exact division, like Sort(20, 3)?

  • At first, it would be done only with exact division. But in the case of an inaccurate division, the idea was to round down Ceil(), for example.

1 answer

1


Can use .slice() to slice the array according to the number of people (second argument passed to the function) by inserting into a new array the values in each index of that new array. It would be something like the function array_chunk of PHP.

Use the Math.floor() in the division to round down in cases where the division is not exact.

Behold:

const sort = (totalImage, totalPeople) => {
   let imageList = new Array;
   for (let interval = 1; interval <= totalImage; interval++) {
      let intervalValue = parseInt(interval);
      imageList.push(intervalValue);
   }   
   let result = Math.floor(totalImage / totalPeople);

   let sliced = new Array; // cria nova array
   
   for(let x = 0; x < totalImage; x+=result){
      sliced.push(imageList.slice(x, x+result).join()); // aqui fatia e converte em string
   }
   
   return sliced;
}

console.log(sort(20,4));
console.log(sort(20,3));

  • I was on the right track! Thank you so much for the help, Sam! I just didn’t see the need for variable i as a counter.

  • 1

    It’s true. I was testing I ended up forgetting to remove from the code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.