Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Problems with For loop and finding highest amount.
Old 06-29-2007, 07:18 PM Problems with For loop and finding highest amount.
Novice Talker

Posts: 12
Location: Somewhere on a Planet.
Trades: 0
So, the code I am using is below. It all works, except for the last part where it is supposed to go through and find the highest number out of a set of five. At the moment, it just choses Monday and the amount of books borrowed on that day. It is not going through the other amounts and comparing them. So, can anyone help and show me what I have missed out?

var booksArray = new Array (5);
var dayNamesArray = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
var maxBooksIndex;
document.write('Array program to read in a known number of books borrowed over five days');
for (var day = 0; day < booksArray.length; day = day + 1)
{
booksArray[day] = window.prompt('Enter number of books borrowed on ' + dayNamesArray[day],'')
};
document.write('<BR>' + '<BR>');
document.write('Confirmation of data input' + '<BR>' + '<BR>');
for (var day = 0; day < booksArray.length; day = day + 1)
{
document.write(dayNamesArray[day] + ': ' + booksArray[day] + '<BR>')
};
maxBooksIndex = 0;
for (var day = 1; day < booksArray.length; day = day + 1)
{
if (booksArray[day] > booksArray[maxBooksIndex])
{
maxBooksIndex = day;
}
};
document.write('Maximum number of books was ' + booksArray[maxBooksIndex] + ' and this was on ' + dayNamesArray[maxBooksIndex])
NinjaKitty is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-30-2007, 03:31 AM Re: Problems with For loop and finding highest amount.
vangogh's Avatar
Post Impressionist

Posts: 10,689
Name: Steven Bradley
Location: Boulder, Colorado
Trades: 0
What browser are you using. I tried in Firefox 2 and IE6 and it worked for me in both.

I looked over the code and I don't see anything that looks wrong either. From what I can see it should be working.
__________________
l Search Engine Friendly Web Design |
Please login or register to view this content. Registration is FREE

l Tips On Marketing, SEO, Design, and Development |
Please login or register to view this content. Registration is FREE

l
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
vangogh is offline
Reply With Quote
View Public Profile Visit vangogh's homepage!
 
Old 06-30-2007, 12:18 PM Re: Problems with For loop and finding highest amount.
Novice Talker

Posts: 12
Location: Somewhere on a Planet.
Trades: 0
Ok, that's odd. I will try it in another browser then and see what happens. I am using mozilla firefox ver 2.0.0.4

Edit:- I tried it in another browser and it is still choosing Monday and the number of books borrowed on that day. These are the numbers I am entering for each day: Monday - 9, Tuesday - 1, Wednesday - 1, Thursday - 23, Friday - 0. So, it should be choosing Thursday and the number 23, but it isn't. Anyone got any clues?

Edit2:- I tried it with a different set of data (5, 7, 21, 3, 9), in this case it is supposed to pick Wednesday with 21 books, but it picks Friday with 9 books. It doesn't seem to like numbers with two digits??

Last edited by NinjaKitty; 06-30-2007 at 12:34 PM..
NinjaKitty is offline
Reply With Quote
View Public Profile
 
Old 06-30-2007, 02:26 PM Re: Problems with For loop and finding highest amount.
Novice Talker

Posts: 12
Location: Somewhere on a Planet.
Trades: 0
Solved it *dances round room*.

I had missed out a parseFloat part. Thanks for any input anway. Always appreciated.
NinjaKitty is offline
Reply With Quote
View Public Profile
 
Old 07-01-2007, 01:46 AM Re: Problems with For loop and finding highest amount.
vangogh's Avatar
Post Impressionist

Posts: 10,689
Name: Steven Bradley
Location: Boulder, Colorado
Trades: 0
Glad you got it working. I admit I didn't thoroughly test it so maybe I just got lucky with the numbers I tried. Sorry I couldn't help more, but still glad you got it all sorted out.
__________________
l Search Engine Friendly Web Design |
Please login or register to view this content. Registration is FREE

l Tips On Marketing, SEO, Design, and Development |
Please login or register to view this content. Registration is FREE

l
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
vangogh is offline
Reply With Quote
View Public Profile Visit vangogh's homepage!
 
Reply     « Reply to Problems with For loop and finding highest amount.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.87586 seconds with 12 queries