JavaScript - closure Tutorial Points (4)
Create a separate function using JavaScript closure which accepts the tax percentage and returns a function that accepts the amount and returns the amount after adding tax percentage. Try adding tax percentage to ‘this’ object and check if it works.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="XX-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AF LAB 01 - Ex 04</title>
</head>
<body>
<script>
/*Create a separate function using JavaScript closure which accepts the tax percentage
and returns a function that accepts the amount and returns the amount after adding tax
percentage. Try adding tax percentage to ‘this’ object and check if it works */
let taxPercentage = 0.3;
let vat = function(){
let amount = 1000;
let calculate = function(){
return function(){
amount = amount + amount * taxPercentage;
return amount;
}();
}
return calculate();
}
console.log(vat());
document.write(`<h1>${vat(taxPercentage)}</h1>`);
document.write(`<h1>${taxPercentage}</h1>`);
</script>
</body>
</html>
Output :
Comments
Post a Comment