SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Help please

  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Help please

    Can't get the right value result with Maximum number of books borrowed ( don't get the highest value??)


    Code JavaScript:
    var booksArray = new Array (5);
    var fivedaysArray = ['Monday','Tuesday','Wednesday','Thursday','Friday'];
    var maxBooksIndex;
     
    maxBooksIndex = 0;
     
    document.write('Total number of library books borrowed in 5 days.');
    for (var day = 0; day < fivedaysArray.length; day = day + 1)
    {
        booksArray[day] = window.prompt('Please enter how many books borrowed on ' + (fivedaysArray[day]),'')
    }; 
    document.write('<BR>' + '<BR>');
    document.write('Confirmation of students input' + '<BR>' + '<BR>');
     
    for (var day = 0; day < fivedaysArray.length; day = day + 1)
    {
        document.write(fivedaysArray[day] + ' : '
    	+ booksArray[day] + '<BR>')
    }
    for (var day = 0; day < booksArray.length; day = day + 1) 
    {
    if (booksArray[day] > booksArray[maxBooksIndex]) 
    {
    maxBooksIndex = day
    }
    };
     
    document.write('Maximum number of books borrowed was ' + booksArray[maxBooksIndex] + ' occurred in ' + fivedaysArray[maxBooksIndex])

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I rewrote this to make it a little cleaner,

    Code:
            var booksArray = new Array();
            var dayArray = new Array('Monday','Tuesday','Wednesday','Thursday','Friday');
            var maxBooks = 0;
    
            for(var i = 0; i < dayArray.length; i++) {
                booksArray[i] = parseInt(prompt('Please enter how many books borrowed on ' + (dayArray[i]), ''));
                document.write(dayArray[i] + ' ' + booksArray[i] + '<br />');
            }
            for(var i = 0; i < dayArray.length; i++) {
                if(booksArray[i] > maxBooks)
                    maxBooks = booksArray[i];
            }
            alert(maxBooks);
    But the problem was your if statement required the input to be numbers to successfully work. I got around this by parsing the prompt input into an int using parseInt.

    Hope that helps


  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you so much for the reply, I try to figure out another way without using the parseInt and get the result written on the screen not as an alert anybody????

  4. #4
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just use your original code but wrap the window.prompt in a parseInt

    Code:
    var booksArray = new Array (5);
    var fivedaysArray = ['Monday','Tuesday','Wednesday','Thursday','Friday'];
    var maxBooksIndex;
     
    maxBooksIndex = 0;
     
    document.write('Total number of library books borrowed in 5 days.');
    for (var day = 0; day < fivedaysArray.length; day = day + 1)
    {
        booksArray[day] = parseInt(window.prompt('Please enter how many books borrowed on ' + (fivedaysArray[day]),''));
    };
    document.write('<BR>' + '<BR>');
    document.write('Confirmation of students input' + '<BR>' + '<BR>');
     
    for (var day = 0; day < fivedaysArray.length; day = day + 1)
    {
        document.write(fivedaysArray[day] + ' : '
        + booksArray[day] + '<BR>')
    }
    for (var day = 0; day < booksArray.length; day = day + 1)
    {
    if (booksArray[day] > booksArray[maxBooksIndex])
    {
    maxBooksIndex = day
    }
    };
     
    document.write('Maximum number of books borrowed was ' + booksArray[maxBooksIndex] + ' occurred in ' + fivedaysArray[maxBooksIndex]);
    or my code adapted

    Code:
            var booksArray = new Array();
            var dayArray = new Array('Monday','Tuesday','Wednesday','Thursday','Friday');
            var maxBooks = 0;
     
        document.write('Total number of library books borrowed in 5 days.');
        document.write('<BR /><BR />');
        document.write('Confirmation of students input' + '<BR>' + '<BR>');
    
            for(var i = 0; i < dayArray.length; i++) {
                booksArray[i] = parseInt(prompt('Please enter how many books borrowed on ' + (dayArray[i]), ''));
                document.write(dayArray[i] + ' : ' + booksArray[i] + '<br />');
            }
            for(var i = 0; i < dayArray.length; i++) {
                if(booksArray[i] > maxBooks)
                    maxBooks = booksArray[i];
            }
        document.write('Maximum number of books borrowed was ' + maxBooks);



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •