Jump to content

Tusk

Members
  • Posts

    17
  • Joined

  • Last visited

Tusk's Achievements

Newbie

Newbie (1/7)

0

Reputation

  1. Hi DD, there are a few versions of the post so have cleaned up the one I am using and show it below. In the textarea I can have multiple spaces or CR between numbers and it works giving me their sum But if i have a space before the first number or after the last number I get a NAN <html> <head> <title>Calc Numbers</title><script type='text/javascript'>function calc() {var t = document.getElementById("digit").value;var numbers = new Array();numbers = t.split(/\s+/);var sum = 0;var num = '';if (numbers.length == 1) { num = '0'; }for (i = 0; i < numbers.length; i++) {sum = sum + eval(numbers); if (i == numbers.length - 1) { num = num + " and ";} else { if (i > 0) { num = num + ", "} } num = num + numbers; }result = + sum +"<"+"/p>";var resElement = document.getElementById("res");resElement.innerHTML = result;}</script></head> <form> <textarea id='digit' onclick="this.value=''" onchange='calc()'cols = 65 rows = 5 ></textarea><br> <input type="button" name="b1" value="Click" onClick = "calc()"> <span><div id='res'></div></span> </form> </html>
  2. Thanks ShadowMage,much apreciated
  3. DD, I still get a NAN result if I have a space before the first digit or after the last digit, is there a simple way of fixing that?
  4. Hi ShadowMage,its not that I get errors It doesn't always update unless I use the calc button. But I am trying to get it to update without the need of a button. I think it needs a function in the script section to update onblur but am not sure how to do that?
  5. Thanks Mnkn, Below is an extract of the code.I want the result at the bottom "the <input readonly='readonly'> field" to update automatically if either of the the input areas above are changed.many thanks<html> <head> <script type= "JavaScript"></SCRIPT><meta http-equiv='Content-type' content='text/html;charset=UTF-8'><title>Calc Numbers</title><script type='text/javascript'>function calc() {var t = document.getElementById("ta2").value;var numbers = new Array();numbers = t.split(/\s+/);var sum = 0;var num = '';if (numbers.length == 1) { num = '0'; }for (i = 0; i < numbers.length; i++) {sum = sum + eval(numbers); if (i == numbers.length - 1) { num = num + " and ";} else { if (i > 0) { num = num + ", "} } num = num + numbers; }result = + sum +"<"+"/p>";var resElement = document.getElementById("res");resElement.innerHTML = result;}function a_div_b(form) { a=parseInt(result); b=parseInt(form.b.value,10); c=a/b; d=(Math.floor(a/:)); e=b*d; f=a-e; if (f==0) { form.ans.value = b } else { form.ans.value = f }}</script></head> <body bgcolor="#FFFFFF" background="bckgrnd.gif" text="#000000" leftmargin="0" topmargin="0" class="titlestyle"> <div align="center"> <br> <font size="5" face="comic sans ms">1)<br> <textarea id='ta2' onclick="this.value=''" onchange='calc()'cols = 65 rows = 5 ></textarea> <div id='res'></div> </font></div> <CENTER><FORM name="formx" style="font-family: 'Comic Sans MS'; Comic Sans MS&amp; quot;; font-size: small; color: #0000FF"> <font size="5" face="comic sans ms">2)<br></font> <input type=text size=4 value=0 name="b" style="font-size: medium; font-weight: normal; font-family: 'Comic Sans MS';"> <br><span class="style3"> </span><br> <input type="button" value="calc" onClick="a_div_b(this.form)"> <input readonly='readonly' value=0 name="ans" size=9 style="font-family: "Comic Sans MS"; font-size: medium; color: #0000FF; background-color: #FFFF99"> </FORM></CENTER></html>
  6. I have a result that I display in a <input readonly='readonly'> field. It was being driven by a button, But I want it to recalculate or refresh every time anything is changed in other parts of the page (it's value is derived form other sections of the page)I have tried using "onchange" and "onclick" events but they don't seem to work every time? Is there another or better way of achieving this?
  7. Pierre Thanks,and for the explanation, I struggle so much trying to get things to work by looking for other examples hacking and pasting ...so when you give an explanation I realize ignorant i really am it should be bliss but with code its so frustrating ...one step forward 2 steps backwards.....when doe you run???? LOL Much apreciated
  8. Hi Synookhere is the whole form basically "a "and "b" are input fields if you can suggest a better way of defining them great, bare in mind I am really new to this so be gentle with explanations Thanks<html xmlns='http://www.w3.org/1999/xhtml' lang='en' xml:lang='en'><HEAD><script type="text/javascript">function a_div_b(form) {a=eval(form.a.value)b=eval(form.b.value)c=a/bd=(Math.floor(a/:))e=b*df=a-eform.ans.value = f}</script></HEAD><BODY><CENTER><FORM name="formx"><input type=text size=4 value=211 name="a"> <input type=text size=4 value=49 name="b"> = <input type "number" value=0 name="ans" size=9> <input type="button" value=" Calc " onClick="a_div_b(this.form)"></FORM></CENTER></html>
  9. I want to display one of two values either "b" or "f"If "f=0" I want to display "b" and if not display "f" below is my code but its not working?<script type="text/javascript">function a_div_b(form) {a=eval(form.a.value)b=eval(form.b.value)c=a/bd=(Math.floor(a/:))e=b*df=a-eif (f=0) { form.ans.value = b }else { form.ans.value = f }}</script>
  10. I want to use a result for furtherer calculations but only want the whole number and not the decimal portion. In Excel there is a function "=INT(A1)" this ignores the decimal values in cell "A1", e.g. if call "A1=5.345"and I use the following function in cell "A2" "=INT(A1)*3" I get the result "15"How do I do this in JavaScript ?Thanks
  11. Thanks all, this is the putting "+" where the "*" was in DD's suggestion did the trick, appreciate all the helpnumbers = t.split(/\s+/);
  12. My "<!-- Script Block 1-->" Returns a NAN result when i add "<!-- Script Block 2-->" but if I remove Block 2, Block 1 works? Is there a way I can run the two together on the same page without them clashing?<html> <head> <!-- Script Block 1--><script language='javascript' type='text/javascript'>function calc(data){var cA1A=data.pA1A,cA2A=data.pA2A,cA3A=Math.floor(cA1A/cA2A),cA4A=cA3A*cA2A,cA5A=cA1A-cA4A,cA6A=cA5A>0?cA5A:cA2A;data.pA3A=cA3A;data.pA4A=cA4A;data.pA5A=cA5A;data.pA6A=cA6A}</script><script language='javascript' type='text/javascript'>var eeisus=0,eetrue="TRUE",eefalse="FALSE",eedec=".",eeth=",",eedecreg=new RegExp("\\.","g"),eethreg=new RegExp(",","g")</script><script language='javascript' type='text/javascript'>var co={};function recalc_onclick(){if(true){co.pA1A=eeparseFloat(document.formc.pA1A.value);co.pA2A=eeparseFloat(document.formc.pA2A.value);calc(co);document.formc.pA3A.value=eedisplayFloat(co.pA3A);document.formc.pA4A.value=eedisplayFloat(co.pA4A);document.formc.pA5A.value=eedisplayFloat(co.pA5A);document.formc.pA6A.value=eedisplayFloat(co.pA6A)}}</script><script language='javascript' type='text/javascript'>function myIsNaN(x){return isNaN(x)||typeof x=="number"&&!isFinite(x)}function round(n,nd){if(isFinite(n)&&isFinite(nd)){var sign_n=n<0?-1:1,abs_n=Math.abs(n),factor=Math.pow(10,nd);return sign_n*Math.round(abs_n*factor)/factor}else return NaN}function eeparseFloat(str){str=String(str).replace(eedecreg,".");var res=parseFloat(str);if(isNaN(res))return 0;else return res}var near0RegExp=new RegExp("[.](.*0000000|.*9999999)");function eedisplayFloat(x){if(myIsNaN(x))return Number.NaN;else{var str=String(x);if(near0RegExp.test(str)){x=round(x,8);str=String(x)}return str.replace(/\./g,eedec)}}</script><script language='javascript' type='text/javascript'>var reqlist=[];function reset_onclick();recalc_onclick("")}</script><!-- Script Block 2--><script language='javascript' type='text/javascript'>function calc() {var t = document.getElementById("cipher").value;var numbers = new Array();numbers = t.split(/\s+/);var sum = 0;var num = '';if (numbers.length == 1) { num = '0'; }for (i = 0; i < numbers.length; i++) {sum = sum + eval(numbers); if (i == numbers.length - 1) { num = num + " and ";} else { if (i > 0) { num = num + ", "} } num = num + numbers; }result = "<p>The sum of the numbers " + num + " is " + sum +"<"+"/p>";var resElement = document.getElementById("res");resElement.innerHTML = result;}</script><meta http-equiv='Content-type' content='text/html;charset=UTF-8'><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head> <body bgcolor="#FFFFFF" background="bckgrnd.gif" text="#000000" leftmargin="0" topmargin="0" class="titlestyle"> <div align="center"> <br> <form name="form1" class="textstyle"> <br> <br> <p><br></p> <font color="#0000FF" size="4" face="comic sans ms"><br> <textarea id='cipher' onclick="this.value=''" onchange='calc()'cols = 65 rows = 5 ></textarea> <div id='res'></div> </font> </form> </div> </body> <body> <form id='formc' name='formc' method='post' action=''><table cellspacing='0' cellpadding='0' width='419' style='border-collapse:collapse' align="center"><tr style="height:1px;"><td style="width: 49px"></td><td width='192'></td></tr><tr><td class='ee100' style="width: 49px"><input onchange="this.value=eedisplayFloat(eeparseFloat(this.value));recalc_onclick('pA1A') ;setValueofBtn();" value='211' type='text' tabindex='1' style=' width:100%;padding-right:1px; color: #000000; text-align: right; background-color: #C0C0C0;' name='pA1A' id='pA1A' class='ee102' downkey='pA2A' onKeyDown="onEnterPress('pA1A');" > </td><td class='ee103' colspan='3'> </td></tr><tr style='height:13pt; '><td class='ee100' style="width: 49px"><input onchange="this.value=eedisplayFloat(eeparseFloat(this.value));recalc_onclick('pA2A') ;setValueofBtn();" value='49' type='text' tabindex='2' style=' width:100%;padding-right:1px; color: #000000; text-align: right; background-color: #C0C0C0;' name='pA2A' id='pA2A' class='ee102' onKeyDown="onEnterPress('pA2A');" > </td><td class='ee103' colspan='3'> </td></tr><tr style='height:13pt'><td class='ee109' style="width: 49px"><input type='hidden' value='0' tabindex='-1' style='overflow:hidden; border:0px solid #000000; width:100% ;padding-right:1px;' name='pA3A' id='pA3A' class='ee110' > </td><td class='ee103' colspan='3'><div style="height:13pt;overflow:hidden"></div></td></tr><tr style='height:13pt'><td class='ee109' style="width: 49px" ><input type='hidden' value='0' tabindex='-1' style='overflow:hidden; border:0px solid #000000; width:100% ;padding-right:1px;' name='pA4A' id='pA4A' class='ee110' > </td><td class='ee103' colspan='3'><div style="height:13pt;overflow:hidden"></div></td></tr><tr style='height:14pt'><td class='ee109' style="width: 49px" ><input value='0' name='pA5A' id='pA5A' type='hidden' > </td><td class='ee103' colspan='3'><div style="height:14pt;overflow:hidden"></div></td></tr><tr style='height:14pt; font-family: 'Comic Sans MS'; color: #0000FF; font-size: large; background-color: #FFFFCC; text-align: center;'><td class='ee111' style="width: 49px" ><input readonly='readonly' value='0' type='text' tabindex='-1' style='background: #FFFFCC; overflow:hidden; border:0px solid #000000; width:100%; padding-right:1px; text-align: center; color: #0000FF; font-weight: bold; font-size: large;' name='pA6A' id='pA6A' class='ee113' > </td><td class='ee103' colspan='3'>Result</td><tr style='height:13pt'><td class='ee103' style="width: 49px" ><div style="height:13pt;overflow:hidden"></div></td><td class='ee103' colspan='3'><div style="height:13pt;overflow:hidden"></div></td></tr></table></form></html>
  13. Thanks for help I am almost there but stuck at the last hurdle, in the input field you can enter spaces any where and any amount of spaces between the numbers and the result returns a list of the numbers with a single space separating them which is great I now just want to calculate the sum of the result ?? have spent hours trying to do this but cant get scrip or figure out how to do it, will be most great full if some body can do this for me Many thanks<head><script type="text/javascript" src="https://getfirebug.com/firebug-lite-debug.js"></script><meta http-equiv='Content-type' content='text/html;charset=UTF-8'><title>Calc Numbers</title><script type='text/javascript'>function calc() {// calc() is designed to sum numbers entered in an input fieldvar t = document.getElementById("num").value; // the numbers enteredvar numbers = new Array();numbers = t.split(" "); // the numbers extracted from the stringvar sum = 0; // variable to hold the sum of the numbersvar num = ''; // string to hold a list of the numbersif (numbers.length == 1) { num = '0'; } // to add single number tofor (i = 0; i < numbers.length; i++) {sum = sum + eval(numbers); // add this numberif (i == numbers.length - 1) { num = num + " ";} // last number?else { if (i > 0) { num = num + " "} } // otherwise if not 1st, join with ,num = num + numbers; // and add the number to the list}// create the text to display with the resultresult = num +"<"+"/p>";// find the placeholder for displaying the resultvar resElement = document.getElementById("res");// display the resultresElement.innerHTML = result;}</script></head><body><h1>Calc Numbers</h1><p>Enter some numbers separated by spaces:</p><textarea id='num' onclick="this.value=''" onchange='calc()' cols = 65 rows = 5 wrap="PHYSICAL"></textarea><!-- provide a dummy button - encourage user to leave input tag --><input type="button" value="Calc"><!-- the following div id='res' provides a placeholderthat calc() can use to display the result --><div id='res'></div></body>
  14. Thanks DD,It has solved the problem of the spaces, but have encounted another problem in the result, for some reason my function is not recognizing double digit numbers it converts them to single digits e.g "23 4 55" it converts to "2+3+4+5+5=19" and not "23+4+55=82"<head><script type="text/javascript" src="https://getfirebug.com/firebug-lite-debug.js"></script><meta http-equiv='Content-type' content='text/html;charset=UTF-8'><title>Calc Numbers</title><script type='text/javascript'>function calc() {// calc() is designed to sum numbers entered in an input fieldvar t = document.getElementById("num").value; // the numbers enteredvar numbers = new Array();numbers = t.split(/\s*/); // the numbers extracted from the stringvar sum = 0; // variable to hold the sum of the numbersvar num = ''; // string to hold a list of the numbersif (numbers.length == 1) { num = '0'; } // to add single number tofor (i = 0; i < numbers.length; i++) {sum = sum + eval(numbers); // add this numberif (i == numbers.length - 1) { num = num + " and ";} // last number?else { if (i > 0) { num = num + ", "} } // otherwise if not 1st, join with ,num = num + numbers; // and add the number to the list}// create the text to display with the resultresult = "<p>The sum of the numbers " + num + " is " + sum +"<"+"/p>";// find the placeholder for displaying the resultvar resElement = document.getElementById("res");// display the resultresElement.innerHTML = result;}</script></head><body><h1>Calc Numbers</h1><p>Enter some numbers separated by spaces:</p><textarea id='num' onclick="this.value=''" onchange='calc()' cols = 65 rows = 5 wrap="PHYSICAL"></textarea><!-- provide a dummy button - encourage user to leave input tag --><input type="button" value="Calc"><!-- the following div id='res' provides a placeholderthat calc() can use to display the result --><div id='res'></div></body>
×
×
  • Create New...