IF to check quantity required for given KG

4

I need to make an IF in Javascript. I'll explain:

A customer sells:

  • 40kg barrels.
  • 3kg tube.

I'm not sure how to do this IF. The idea is to 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, this is because multiples are 40.

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

Link link

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:

Suppose 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 link

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

Thanks for any help.

    
asked by anonymous 16.01.2016 / 23:55

2 answers

6

I think it's very simple to do this, 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();">
    
17.01.2016 / 00:18
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><inputtype="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">
    
17.01.2016 / 00:22