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 sum won't compute

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 03-04-2012, 08:25 PM PM User | #1 hmcka New to the CF scene   Join Date: Mar 2012 Posts: 9 Thanks: 2 Thanked 0 Times in 0 Posts sum won't compute Hello. Writing Javascript is new to me, and I've been reviewing this code for awhile and I can't find what's different than the printed example that I'm referring to. Why won't it work? I want a sum to compute after the user types in two integers. I've even gone as far a counting the characters in each line--everything is the same. Thank you. [CODE]
 03-04-2012, 08:26 PM PM User | #2 hmcka New to the CF scene   Join Date: Mar 2012 Posts: 9 Thanks: 2 Thanked 0 Times in 0 Posts more specific info for post Oh, I should mention that I think the error is in the end portion of this as I am able to get it to enter in numbers. [CODE] //convert numbers from strings to integers number1 = parseInt( firstNumber ); number2 = parseInt( secondNumber ); sum = number1 + number2; // add the numbers //display the results document.writeIn( "

The sum is " + sum + "

"); // -->
 03-04-2012, 08:35 PM PM User | #3 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,473 Thanks: 200 Thanked 2,469 Times in 2,447 Posts It is not document.writeIn() but document.writeln() if you wish to use this long-obsolete method of displaying a result. Learn from this that some characters 0 and O and 1 and l and I are easily confused, and best avoided. You can greatly simplify your code (there is no need to declare your variables separately):- Code: "Always carry a bottle of whisky in case of snakebite and furthermore always carry a small snake. " - W. C. Fields __________________ All the code given in this post has been tested and is intended to address the question asked. Unless stated otherwise it is not just a demonstration. Last edited by Philip M; 03-04-2012 at 08:40 PM..
 03-04-2012, 08:46 PM PM User | #4 hmcka New to the CF scene   Join Date: Mar 2012 Posts: 9 Thanks: 2 Thanked 0 Times in 0 Posts thank you and follow-up question Ok. I think I'm picking up what you're putting down. You are basically shortening the code by grouping and inserting variables--simple substitution. Or, looks a lot like algebra. What's the ,"" doing in the below code. n other words, what's that doing? [CODE] // read in first number from user as a number var firstNumber = parseInt(window.prompt("Enter first integer","")); Thank you.
 03-04-2012, 08:54 PM PM User | #5 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,473 Thanks: 200 Thanked 2,469 Times in 2,447 Posts Try it with and without the quotes, and observe the difference. You can improve the script with this construct firstNumber = parseInt(window.prompt("Enter first integer" )) || 0; || means or, and || 0 traps invalid entries so that if parseInt(() cannot evaluate the input value as an integer, it is replaced by 0. __________________ All the code given in this post has been tested and is intended to address the question asked. Unless stated otherwise it is not just a demonstration. Last edited by Philip M; 03-04-2012 at 09:00 PM..
 Users who have thanked Philip M for this post: hmcka (03-04-2012)
 03-04-2012, 09:17 PM PM User | #6 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,473 Thanks: 200 Thanked 2,469 Times in 2,447 Posts I should have said that if you use parseInt() you should specify the radix (10), as otherwise if the user enters a number with a 0 prefix it will be interpreted as octal, not decimal. firstNumber = parseInt(window.prompt("Enter first integer", "" ),10) || 0; __________________ All the code given in this post has been tested and is intended to address the question asked. Unless stated otherwise it is not just a demonstration.

 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 11:20 AM.