/    Sign up×
Community /Pin to ProfileBookmark

simple function

Hi, im a complete noob at javascript and i need a function that calculates the value of a given number of words for the person to get a aproximate cost of the translation of text. This is for a simple website for my wife she translates some languages etc…
So what i want is that this code returns that cost, it gets the value from every textbox and gives the person the cost. (i will probably just use one textbox):

[CODE] var total = 0;
var idx = 0;
while(fieldObj = document.getElementById(‘num_’+ idx++))
{
total += parseInt(fieldObj.value) * ‘0.06’;

var thecost = “The cost will be about “;
var Euros = ” Euros”;
document.writeln(‘<br/>’ + thecost + total + Euros + ‘<br/>’);
return total;
}[/CODE]

The price per word is like 0.06 cents so for 1000 words it should reply 60, my problem is that the text does not get on the page, the document.writeln shows while you dont press the “return” messagebox that shows. and i dont want no messagebox, i just want the page to show a simple text saying “The cost will be about 60 Euros” and that’s it.

heeeeelp :p

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@WolfShadeMar 16.2011 — Honestly, you shouldn't even have to do that much.

Get the value of the text/textarea.

Use RegEx to replace all instances of 2+ spaces with just one space.

Split the value into an array, using a single space as the delimiter.

Count the length of the array for word count, multiply that by .06, and Voila!

^_^

BETTER YET.. replace all instances of 2+ space AND all whitespaces (tabs, etc.) with a single space. [b]NOW [/b]I'm thinkin'.. ?
Copy linkTweet thisAlerts:
@droidmanauthorMar 16.2011 — Honestly, you shouldn't even have to do that much.

Get the value of the text/textarea.

Use RegEx to replace all instances of 2+ spaces with just one space.

Split the value into an array, using a single space as the delimiter.

Count the length of the array for word count, multiply that by .06, and Voila!

^_^

BETTER YET.. replace all instances of 2+ space AND all whitespaces (tabs, etc.) with a single space. [b]NOW [/b]I'm thinkin'.. ?[/QUOTE]


thanks dude but i dont know how to get there. to write that example i gave before took me like 3 hours readinb about javascript :o
Copy linkTweet thisAlerts:
@WolfShadeMar 16.2011 — Working with arbitrary names:
[code=html]
<form name="getQuote" id="getQuote" onsubmit="return transQuote();">
<textarea name="translateTHIS" id="translateTHIS"></textarea>
<input type="submit">
</form>
[/code]

<i>
</i>&lt;script type="text/javascript"&gt;
function transQuote() {
var df = document.forms["getQuote"];
var translationText = df.translateTHIS.value;
translationText = translationText.replace(/[t| {2,}]/g," "); // global replace of all whitespaces OR instances of 2 or more spaces with a single space
var transArray = new Array();
transArray = translationText.split(" "); // This is the array
var tAlen = transArray.length; if(tAlen == 1) { alert("More than one word, please."); return false; }
var thisPrice = parseFloat(tAlen * .06);
alert("The cost will be about " + thisPrice + " Euros.");
return false; // this prevents the form from actually being submit
}&lt;/script&gt;

Untested. Let me know if anything goes horribly wrong. ?

^_^
Copy linkTweet thisAlerts:
@WolfShadeMar 16.2011 — Hang on.. not yet.. I'm having some regex issues with carriage returns.

^_^
Copy linkTweet thisAlerts:
@WolfShadeMar 16.2011 — Alright.. here is the finalized JavaScript function. ?
<i>
</i>function transQuote() {
var df = document.forms["getQuote"];
var translationText = df.translateTHIS.value;
translationText = translationText.replace(/[[r *|rn *]{1,}|t{1,}| {2,}]/g," "); // global replace of all whitespaces OR instances of 2 or more spaces with a single space
translationText = translationText.replace(/^[s|t| ]+|[s|t| ]+$/,""); // trim spaces/tabs from beginning and end of string
var transArray = new Array();
transArray = translationText.split(" "); // This is the array
var tAlen = transArray.length; if(tAlen == 1) { alert("More than one word, please."); return false; }
var thisPrice = parseFloat(tAlen * .06);
alert("The cost will be about " + thisPrice + " Euros.");
return false; // this prevents the form from actually being submit
}

^_^

[color=red]PS. I just noticed that this does not take punctuation into account; if someone types "Hello , is it raining ?", this will see 6 words, not 4. I can make that adjustment, if you really need it.[/color]
Copy linkTweet thisAlerts:
@droidmanauthorMar 16.2011 — Alright.. here is the finalized JavaScript function. ?
<i>
</i>function transQuote() {
var df = document.forms["getQuote"];
var translationText = df.translateTHIS.value;
translationText = translationText.replace(/[[r *|rn *]{1,}|t{1,}| {2,}]/g," "); // global replace of all whitespaces OR instances of 2 or more spaces with a single space
translationText = translationText.replace(/^[s|t| ]+|[s|t| ]+$/,""); // trim spaces/tabs from beginning and end of string
var transArray = new Array();
transArray = translationText.split(" "); // This is the array
var tAlen = transArray.length; if(tAlen == 1) { alert("More than one word, please."); return false; }
var thisPrice = parseFloat(tAlen * .06);
alert("The cost will be about " + thisPrice + " Euros.");
return false; // this prevents the form from actually being submit
}

