Beispiel:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS-Demo for nested if-conditions</title>
</head>
<body>
<h3>JS-Demo for nested if-conditions </h3>
<p>
Example for variable discount depending on price; the bigger the price, the bigger the discount
</p>
<p>
If price <= 5000 then discount = 5% <br/>
If price between 5000 and 15000 then discount = 25% <br/>
If price > 30000 then discount = 40% <br/>
</p>
<script>
// idea from: https://www.includehelp.com/code-snippets/nested-if-example-in-javascript.aspx
"use strict";
const listPrice = Number(prompt("How much ist the price?"));
let discount = 0.0;
if (listPrice > 0) {
if (listPrice > 5000) {
if (listPrice > 15000) {
if (listPrice > 30000) {
discount = listPrice * 0.4;
} else {
discount = listPrice * 0.25;
}
} else {
discount = listPrice * 0.15;
}
} else {
discount = listPrice * 0.05;
}
const actualPrice = listPrice - discount;
alert("Discount: " + discount);
alert('Actual Price: ' + actualPrice);
} else {
alert('Invalid List Price!');
}
</script>
</body>
</html>