PDA

View Full Version : Need to do a simple arithmetic operation

samiam
04-15-2008, 07:54 PM
Hi folks, need a little help writing out some code here. Disclaimer: my coding knowledge is non-existent, but I'm a quick learner.

I'm using blogspot.com to host a site and need to take in a numerical value from the user, do some basic arithmetic to that value, and then spit out the result.

Specifically, I'd like to calculate the number of pages given the number of words. For example, say the word count (input user value) is 1,335. I'd like my html code to divide that number by 300 and spit out the answer (in decimal form if necessary).

Thanks in advance for your help!

S.

Master_script_maker
04-15-2008, 08:20 PM
paginate(1000); would result in "3 33/100"

<script type="text/javascript">
function paginate(num) {
if (typeof (num+0) == "number") {//if num is a number
var pagesT = num/300;//number of pages
var remainder = ((num%300)/300==0)?"":(((num%300)/300)+"").substr(2,2)+"/100";//if there is a remainder, convert to n/100
var pagesP = pagesT-remainder;//pages to display
pagesP=pagesP+" "+remainder;// add up all of the results into a string
alert(pagesP);
} else {
alert("Inputed value is not a number!"); //if num is not a number
}
}
</script>

samiam
04-15-2008, 10:16 PM
Thanks for your reply master script maker.

Unfortunately when I tried to run it on blogspot.com nothing happened. Didn't even see a text box to enter the page count.

Maybe I'm posting in the wrong section. I need html coding for a website. I think I may need the coding in terms of a <form> rather than a <script>. Don't know enough about coding to be sure though.

Appreciate any further help!

Master_script_maker
04-15-2008, 10:20 PM
oh sorry, i only thought you needed the script. place this in the body:

<script type="text/javascript">
function paginate(num) {
if (typeof (num+0) == "number") {//if num is a number
var pagesT = num/300;//number of pages
var remainder = ((num%300)/300==0)?"":(((num%300)/300)+"").substr(2,2)+"/100";//if there is a remainder, convert to n/100
var pagesP = pagesT-remainder;//pages to display
pagesP=pagesP+" "+remainder;// add up all of the results into a string
alert("Pages: "+pagesP);
} else {
alert("Inputed value is not a number!"); //if num is not a number
}
}
</script>
<form method="get" action="#">
Number of words: <input type="text" name="words" id="words"><br>
<input type="button" value="Calculate the number of pages" onclick="paginate(document.getElementById('words').value);">
</form>

samiam
04-16-2008, 01:46 AM
I've been trying to get this thing working for awhile now with not much success. So I appreciate your help thus far!

Just need to fix one more thing. I used the new code you provided and it successfully provides the user with a text box asking for the word count, but unfortunately the answer is not displayed anywhere for the user to see (apologies for not being clear with what I was looking for.)

Basically i'd like to enter the word count, and then have the answer *displayed* on the same screen.

Thanks again for all your help!

Master_script_maker
04-16-2008, 11:25 AM
my last script was buggy, try this:
<script type="text/javascript">
function paginate(num) {
if (typeof (parseInt(num)) == "number") {//if num is a number
var pagesT = num/300;//number of pages
var remainder = (num%300)/300;
var remainderD = (remainder==0)?"":(remainder+" ").substr(2,2)+"/100";//if there is a remainder, convert to n/100
var pagesP = pagesT-remainder;//pages to display
pagesP=pagesP+" "+remainderD;// add up all of the results into a string
document.getElementById("pages").innerHTML=pagesP;
} else {
alert("Please enter a value!"); //if num is not a number
}
}
</script>
<form method="get" action="#">
Number of words: <input type="text" name="words" id="words"><br>
<input type="button" value="Calculate the number of pages" onclick="paginate(document.getElementById('words').value);"><br>
Number of pages: <div id="pages"></div>
</form>

Master_script_maker
04-18-2008, 12:27 AM
have you tried out the new script?