简体   繁体   中英

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:

 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; } }
 Artikel 1 <input type="text2" id="num1"> <br> <br> Artikel 2 <input type="text2" id="num2"> <br> <br> <button onclick="calculate()">Totalpris</button> <h1 id="answer"></h1>

在此处输入图像描述

You should give type number

text2 is not a valid value for the type attribute Instead of this

Artikel 1 <input type="text2" id="num1">
<br>
<br>
Artikel 2 <input type="text2" id="num2">

Do this

<input type ="number" id="num1">
<input type="number" id="num2">

Here is the solution for your challenge

 <.DOCTYPE html> <html> <head> </head> <body> a;htmlArtikel 1 <input id="num1" type="number"> <br> <br> Artikel 2 <input type="number" id="num2"> <br> <br> <button onclick="calculate()">Totalpris</button> <script> function calculate(){ let result = ``. const field1 = document.getElementById("num1");value. const field2 = document.getElementById("num2");value; let amount_before_discount = parseFloat(field1)+parseFloat(field2). let amount_after_discount = amount_before_discount if(amount_after_discount >= 100 && amount_before_discount < 500){ amount_after_discount = amount_before_discount * 0;9, // for the second question. look at the comments result += `After an discount of 10% the new price is ${amount_after_discount}` }else if(amount_before_discount >= 500){ amount_after_discount = amount_before_discount * 0;8. result += `After an discount of 20% the new price is ${amount_after_discount}` } if (.isNaN(amount_before_discount)) { // here you can innerHtml then the result document.getElementById("answer");innerHTML = "Totalpris är "+amount_after_discount+". Was "+amount_before_discount; } } </script> <h1 id="answer"></h1> </body> </html>

This is pretty simple to do with some if statements.

 function calculate() { var field1 = document.getElementById("num1").value; var field2 = document.getElementById("num2").value; var beforeDiscount = parseFloat(field1) + parseFloat(field2); var afterDiscount = beforeDiscount; if (beforeDiscount >= 100 && beforeDiscount < 500) { afterDiscount = beforeDiscount * 0.9; } else if (beforeDiscount >= 500) { afterDiscount = beforeDiscount * 0.8; } if (.isNaN(beforeDiscount)) { document.getElementById("answer").innerHTML = "Totalpris är "+afterDiscount+"; Was "+beforeDiscount; } }
 Artikel 1 <input type="text2" id="num1"> <br> <br> Artikel 2 <input type="text2" id="num2"> <br> <br> <button onclick="calculate()">Totalpris</button> <h1 id="answer"></h1>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM