IF to check quantity needed for given KG

Asked

Viewed 69 times

4

I need to do an IF in Javascript. I will explain:

A customer sells:

  • Barrels of 40kg.
  • Tube of 3 kg.

I don’t know how to do this IF. The idea is, show in the field qtd40kg Only multiples of 40, which is the weight, ie:

If I need 40k in the field shows 1, 80kg shows 2, 120kg shows 3, that’s because they are multiples of 40.

Now in the field qtd3kg is a little different, it is multiples of 3kg that will fill the gap between qtd40kg.

Link https://jsfiddle.net/2w84ec18/

Ex:

qtd40kg

40kg  -> qtd40kg = 1un;
80kg  -> qtd40kg = 2un;
120kg -> qtd40kg = 3un;
160kg -> qtd40kg = 4un;
.
.
.
Até 1.000kg

qtd3kg

0kg -> qtd3kg = 0un;
3kg -> qtd3kg = 1un;
3kg -> qtd3kg = 1un;
9kg -> qtd3kg = 2un;
.
.
.
Até 30kg, depois ele irá fazer assim:

Let’s assume that:

peso = 43;
Então ``qtd40kg = 1`` e ``qtd3kg = 1``.

peso = 46;
Então ``qtd40kg = 1`` e ``qtd3kg = 2``.

peso = 49;
Então ``qtd40kg = 1`` e ``qtd3kg = 3``.


peso = 126;
Então ``qtd40kg = 3`` e ``qtd3kg = 2``.

Link https://jsfiddle.net/2w84ec18/

Can anyone help me? This is a very high level for me.

I appreciate any help.

  • Hello @Bacco, edirei. I think better in JS. Thanks.

  • 1

    It got better. And if the person puts 5kg, or some other value that doesn’t give multiples, like 25kg, what is to happen?

  • @Bacco If it is 5kg it would be 2 tubes of 3kg. 25kg would be 9 tubes of 3kg. and in the field qtd40kg is ZERO, because the quantity is not equal to more than 40kg. Only from 40kg the qtd40kg begins to receive value.

  • You want for example: 0 to 39 = 0, 40 to 79 = 1, ... ? It’s like this?

  • @Bacco the function of qtd3kg is to fill the space that the 40kg barrels leave. From 40kg up to 80kg, has several kg to be filled. The 3kg tube is to fill this space, maximizing the amount of mass.

  • And if the person wants 42, is a 40 and a 3, with a 1 kg error, or is 14 of 3, giving certinho 42?

  • 1

    @Bacco let’s assume you need 60kg of dough, more than that would throw away. If you only had a 40-pound barrel, then you’d have to buy 80 kilos, and 20 kilos would get lost. With the help of the 3kg tubes, it is possible to get as close as possible to the customer’s need. In this example of 60kg would be (1 of 40kg and 7 of 3kg), totaling 61kg. In this case the loss is only 1kg.

  • 1

    @Bacco, the tubes are more expensive, so can not direct. In your case above would be like this: Purchased 42kg would be 1un of 40kg and 1un of 3kg.

Show 3 more comments

2 answers

6


I think it’s very simple, you can test it right here:

function calc() {
  var peso = document.getElementById( 'peso' ).value;
  var b40  = Math.floor( peso / 40 );
  var b3   = Math.ceil( ( peso - b40 * 40 ) / 3 );
  
  document.getElementById( 'qtd40kg' ).value = b40;
  document.getElementById( 'qtd3kg'  ).value = b3;
}
Peso:<br>
<input type="number" id="peso">

<p>QTD Barricas de 40kg:<br>
  <input type="number" id="qtd40kg">

<p>QTD bisnagas de 3kg:<br>
  <input type="number" id="qtd3kg">

<p><input type="button" id="calcular" value="Calcular" onclick="calc();">

3

An option with Jquery:

var barricas = $('#qtd40kg');
var bisnagas = $('#qtd3kg');

$('#peso').on('input',	function(){
var pesoTotal = $(this).val();
var x = Math.floor(pesoTotal%40);
var y = x/3;
barricas.val(Math.floor(pesoTotal/40))
bisnagas.val(Math.floor(y))
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Peso:<br>
  <input type="number" id="peso">
  
  <p>QTD Barricas de 40kg:<br>
  <input type="number" id="qtd40kg">
  
  <p>QTD bisnagas de 3kg:<br>
  <input type="number" id="qtd3kg">
  
  <p><input type="button" id="calcular" value="Calcular">

Browser other questions tagged

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