简体   繁体   中英

Javascript - discount sum

I need help with my code. I should have 2 boxes for pricees And if sum is below 50 a message show SUM is xx - "no discount." if between 50-300 message show SUM is xx - "10% discount" if 300 or more message show SUM is xx - "20% discount"

I just can´t get this work. Also i want the third input to be closed so no input could be inserted.

```

<script>
  function myCalculator(a,b){

    var c=parseInt(a)+parseInt(b);
    document.getElementById('text').value = c;

    if ( c >= 100 {
      text="no discount less than 100!";
    }
    else if (c<100 && total) 500 ) {
      text= "total=(a+b), 10% doscount. New price xx";
    }
    else if (c<500) {
      text= "total=(a+b), 20% doscount. New price xx";
    }
</script>

<h1>BUY</h1>
<input type="text" value="" id="text1"></input> +
<input type="text" value="" id="text2"></input> =
<input type="text" value="" id="text3"></input>
<input type="button" value="TOTALPRICE" 
 onclick='myCalculator(document.getElementById("text1").value,
 document.getElementById("text2").value)'></input>
  • You have syntax issue in script
  • you're not printing the text
  • you're not calculating the discounted value.

Here is the jsfiddle

function myCalculator(a, b) {
  var c = parseInt(a) + parseInt(b);
  var text = '';
  document.getElementById('text3').value = c;
  if (c < 50) {
      text = "no discount less than 50!";
  }
  else if (c >= 50 && c <= 300) {
      text = "total=(a+b), 10% doscount. New price: " + Math.round(c * 0.9);
  }
  else if (c > 300) {
      text = "total=(a+b), 20% doscount. New price:" + Math.round(c * 0.8);
  }
  document.getElementById('text4').innerHTML = text;
}

Firstly, there were a lot of errors in your code but I corrected them. You need to be careful while defining ids' of tags. If you have any questions about the corrected version of the code, you can ask whenever you want.

    <h1>BUY</h1>
    <input type="number" value="" id="text1"></input> + 
    <input type="number" value="" id="text2"></input> = 
    <input type="text" value="" id="text3"></input>
    <input type="button" value="TOTALPRICE" onclick='myCalculator();'></input>
<script>
    function myCalculator(){
      let c= parseInt(document.getElementById("text1").value);
      let d = parseInt(document.getElementById("text2").value);
      let sum = c + d;
      document.getElementById('text3').value = sum;
      if ( sum < 50) {
          window.alert("Your total is " + sum + ". No discount less than 50!");
      }
      else if (sum <= 300 && sum > 50 ) {
          let newPrice = sum * 0.9;
          window.alert("Your total is " + sum + ". 10% discount. New price is " + newPrice);
      }
      else if (sum > 300) {
          let newPrice = sum * 0.8;
          window.alert("Your total is " + sum + ". 20% discount. New price is " + newPrice);
      }
    }
</script>

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