Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

262
Visualizações
Javascript - How to create an array using a orignal array and a variable to determine how many time a the value of an array can go into the variable

I would like to create an Array stating the number of times the approved quantity goes into the original quantity (vqty) and the approved qty associated with it (aqty). The original quantity decreases after each iteration.

For example - var ApprovedQty = [680,120,450,40]; var OriginalQty = 10000;

var NewArray = [(vqty:aqty),(vqty:aqty),(vqty:aqty),(vqty:aqty)]

use some sort of for loop to generate the NewArray by sorting the original array in descending order and dividing original qty by the value in the array.

ideally its 10000/680 = 14.07 so the first value in NewArray is (14:680). Then Original qty becomes 10000 - (680x14) = 480

then 480 / 450 = 1.06 so the second value in NewArray is (1:450). Then original qty becomes 480 - (450 x1) = 30

then 30 / 120 = .25 ..since 120 is not the last available value in the orginal array, the value in the new array is (0:120) and the original qty stays 30

then 30/40 = .75..since 40 is the last value in the array, the value in the new array is (1:40)

so the newarray is [(14:680),(1:450),(0:120),(1:40)]

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You could map the wanted parts by using a closure which is a function which returns a function and keeps variable total.

The code checks if the the last index is reached and takes ceil as method, otherwise floor and calls it later with the division by the value of the (previously sorted) array.

Then total gets the rest and parts is returned as result.

const
    getParts = total => (value, i, { length }) => {
        const
            method = i + 1 === length ? 'ceil' : 'floor',
            parts = Math[method](total / value);

        total %= value;
        return parts;
    },
    values = [680, 450, 120, 40],
    result = values.map(getParts(10000));

console.log(...result);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda