I am trying to make a calculator that calculates the amount of words inputted (words) and multiplying it with a variable decided by an If/Then statement. The way I want it to work is that if there is 1-10,000 inputted it multiplies whatever that amount is by .29, and if there is 10,000-20,000 by .26, and anything more than 20,000 by .24. This is what I have come up with so far:
<script type="text/javascript">
function wordcount(){
var a = document.getElementById('words');
if(a <= 10000){price = 0.29} else {price = 0.26};
var wc = parseFloat(a.value);
var final = wc*price;
alert('Your estimated price is: $' + final);
}
</script>
I can’t get it to work at that point alone. If I had, I would have added another condition for 10k-20k. What am I doing wrong?
Sorry if this has already been answered in a similar way or if this is a very simple mistake on my part. I am very new to JS. Thanks in advance for the help.
You need to convert the string to a number value before you see how big it is-
and format the final cost to two decimal places with toFixed(2).
Make sure ‘words’ is an id, and not name=‘words’
If you don’t have fractions of words you can use parseInt(a.value,10) instead of parseFloat
function wordcountprice(){
var price, final,
a=document.getElementById('words'),
wc= parseFloat(a.value) || 0;
if(wc<= 10000) price= .29;
else price= wc<=20000? .26: 24;
final= (wc*price).toFixed(2);
alert('Your estimated price is: $' + final);
}