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 Create a function that calculates multiplication/division using addition and subtract

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 09-06-2013, 01:43 PM PM User | #1 gepca New to the CF scene   Join Date: Sep 2013 Location: Romania Posts: 1 Thanks: 0 Thanked 0 Times in 0 Posts Create a function that calculates multiplication/division using addition and subtract I'm a beginner in javascript, or any other programming language. I have two functions // function sum(a,b){ return a + b; } function reduce(a,b){ return a - b; } I need to create another function, let's call it calculate, a function that gets a string argument, something like this "4*5", or "9/3". The calculate function will have to use additions, the sum function respectively, in order to calculate the multiplication, and subtractions, the reduce function, in order to calculate the division. Can some one please give me some hints on how to do this ?? Thanks
 09-06-2013, 03:51 PM PM User | #2 Airblader Regular Coder   Join Date: Jan 2013 Location: Germany Posts: 578 Thanks: 4 Thanked 77 Times in 77 Posts A few functions that might come in handy: https://developer.mozilla.org/en-US/...s/String/split https://developer.mozilla.org/en-US/...String/indexOf Also, make sure to convert the numbers you collected from the string to actual Javascript numbers (i.e. using Number('5')), otherwise a+b will result in wrong results ('5' + '3' will evaluate to '53', not 8). I'd call your reduce() function subtract() by the way, but that's up to you. Anyway, here is a skeleton to get you started: Code: ```function multiply(input) { // do something to fill these variables with the corresponding values // make sure leftOperand and rightOperand are actually numerical values! var leftOperand, rightOperand, operationSymbol; // instead of if/elseif/else you could use a switch statement here (if you already know it) if(operationSymbol === "*") { // calculate multiplication } else if(operationSymbol === "/") { // calculate division } }```
 09-06-2013, 04:55 PM PM User | #3 sunfighter Senior Coder   Join Date: Jan 2011 Location: Missouri Posts: 3,320 Thanks: 23 Thanked 477 Times in 476 Posts Hope this helps you also: Code: `````` __________________ Evolution - The non-random survival of random variants.
 09-06-2013, 05:13 PM PM User | #4 Airblader Regular Coder   Join Date: Jan 2013 Location: Germany Posts: 578 Thanks: 4 Thanked 77 Times in 77 Posts Edit: never mind. Last edited by Airblader; 09-06-2013 at 05:19 PM..

 Bookmarks

 Tags function, javascript

 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:37 PM.