Javascript discount for price classes

Ive been trying to add a discount to price classes for a couple of days now but haven’t been able to. The current js code I have is a simple addition calculator, but I want it to discount prices between 100 and 500 with 10% and prices over 500 get 20% discount. I also want it to show the price before and after the discount if possible.

The code I have for the calculator so far, its working fine:

Artikel 1 <input type="text2" id="num1">
Artikel 2 <input type="text2" id="num2">
<button onclick="calculate()">Totalpris</button>


function calculate ()
  var field1=document.getElementById("num1").value;
  var field2=document.getElementById("num2").value;

  var result=parseFloat(field1)+parseFloat(field2);

if (!isNaN(result))
document.getElementById("answer").innerHTML="Totalpris är "+result;



<h1 id="answer"></h1>

Try to write the code that discounts the price based on a range between 100 and 500 (inclusive). You could use an if statement to accomplish this. I suggest creating a discount function that you can call on any value you pass to it and it will return the discounted price based on the code you write for it. That way you can apply it to each price field (i.e. num1 and num2.

Thanks for the fast reply. I have been trying a lot of codes and have only been using JavaScript for a couple of days so I don’t know a lot about the discount function. I have also been using this if statement:

if ( totpr >= 100 && totpr < 500 ) {

but I don’t know much more than this :frowning:

I do not see a totpr variable declared anywhere in your code. I do see a result variable which you could use to compare to the 100 and 500 values. Then, it is just a matter of calculating the discount.

Tell me how you would manually calculate the discounted total if the total of the two fields was $200. If you can describe this process, then you can code it.