Dr. Mark Humphrys

School of Computing. Dublin City University.

Home      Blog      Teaching      Research      Contact


CA216      CA249      CA318

CA400      CA651      CA668

Lab - Javascript

Lab - Random image each time page is loaded

Exercise - Re-code random logo using Array

Re-code the random logo using an Array of image URL strings:

 var thearray = [ 

 var i = Math.floor ( Math.random() * thearray.length );	// returns random 0 to (thearray.length - 1)	 

 var image = ....   // get the ith element of the array

 ....   // display the image inline      

  1. Can have any filenames. No need for careful naming.
  2. No MAX needed.
  3. Can add files by adding line to Array. No adjustment to MAX needed.
  4. Can remove files by removing line from Array. No re-naming/re-numbering needed. No adjustment to MAX needed.

Lab - Mouse over changes image

Test - infinite loop

Remote site sending you code to execute is a strange idea.

What happens if a remote site sends you an infinite loop? Does it crash your browser?

Try this:

            while ( true ) { }

Or this:

            var i = 1;
            while ( i > 0 ) { }


Loops in Javascript

Feeds      w2mind.org

On Internet since 1987.