^_^

[color=red]PS. I just noticed that this does not take punctuation into account; if someone types "Hello , is it raining ?", this will see 6 words, not 4. I can make that adjustment, if you really need it.[/color][/QUOTE]


Omg hold on, you made a program that loads the text, counts the words and says the price? THANK YOU but what i was asking was to enter like 1000 (numeric) and make a simple multiply and say "1000 words = 60€" no need to input the text. but since you did that it is really usefull and i never thought of that, so once again, thank you
Copy linkTweet thisAlerts:
@WolfShadeMar 16.2011 — (shrug) I misunderstood your original question. It happens. That would be even easier:
[code=html]
<form name="getQuote" id="getQuote" onsubmit="return transQuote();">
<input type="text" name="totalWords" id="totalWords">
<input type="submit">
</form>[/code]

<i>
</i>&lt;script type="text/javascript"&gt;
function transQuote() {
var df = document.forms["getQuote"];
var translationText = df.totalWords.value;
if(isNaN(translationText)) { alert("Please enter integers only"); return false;}
else {
var thisPrice = parseFloat(translationText * .06);
alert("The cost will be about " + thisPrice.toFixed(2) + " Euros.");
return false; // this prevents the form from actually being submit
}
}
&lt;/script&gt;
^_^
Copy linkTweet thisAlerts:
@WolfShadeMar 16.2011 — I made the first one even better. Now, it removes all punctuation (except for apostrophe) so "Hello , how are you ?" doesn't show up as six words.
<i>
</i>function transQuote() {
var df = document.forms["getQuote"];
var translationText = df.translateTHIS.value;
translationText = translationText.replace(/[,./?;:"!$&amp;#37;^&amp;*()]/g," "); // global replace of all punctuation (except apostrophe) to improve accuracy of count (no "Hello , how are you ?" counting as 6 words)
translationText = translationText.replace(/[[r *|rn *]{1,}|t{1,}| {2,}]/g," "); // global replace of all whitespaces, carriage returns OR instances of 2 or more spaces with a single space
translationText = translationText.replace(/^[s|t| ]+|[s|t| ]+$/,""); // trim spaces/tabs from beginning and end of string
var transArray = new Array();
transArray = translationText.split(" "); // This is the array
var tAlen = transArray.length; if(tAlen == 1) { alert("More than one word, please."); return false; }
var thisPrice = parseFloat(tAlen * .06);
alert("The cost will be about " + thisPrice.toFixed(2) + " u20AC.");
return false; // this prevents the form from actually being submit
}

^_^
Copy linkTweet thisAlerts:
@droidmanauthorMar 17.2011 — THANKS! it works out great but once again, is it possible to show the result on a new line of the document instead of a message box ?
Copy linkTweet thisAlerts:
@WolfShadeMar 17.2011 — Yes. Just create a div where you want it to appear, give it an id, leave it blank.

Replace the following:
<i>
</i>alert("The cost will be about " + thisPrice.toFixed(2) + " u20AC.");


.. with:
<i>
</i>dspMsg = document.getElementById([color=red]'id of div'[/color]);
dsp.innerHTML = "The cost will be about " + thisPrice.toFixed(2) + "&amp;euro;.";

^_^
Copy linkTweet thisAlerts:
@cluefulMar 17.2011 — Translators will usually want to apply a minimum charge. This rounds to the nearest currency unit and is fully configurable:[CODE]<html>
<head>

<script type="text/javascript">

document.write( "<style>#scriptWarn{display:none}</style>" )

function wCount( box, elemId, symbol, rate, minimum, minCharge )
{
var re=/bw+b/g, words = (words = box.value.match(re) ) ? words.length : 0,
price = Math.max( Math.round( rate * words / 1000 ), minCharge );

document.getElementById( elemId ).innerHTML = words + " word" + (words == 1 ? "" : "s") + " at the rate of " + symbol + rate +" per 1000: " + symbol + price + ( price == minCharge ? " (minimum charge applies)" : "" ) + ".";

return words;
}

</script>
</head>

<body>
<textarea id='wordBox' rows="8" cols="70" id="textField" align="ltr" onkeyup="return wCount(this, 'result', '&pound;', 60, 100, 20)"></textarea><input type='button' value='Calculate' onclick='document.getElementById( "wordBox" ).onkeyup()'>
<span id='scriptWarn' style='color:red'><b>Javascript needs to be enabled</b></span>
<div id = 'result' style='font-weight:bold'></div>
</body>
</html>[/CODE]
×

Success!

Help @droidman spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...