Skip to content

Instantly share code, notes, and snippets.

@lee2sman
Last active April 18, 2023 20:11
Show Gist options
  • Save lee2sman/f8c8ea26d90753db33a905a3be4b0dab to your computer and use it in GitHub Desktop.
Save lee2sman/f8c8ea26d90753db33a905a3be4b0dab to your computer and use it in GitHub Desktop.
example code snippets, from office hours with Fidelis, to select random images from an array, random text, etc
let myImages = [
"image1.jpg",
"image2.jpg",
"image3.jpg"
]
//NOTE:
//ALL CODE SNIPPETS WITH $ BELOW ARE USING jQUERY!
//
//replace the html of the page with a specified image
$("body").html("<img src='image1.jpg' >")
//replace the html with the first image (0 index) listed in the array
$("body").html("<img src='" + myImages[0] + "'>")
//picking a random integer in p5.js
int(random(1,10))
//javascript, an integer between 0 and 9
Math.floor(Math.random() * 10)
//javascript, a random number of an array
Math.floor(Math.random() * myImages.length)
//random element from array
myImages[Math.floor(Math.random() * myImages.length)]
//
let artworks = [
"glitch.com/oercuherche/orecuhecreu/oercuhercupricoeu.jpg",
"myotherart.png"
"aperformanceIdid.gif",
"something.jpg"
]
let descriptions = [
"Artwork1 was made in 2021. I used graphite and grass clippings to....",
"Myotherart is a multidimensional hyperperformance of .....",
"aperformance i did is ....",
"something was my first sculpture to feature glue and albatross"
]
///pick a random integer
let whichArtwork = Math.floor(Math.random() * artworks.length)
//add the image at that arrray index to the end of the page
$("body").append('<img src="' + artworks[whichArtwork] + '">')
//add the text about that image from the array, also to the end of the page
$("body").append("<p>" + descriptions[whichArtwork] + "</p>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment