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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Script to Diplay random images.

    I am stuck on some javascript homework I realize it is probably really easy but my stupid textbook is not clear on one point. I have to access code in an external javascript page (random.js) which I have done. But then i have to create a second script element to create the function showlmg() and within that I require statements to declare a variable named imgNumber equal to the value returned by the randomInteger() (which is from the external sheet found in data files from the textbook therefore I know they are right). I am supposed to use 9 as the value of the size parameter in the randomInteger() function. The second statement should be command that writes the following text to the document: <img src='imgNumber.jog' alt= ' '/>. So basically the coding I have done is :

    Code:
    <script type= "text/javascript" src= random.js"> </script>
    <script type="text/javascript">
            function showlmg() {
    		
    		var imgNumber= randomInteger(9);// Return a random number from 0 to 9.
    		document.write("<img src='imgNumber.jpg' alt=''/>");
    		}
         </script>
    all in the head of the document and then
    Code:
    function randomInteger(size) {
       return Math.floor((size+1)*Math.random());} from the external file.
    and

    Code:
    <tr>
       <td colspan="2" class="center">
       <script type= "text/javascript">
       showlmg();
       showlmg();
       showlmg();
       showlmg();
       showlmg();
       </script>
       </td>
    </tr>
    to post in a table on the page... I believe the issue is in the declaring a variable the is equal to the result of the function random integer which the text book or anywhere that I can find online fails to explain how to do. Thank you.
    Last edited by kobyskid; 01-19-2009 at 07:39 PM.

  • #2
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    In JavaScript you always have to break your variables out of strings so you should have
    Code:
    document.write("<img src='"+imgNumber+".jpg' alt=''/>");
    instead of
    Code:
    document.write("<img src='imgNumber.jpg' alt=''/>");

  • #3
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Did that but the random images still don't appear

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,086
    Thanks
    38
    Thanked 498 Times in 492 Posts
    What are the names of your image file?

    1.jpg to 9.jpg
    or
    imgNumber1.jpg to imgNumber9.jpg

    Whatever their names, are they located in the same directory as the script?

    Are you getting any errors in the error console?
    Do you have a link to a test site?

  • #5
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    image names are 1.jpg to 9.jpg no test link but they are in the same folder

  • #6
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,086
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    The following works in FF and IE as is.
    Check it out to be sure, then remove the 3 lines to the external 'random.js' file
    and uncomment the external script call. All should work as before
    Code:
    <html>
    <head>
    <title>Random Images</title>
    <!-- script type= "text/javascript" src= random.js"> </script -->
    <script type="text/javascript">
    function randomInteger(size) {
       return Math.floor((size+1)*Math.random()); 	// from the external file.
    }
    function showlmg() {
      var imgNumber= randomInteger(9);// Return a random number from 0 to 9.
      document.write("<img src='"+imgNumber+".jpg' alt='"+imgNumber+".jpg' height='100' width='100' />");
    }
    
    // From: http://www.codingforums.com/showthread.php?p=772337#post772337
    </script>
    </head>
    <body>
    <h1>Random Images</h1>
    <table>
    <tr>
     <td colspan="2" class="center">
      <script type= "text/javascript">
       showlmg();
       showlmg();
       showlmg();
       showlmg();
       showlmg();
      </script>
     </td>
    </tr>
    </table>
    </body>
    </html>
    Make sure you do not have any HTML code in the external JS file.
    When all works, go back and check the differences line by line in your original script.

    Good Luck!


  •  

    LinkBacks (?)

    1. 04-21-2014, 04:49 PM

    Posting Permissions

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