Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Simple Math

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 04-01-2003, 09:49 PM PM User | #1 Saj Regular Coder   Join Date: Feb 2003 Posts: 181 Thanks: 0 Thanked 0 Times in 0 Posts Simple Math Hi, I am trying to create a javascript that says: add a and b together and then divide it by 2. (a + b) / 2 How would you do this? __________________ - Saj
 04-01-2003, 09:54 PM PM User | #2 Choopernickel Regular Coder   Join Date: Apr 2003 Location: Atlanta, GA Posts: 487 Thanks: 0 Thanked 0 Times in 0 Posts solution using functions Code: ```function add(num1, num2) { sum = num1 + num2; return sum } function halve(num) { half = num/2; return half; } function sumAndHalve(numA, numB) { numC = add(numA, numB); numD = halve(numC); return numD }``` then, anywhere, you'd call Code: `sumAndHalve(3,1)` I know there are easier, more terse ways to code this, but I like to keep each operation separate. Is that so wrong? Last edited by brothercake; 04-01-2003 at 10:07 PM..
 04-01-2003, 09:56 PM PM User | #3 Choopernickel Regular Coder   Join Date: Apr 2003 Location: Atlanta, GA Posts: 487 Thanks: 0 Thanked 0 Times in 0 Posts D'oh! I posted a new thread instead of posting a reply. D'oh! Check it. I merged your answer (above) into this thread Last edited by brothercake; 04-01-2003 at 10:08 PM..
 04-01-2003, 10:12 PM PM User | #4 beetle Senior Coder   Join Date: Aug 2002 Posts: 3,467 Thanks: 0 Thanked 0 Times in 0 Posts Or make some methods! I love methods! Code: ```Number.prototype.add = function() { var arg, i = 0, sum = this; while( arg = arguments[i++] ) { sum += parseInt( arg, 10 ); } return sum; } Number.prototype.divide = function( denom ) { return this / denom; } var a = 4; var b = 2; alert( a.add( b ).divide( 2 ) );``` __________________ My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP “Minds are like parachutes. They don't work unless they are open” “Maturity is simply knowing when to not be immature”
 04-01-2003, 10:16 PM PM User | #5 Choopernickel Regular Coder   Join Date: Apr 2003 Location: Atlanta, GA Posts: 487 Thanks: 0 Thanked 0 Times in 0 Posts thanks, brothercake. beetle, great solution. For some reason, I don't seem to consider extending the prototype until long after the original need is fulfilled by functions. Dur!
 04-01-2003, 10:17 PM PM User | #6 Saj Regular Coder   Join Date: Feb 2003 Posts: 181 Thanks: 0 Thanked 0 Times in 0 Posts ok thanks. would i be able to use variables already defined as the addends? __________________ - Saj
 04-01-2003, 10:23 PM PM User | #7 liorean The thread killer   Join Date: Feb 2003 Location: Umeå, Sweden Posts: 5,575 Thanks: 0 Thanked 84 Times in 75 Posts Wouldn't this be cooler to have on an Array? Code: ```Array.prototype.sum=function(){ var l=this.length, r=0; while(--l) r+=this.[l]; return r; } Array.prototype.avg=function(){ var k, l=k=this.length, r=0; while(--l) r+=this.[l]; return (r/k); }``` Then you can use [a,b].avg() for doing the job. __________________ liorean <[lio@wg]> Articles: RegEx evolt wsabstract , Named Arguments Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards Last edited by liorean; 04-01-2003 at 10:26 PM..
 04-01-2003, 10:40 PM PM User | #8 beetle Senior Coder   Join Date: Aug 2002 Posts: 3,467 Thanks: 0 Thanked 0 Times in 0 Posts Cooler? Yes? More practical? Your mileage may vary Choopernickel - extending the prototype is a large part of an upcoming article I'm writing for Sitepoint.com on javascript namespaces/scope It should be an eye-opener for many (I hope) __________________ My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP “Minds are like parachutes. They don't work unless they are open” “Maturity is simply knowing when to not be immature”
 04-01-2003, 10:56 PM PM User | #9 Choopernickel Regular Coder   Join Date: Apr 2003 Location: Atlanta, GA Posts: 487 Thanks: 0 Thanked 0 Times in 0 Posts Excellent, beetle. I've gotten familiar with extending it through some string operations I've come up with, then with an Array.search() method that I think was pretty cool. I'm SO freakin' happy to finallly have found a javascript forum where it's like, all serious javascripting and everything! like you don't even know.
 04-01-2003, 11:26 PM PM User | #10 Saj Regular Coder   Join Date: Feb 2003 Posts: 181 Thanks: 0 Thanked 0 Times in 0 Posts Ok here's what I'm trying to do: Code: ```Number.prototype.add = function() { var arg, i = 0, sum = this; while( arg = arguments[i++] ) { sum += parseInt( arg, 10 ); } return sum; } Number.prototype.divide = function( denom ) { return this / denom; } function calculate() { var a = document.triangle.a.value var b = document.triangle.b.value var c = document.triangle.c.value var b2 = b*b var c2 = c*c if (a=" ") { document.triangle.a.value = b2.add(c2).divide( 2 ) } else { javascript:calculate2() } }``` For this one I want c - b, not c + b. I've spent ages on this, and can't firure it out. __________________ - Saj Last edited by Saj; 04-01-2003 at 11:30 PM..
 04-01-2003, 11:30 PM PM User | #11 Saj Regular Coder   Join Date: Feb 2003 Posts: 181 Thanks: 0 Thanked 0 Times in 0 Posts LOL Never Mind stupid question. I think I got it. __________________ - Saj
 04-02-2003, 12:08 AM PM User | #12 Saj Regular Coder   Join Date: Feb 2003 Posts: 181 Thanks: 0 Thanked 0 Times in 0 Posts Please tell me what I am doing wrong here! Code: ```function divide(num) { half = num/2; return half; } function calculate() { var a = document.triangle.a.value; var b = document.triangle.b.value; var c = document.triangle.c.value; var b2 = b*b; var c2 = c*c; var a2 = a*a; var c2b2 = c2-b2; if (a=" ") { document.triangle.a.value = divide(c2b2); } else { alert("Hello"); } }``` By the way, yes I am doing the pythagorean theorem. A few of the variables are not necessary now, but I need them later. __________________ - Saj
 04-02-2003, 12:20 AM PM User | #13 beetle Senior Coder   Join Date: Aug 2002 Posts: 3,467 Thanks: 0 Thanked 0 Times in 0 Posts All values retrieved from form elements are String-type variables. You need to convert them to Number-type with parseInt var f = document.triangle; var a = parseInt( f.a.value, 10 ); var b = parseInt( f.b.value, 10 ); var c = parseInt( f.c.value, 10 ); Which is why I suspect you also had problem using methods prototyped onto the Number object __________________ My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP “Minds are like parachutes. They don't work unless they are open” “Maturity is simply knowing when to not be immature”
 04-02-2003, 01:01 AM PM User | #14 Saj Regular Coder   Join Date: Feb 2003 Posts: 181 Thanks: 0 Thanked 0 Times in 0 Posts Ok I see what your doing, but what is the need for the 10? __________________ - Saj
 04-02-2003, 01:05 AM PM User | #15 Saj Regular Coder   Join Date: Feb 2003 Posts: 181 Thanks: 0 Thanked 0 Times in 0 Posts Also, I'm still getting an answer of 4.5, when I want three, with this code: Code: ```function divide(num) { half = num/2; return half; } function calculate() { var triangle = document.triangle; var a = parseInt( triangle.a.value, 10 ); var b = parseInt( triangle.b.value, 10); var c = parseInt( triangle.c.value, 10); var a2 = a*a; var b2 = b*b; var c2 = c*c; var c2b2 = c2-b2; if (a=" ") { document.triangle.a.value = divide(c2b2); } else { alert("Hello"); } }``` __________________ - Saj

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 01:09 PM.