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 9 of 9
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Character Change

    Good morning to all, i`m trying to change the code so that instead of displaing the days as numbers i`d like to change it to names, been trying with code very kindly provided by PhillipM (fortunatley made a copy of it) but screen doesn`t display anthing by the time i`ve finished messing with it any help would be much appreicated Neil

    <html>
    <head>
    <title>Arrays</title>
    <script type = "text/javascript">
    /* Program to read in a known number of data and store them in an array */

    var bookArray = new Array (5);
    var highestNum = 0;
    var highestDay = 0;
    document.write(' Array to read in a number of data items');

    for (var day = 0; day <bookArray.length; day = day + 1) {
    bookArray[day] = window.prompt('Enter books value for day ' + day,'')
    // Update highestNum and highestDay if highestNum is greater than previous value
    if (bookArray[day] > highestNum) {
    highestNum = parseInt(bookArray[day]); // number not string
    highestDay = day;
    }
    }
    document.write('<BR>' + '<BR>');
    document.write('confirmation of data input' + '<BR>' + '<BR>');

    for (var day = 0; day < bookArray.length; day = day + 1) {
    document.write(bookArray[day] + '<BR>');
    }
    document.write("<BR>" + "The highest number of books was " + highestNum + " on day " + highestDay);

    </script>

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Do you mean something like this?

    Code:
    <script type = "text/javascript">
    /* Program to read in a known number of data and store them in an array */
    
    var days=["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
    
    var bookArray = new Array (5);
    var highestNum = 0;
    var highestDay = 0;
    
    onload=function test(){
    document.getElementById("display").innerHTML='Array to read in a number of data items';
    
    for(var dayNum = 0; dayNum<bookArray.length; dayNum++){
    
    bookArray[dayNum] = window.prompt('Enter books value for day ' + days[dayNum],'')
    
    // Update highestNum and highestDay if highestNum is greater than previous value
    if (bookArray[dayNum] > highestNum) {
    highestNum = parseInt(bookArray[dayNum]); // number not string
    highestDay = days[dayNum];
    }
    
    }
    document.getElementById("display").innerHTML+='<BR>' + '<BR>';
    document.getElementById("display").innerHTML+='confirmation of data input' + '<BR>' + '<BR>';
    
    for(var dayNum = 0; dayNum < bookArray.length; dayNum++) {
    document.getElementById("display").innerHTML+=bookArray[dayNum] + '<BR>';
    }
    document.getElementById("display").innerHTML+="<BR>" + "The highest number of books was " + highestNum + " on " + highestDay;
    }
    
    </script>
    
    <div id="display"></div>
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #3
    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
    I'm not sure that this chap has got as far as innerHTML yet.

    And surely the first day of the week is Sunday, not Monday?

    I think what he was looking for at this stage was more like:-

    <html>
    <head>
    <title>Arrays</title>
    <script type = "text/javascript">
    /* Program to read in a known number of data and store them in an array */

    var dayNames = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

    var bookArray = new Array (6);
    var highestNum = 0;
    var highestDay = 0;

    document.write(' Array to read in a number of data items');

    for (var day = 1; day <bookArray.length; day = day + 1) {
    bookArray[day] = window.prompt(' enter books value for day ' + day,'')

    if (isNaN(bookArray[day])) {
    alert ("Invalid value!")
    }

    if (bookArray[day] > highestNum) {
    highestNum = parseInt(bookArray[day]); // number not string
    highestDay = day;
    }

    }
    document.write('<BR>' + '<BR>');
    document.write('confirmation of data input' + '<BR>' + '<BR>');

    for (var day = 1; day < bookArray.length; day = day + 1) {
    document.write(bookArray[day] + '<BR>');
    }
    document.write("<BR>" + "The highest number of books was " + highestNum + " on day " + highestDay + " which is " + dayNames[highestDay]);

    </script>
    </head>
    <body>
    </body>
    </html>
    Last edited by Philip M; 06-15-2007 at 03:59 PM.

  • #4
    New Coder
    Join Date
    Jun 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hi Philip, i`ve copied and run it but it still gives the day`s as number values not actual names your help once again would be much appreciated best regards Neil

  • #5
    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
    Well, it works for me.

    An example of the output:-

    Array to read in a number of data items

    confirmation of data input

    25
    12
    54
    34
    17

    The highest number of books was 54 on day 3 which is Wednesday

  • #6
    New Coder
    Join Date
    Jun 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hi Philip, very sorry your dead right, i was expecting it to display the day names in the dialouge box on top of the window, can that be changed so that the user see`s it also? as alway`s amazed at your knowledge, something i can only aspire to (still having fun with the other codes you so kindly posted, i will get there honest, well i hope so) thank`s again Neil

  • #7
    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
    bookArray[day] = window.prompt('Enter books value for day ' + day + " which is " + dayNames[day],'')

    Can I very respectfully point out that the apostrophe before an s in English normally means possession - e.g. Philip's book (which is a contraction of 'Philip his book').
    Plurals just take an s (no apostrophe). E.g. Many thanks!

    But it's with an apostrophe is a contraction of 'it is' - e.g. It's a book!
    Its without an apostrophe is possessive - e.g. Always include its parameters.

    Sorry to mention this, but accuracy in language is important as well as in JavaScript. It can often make a big difference to the meaning.

    Consider: Those weird-looking things over there are my husbands (husband's).

    I once saw a sign for Fi'sh and Chip's!

  • #8
    New Coder
    Join Date
    Jun 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Think I better put down for English in September as well? thanks again Neil

  • #9
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    And surely the first day of the week is Sunday, not Monday?
    Yep my mistake, I suppose my post does deserve to be ignored
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.


  •  

    Posting Permissions

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