I have the following code:
<script type="application/javascript">
$(document).ready(function () {
$("#market_value").on("change paste keyup", function() {
var market_value = parseInt($(this).val());
var data = [
{"percent":"10","market_value":"10000"},
{"percent":"20","market_value":"10001"},
{"percent":"25","market_value":"20000"}
];
for (var x in data) {
if(market_value >= parseInt(data[x].market_value)) {
console.log(percentage(market_value, data[x].percent));
}
};
});
function percentage(num, per)
{
return (num/100)*per;
}
});
</script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="form-control" id="market_value" name="market_value" value="">
I just need a simple code to calculate the percentage of the market_value based on the JSON data list.
For example:
If I type 8000 result must be 800 (10% based on the "percent" field).
If I type 11000 result must be 2200 (20% based on "percent" field because is highest than 10001)
If I type 50000 result must be 12500 (25% based on "percent" field because is highest than 20000)
If I type 3000 result must be 300 (10% based on "percent" field because is NOT highest than 10000)
The code I made is working in some way but if I type a number lower than 10000 then nothing shows.
These are not fixed values, the data (JSON) is dynamic.