Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    arithmetic in javascript

    hi,
    i have written the following code however it doesn't function as intended. the final for loop needs to report the greatest number in the array aswell as the day it occured. i am a complete novice but i am guessing the numbers need to be converted to carry out the necessary arithmetic. any help would be greatly appreciated. i'm sure it very simple but i am struggling to work it out.
    cheers
    thebigsquid

    <HTML>
    <HEAD>
    <TITLE> books borrowed
    </TITLE>
    <SCRIPT >

    /* Program to read in a known number of data items and store them in an array */

    var booksBorrowed = new Array (5);
    var dayNamesArray = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday'];
    var maxNumberBooks;

    document.write('Array program to read in a known number of data items');
    for (var day = 0; day < booksBorrowed.length; day = day + 1)
    {
    booksBorrowed[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 < booksBorrowed.length; day = day + 1)
    {
    document.write('the amount of books borrowed on ' + dayNamesArray[day] + ' is ' + booksBorrowed[day] + '<BR>')
    };

    maxNumberBooks = 0
    for (var day = 0; day < booksBorrowed.length; day = day + 1)
    {
    if (booksBorrowed[day] > booksBorrowed[maxNumberBooks])
    {
    maxNumberBooks = day
    }
    };

    document.write('the greatest number of books borrowed was ' + booksBorrowed[maxNumberBooks] + ' on ' + dayNamesArray[maxNumberBooks])

    </SCRIPT>
    </HEAD>
    <BODY>
    </BODY>
    </HTML>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    As far as I can see this homework assignment is working fine.
    But please alter "amount" of books to "number" or "quantity".
    And don't place a semi-colon after }

  • #3
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for the reply, it works ok if i input numbers with the same amount of digits, for e.g 12, 16, 76, 97,54 but if i throw in a three digit number 12, 76, 137, 41, 54 it will return 76 as the greatest number?

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    The value entered in the prompt is a string, so you have to convert it to number.
    Code:
    booksBorrowed[day] = Number(window.prompt('Enter number of books borrowed on ' + dayNamesArray[day],''));
    But take note that if the user clicks on Cancel, the value would be NaN (Not a Number). You may want to do validation on it.

    Array has built-in sort function, you don't have to manually sort it.

    Code:
    function sortNumbers(a, b) { return b - a} //descending order
    
    booksBorrowed.sort(sortNumbers)
    alert(booksBorrowed);
    alert(booksBorrowed[0]);  //max number
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #5
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thats great! thanks for your help


  •  

    Posting Permissions

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