Reputation: 754
I have the need to calculate a price based on the number of words in a textarea. I will do my best to explain:
Flat fee = $35
Per prompt fee = $10
Per word fee = $.50 (after 30 words)
So I have the following textarea:
<textarea rows="10" class="form-control" name="voice-prompt-1" id="voice-prompt-1" placeholder="Type or paste your prompt here."></textarea>
<p>You have <span class="wordcount">0</span> words.</p>
And the following JS:
var perWord = '.50';
var flatFee = '35.00'
var promptPrice = '10.00';
function wordCount( val ){
return {
words : val.match(/\S+/g).length
}
}
var div = $('span.wordcount');
jQuery('textarea#voice-prompt-1').on('input', function($) {
var count = wordCount( this.value );
div.html(count.words);
additionalFees = perWord*count.words;
total = parseFloat(flatFee) + parseFloat(promptPrice) + parseFloat(additionalFees);
$('span.total').html(parseFloat(total.toString()).toFixed(2));
console.log(total);
});
My question is how do I get the perWord
value to calculate only after 30 words is entered?
Upvotes: 1
Views: 145
Reputation: 5890
You want to first check that you have at least 30 words then, subtract 30 form the word count to get additiona fees.
if (count.words > 30) {
additionalFees = perWord * (count.words - 30);
}
Upvotes: 1