Add value to my contact for each multiples of 200 without having to create individual variables

Asked

Viewed 83 times

0

I am creating a numerical converter. but due to my knowledge limited, I cannot program it so that every time it reaches a multiple of 200 in the counter, 4 is added in the result.

I even got a result by creating an individual variable for each multiple, but I believe that is not the most appropriate form.

And also my accountant is computing negative values.

<!DOCTYPE html>
<html>
<title>Conversor</title>
<body>

<h2>Conversor</h2>
<p>Conversor:</p>

<p>
  <label>valor</label>
  <input id="inputValor" type="number" placeholder="valor" oninput="Conversor(this.value)" onchange="Conversor(this.value)">
</p>
<p>Resultado: <span id="outputValor"></span></p>

<script>
function Conversor(valNum) {

  var kwh = valNum; 
  var gre;
  if (kwh >= 200) {
    gre= 4;
  } else {
    gre= 2;
  }  
  var b2 = valNum;
 
  var add; 
  if (b2 >= 200) {
    add = 4;
   } else {
    add = 0;
  
   }
   
     var add2; 
  if (b2 >= 400) {
    add2 = 4;
   } else {
    add2 = 0;
  
   }
   
   document.getElementById("outputValor").innerHTML=valNum*gre+add+add2;
}

</script>
</body>
</html>

  • What error appears in?

  • Good morning Ronaldo. It’s running, only I’d like to know if you have any better method than the one I did, because that way I’ll have to make a variable for each multiple of 200. to add 4 to the result.

3 answers

1


There are too many variables there. You could use the conditional operator instead of the if. I could simplify a little more, but in the background it would be worse, so I left it so. I also organized the code better.

<!DOCTYPE html>
<html>
<title>Conversor</title>
<body>

<h2>Conversor</h2>
<p>Conversor:</p>

<p>
  <label>valor</label>
  <input id = "inputValor" type = "number" placeholder = "valor" oninput = "Conversor(this.value)" onchange = "Conversor(this.value)">
</p>
<p>Resultado: <span id = "outputValor"></span></p>

<script>
function Conversor(valNum) {
    var gre = valNum >= 200 ? 4 : 2;
    var add = valNum >= 200 ? 4 : 0;
    var add2 = valNum >= 400 ? 4 : 0;
    document.getElementById("outputValor").innerHTML = valNum * gre + add + add2;
}

</script>
</body>
</html>

I put in the Github for future reference.

AP now says it wants something else, who knows what it wants (note that it does not do the same thing):

<!DOCTYPE html>
<html>
<title>Conversor</title>
<body>

<h2>Conversor</h2>
<p>Conversor:</p>

<p>
  <label>valor</label>
  <input id = "inputValor" type = "number" placeholder = "valor" oninput = "Conversor(this.value)" onchange = "Conversor(this.value)">
</p>
<p>Resultado: <span id = "outputValor"></span></p>

<script>
function Conversor(valNum) {
    valNum = parseInt(valNum); //não estou tratando se vai dar erro igual ao que foi feito antes
    document.getElementById("outputValor").innerHTML = (valNum * ((Math.trunc(valNum / 200) + 1) * 2)) + valNum + (Math.trunc(valNum / 200) * 4);
}

</script>
</body>
</html>

I put in the Github for future reference.

  • Good morning I appreciate attention, but my doubt is whether I would just make a single var add, and the same recognize the multiples of 200 and add +4 or will I have var add manually for each multiple (example: var add3 = valNum >= 600 ? 4 : 0;)

  • 2

    But your code doesn’t do that. You put in a code, you say it’s working and you just want to improve it. I made the improvement based on your code. If he is wrong and does not do what he wishes, he should not say what he said. The answer you get is according to your question. If you want something else and you don’t say it, there’s no way people can guess. Now you’re saying you want something completely different than what’s in your code. Who’s to say that after I do it this way it won’t happen to be something else you hadn’t noticed?

  • I did not mean to ask the question wrongly, I thank you for your patience and I hope that the topic will help other people.

  • I know you didn’t mean it.

0

Here’s an example of how to do this:

<script>
function Conversor(valNum) {

  var kwh = valNum; 
  var gre;
  if (kwh % 200 == 0) {
    gre= 4;
  } else {
    gre= 2;
  }  
  var b2 = valNum;
 
  var add; 
  if (b2 % 200 == 0) {
    add = 4;
   } else {
    add = 0;
  
   }
   
  var add2; 
  if (b2 % 200 == 0) {
    add2 = 4;
   } else {
    add2 = 0;
  
   }
   console.log(gre);
   var resultado=valNum*gre+add+add2;
   //document.getElementById("outputValor").innerHTML=resultado;
   console.log(resultado);
}
Conversor(1600);
</script>
</body>
</html>

0

Correct me if I’ve got it wrong, but try this:

function Conversor(valNum){
    var gre = valNum >= 200 ? 4 : 2;
    var soma = parseInt(valNum / 200)
    return soma * 4 + valNum * gre;
}
  • It didn’t work, is that the Var "Gre" is to change the converter multiplier. it starts *2 and when reaching 200 it gets *4, that part ta ok. The problem is that I want to add +4 to every multiple of 200 (added in input). without correlation with var Gre.

  • I edited the code, see if that’s it please.

Browser other questions tagged

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