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 Help with a simple for loop function

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 09-08-2013, 11:27 PM PM User | #1 CaptainC New to the CF scene   Join Date: Sep 2013 Posts: 2 Thanks: 2 Thanked 0 Times in 0 Posts Help with a simple for loop function In my program, the user enters an integer. Using this integer as the upper limit, the program calculates the sum of the floors of the square roots of each integer from 1 to the supplied integer. I am making use of a for loop in my program, but am having trouble getting the desired result. Here is my current setup: Code: ```var max = Number(prompt("enter an integer")); var floor; for (i=1; i<=max; i++) { floor = Math.floor(Math.sqrt(i)); } alert("the sum of the floors of the square roots of each integer from 1 to " + max + " is " + floor);``` When I run this with 4 as the supplied integer, the program gives me 2 as the answer. However, 5 is the actual correct answer (Math.floor(Math.sqrt(1))+Math.floor(Math.sqrt(2))+ Math.floor(Math.sqrt(3))+Math.floor(Math.sqrt(4)) produces 1+1+1+2, which is 5). What am I doing wrong? Thanks in advance for your help.
 09-08-2013, 11:41 PM PM User | #2 xelawho Senior Coder     Join Date: Nov 2010 Posts: 2,590 Thanks: 54 Thanked 477 Times in 475 Posts this line: Code: `floor = Math.floor(Math.sqrt(i));` overwrites the floor variable every time the loop iterates if you want to be adding the result, initialize floor as 0 and then add the result of your calculation to it on each iteration: Code: `floor += Math.floor(Math.sqrt(i));` you can read elsewhere why alert and prompt are antiquated and generally frowned upon
 Users who have thanked xelawho for this post: CaptainC (09-09-2013)
 09-08-2013, 11:48 PM PM User | #3 DrDOS Senior Coder   Join Date: Sep 2010 Posts: 1,879 Thanks: 15 Thanked 224 Times in 224 Posts Code: ```var max = Number(prompt("enter an integer")); var floor=0; for (i=1; i<=max; i++) { floor = floor + Math.floor(Math.sqrt(i)); } alert("the sum of the floors of the square roots of each integer from 1 to " + max + " is " + floor);``` __________________ Welcome to http://www.myphotowizard.net where you can edit images, make a photo calendar, add text to images, and do much more. When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.
 Users who have thanked DrDOS for this post: CaptainC (09-09-2013)
 09-09-2013, 12:00 AM PM User | #4 CaptainC New to the CF scene   Join Date: Sep 2013 Posts: 2 Thanks: 2 Thanked 0 Times in 0 Posts Thanks to both of you! My problem is solved.

 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 06:13 PM.