Dr. Mark Humphrys

School of Computing. Dublin City University.

Home      Blog      Teaching      Research      Contact

My big idea: Ancient Brain

Search:

CA114      CA170

CA668      CA669      Projects


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 = [ 
	"http://humphrysfamilytree.com/Icons/logo.rotate.2.jpg", 
	"http://upload.wikimedia.org/wikipedia/commons/5/5d/Mona_Lisa_Portrait.png",
	"https://computing.dcu.ie/~humphrys/3d/Images/earth.1.big.jpg"
	];

 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      

Advantages:
  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:


        <script>
            while ( true ) { }
        </script>

Or this:


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

  

Loops in Javascript




ancientbrain.com      w2mind.org      humphrysfamilytree.com

On the Internet since 1987.

Wikipedia: Sometimes I link to Wikipedia. I have written something In defence of Wikipedia. It is often a useful starting point but you cannot trust it. Linking to it is like linking to a Google search. A starting point, not a destination. I automatically highlight in red all links to Wikipedia and Google search and other possibly-unreliable user-generated content.