Week 9: Electronic Literature + Intro to AI
To Do This Week:
Assignments:
Upcoming Assignments
- Electronic Literature (10%) (Due Week 11 — 11/04)
Using the JS Mentor Metaprompt
JS Mentor Metaprompt is for JS scripting assistance. Just upload to a new chat. You may generate the HTML and CSS outside of the chat, on your own or in another AI chat Think of it as your personal JavaScript tutor. It helps you learn by guiding you step by step (without giving the answer to the full solution).
Start Your JS Mentor Chat
- Open a new chat in ChatGPT or Claude.
- Upload the JS Mentor — Assignment Mode document.
- Copy and paste the following message into the chat:
"Read the attached JS Mentor — Assignment Mode document and follow its tutoring instructions for this conversation.
Begin with the required Assignment Mode signature, then ask me about my JavaScript assignment. Do not provide assignment solutions.""
- Send the message along with the attached document.
- Confirm that the AI responds with JS MENTOR — ASSIGNMENT MODE ACTIVE.
- Share your assignment instructions and begin working through your logic steps and JavaScript code.
Important: Start each new assignment chat with the JS Mentor document and activation message. The AI is your learning coach, not your code generator.
The rule for this class: AI supports your understanding — it does not replace it. You must be able to explain every line in your project. Always document your AI use by including the chat URL(s) in your comments.
Review
This project brings together everything so far — arrays, functions, loops, conditionals — and points it at text. Review as needed:
- Statements
- Variables
- Data Types
- Operators (basic and "shortcut" operators)
- Comparison Operators
- Arrays
- Functions
- For Loops, While Loops
- Conditional Statements
- Scope
- Array Methods
- Array Iterators
- Objects, Methods, and Properties
Electronic Literature
What is it?
Electronic literature (e-lit) is writing that depends on the computer — work that couldn't exist on paper because the code is part of the poem. A script selects, combines, transforms, or rearranges language; randomness and process produce text that shifts each time it runs or as the reader interacts.
These techniques all have practical uses — shuffling data, building forms, assembling shopping carts, generating Q&A. But in the next project you will manipulate language in an artistic way: making things happen in the text of the web page.
Examples of generative literature
- Taroko Gorge by Nick Montfort — the classic generative poem; view source to see how little code it takes
- "Refactored" Taroko Script (with comments)
- "Refactored" Taroko Page
- Taper: an online literary magazine for small computational pieces
Student E-Lit
- holly-slocum-bedbugs
- Noise — Tommy Ortale
- A Poem of Love & Unity Across Languages
- Elastic Text
- Tech Support
Taper: an online literary magazine for small computational pieces
- All code (HTML, CSS, JavaScript) must fit within 2KB (2048 bytes) — a extreme constraint to work against.
- Use a Minifier to compress code or Unminify for readability.
More Taper Projects
- if jupiter had turned into a star
- links
- chance infections
- night voyagers
- moons of jupiter
- verdigris
- returning thoughts
- hollywood hitmaker
- infinite scroll
- this bird has flown
- hail
- sense of existence
- hell is overthinking
- life plan
- nein finality
- wreckage of the infinite
- flaneur treadmill
Manipulating Text
Array Methods
- pop(): remove the last element
- push(): add an element to the end
- shift(): remove the first element
- unshift(): add an element to the beginning
- splice(): add or remove elements at a specific position
- slice(): copy part of an array into a new array
- sort(): sort array elements
- reverse(): reverse the order of elements
- includes(): check whether an array contains a value (true or false)
- indexOf(): find the position of an element (-1 if not found)
- join(): combine array elements into a string with a separator
- forEach(): run a function once for each array element
String Methods
- split(): turn a string into an array using a separator
- toLowerCase(): convert a string to lowercase
- toUpperCase(): convert a string to uppercase
- trim(): remove spaces from the beginning and end of a string
- charAt(): get a character at a specific position
- includes(): check whether a string contains certain text (true or false)
- indexOf(): find the position of text within a string (-1 if not found)
- replace(): replace part of a string with new text
- slice(): extract part of a string
Pick a Random Item from an Array
The engine of most generative text — choose one item at random:
const words = ["glass", "river", "signal", "ash"];
function pick(arr) {
const i = Math.floor(Math.random() * arr.length);
return arr[i];
}
console.log(pick(words)); // a different word each run
JavaScript Animation: Text in Motion
Open the E-lit Text Animation Examples demo to explore four examples that combine these techniques:
-
Falling Words:
Three words move down the page using
setInterval(). Clicking a word stops it. - Scrambling Letters: Hovering over a word generates random letters. Moving away selects a new word from an array.
- Snake Text: Individual letters follow the movement of the mouse.
- Word Clusters: Words scatter when hovered over and gather into new random clusters when the mouse leaves.
Methods & Properties
- getBoundingClientRect() — get an element's position and dimensions
- offsetWidth — measure an element's width
- offsetHeight — measure an element's height
- parseFloat() — convert a string to a decimal number
- setInterval() — repeat a function
- clearInterval() — stop a repeating function
JavaScript can be used to animate words, letters, and other HTML elements. Text can move across the screen, change rapidly, follow the mouse, or respond when someone clicks or hovers. Animation can introduce rhythm, surprise, and interactivity into a digital writing project.
There are two main approaches: JavaScript can repeatedly change an element's properties, or JavaScript can trigger a CSS transition. Both approaches can be combined with mouse events and random values.
JavaScript or CSS?
- Use JavaScript animation when you want repeated changes, random behavior, precise timing, or programmatic control over movement.
- Use CSS transitions when you want smooth movement, fading, scaling, or color changes between states.
- Combine both when JavaScript determines what happens and CSS controls how the visual change unfolds.
1. Animation with setInterval()
The setInterval() method runs a function repeatedly at
a specified time interval. Each time the function runs, it can
change an element's position, text, color, or other properties.
In this example, a word moves down the page by 2 pixels every 50 milliseconds.
const word = document.querySelector("#word");
let position = 0;
function moveWord() {
position += 2;
word.style.top = `${position}px`;
}
// Run the function every 50 milliseconds
let timer = setInterval(moveWord, 50);
The word needs position: absolute or
position: relative in CSS so that its
top property can be changed.
#word {
position: absolute;
top: 0;
}
Use clearInterval(timer) to stop the animation.
For example, clicking a moving word could stop it.
word.addEventListener("click", function() {
clearInterval(timer);
});
Objects and Object Literals
Objects store related properties (and methods) together — handy for bundling a fragment with its data (speaker, mood, weight).
JSON (JavaScript Object Notation) is a format for storing and exchanging data. It looks similar to a JavaScript object and behaves in a comparable way.
Simple Object Literal
// JavaScript Object Literal Example
const person = {
firstName: "Margie",
lastName: "Pepper",
age: 50,
eyeColor: "brown",
};
console.log(`Hi, ${person.firstName}`);
Object Literal with "this"
// JavaScript Object Literal Example
const person = {
firstName: "Henry",
lastName: "Smith",
age: 24,
eyeColor: "green",
fullName() {
return `${this.firstName} ${this.lastName}`;
}
};
console.log(`Hi, ${person.fullName()}`);
Object Constructors
An object constructor is a function used to create multiple objects with the same properties and methods. Instead of writing a separate object literal for each person, we can use a constructor to create as many people as we need.
Constructor functions are capitalized by convention. The
new keyword creates a new object, and this
refers to that new object.
// Constructor function
function Person(firstName, lastName, age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.fullName = function() {
return `${this.firstName} ${this.lastName}`;
};
}
// Create two objects using the constructor
const person1 = new Person("Margie", "Pepper", 50);
const person2 = new Person("Henry", "Smith", 24);
// Access properties and call methods
console.log(person1.firstName);
console.log(person2.age);
console.log(person1.fullName());
console.log(person2.fullName());
JavaScript Classes (ES6)
A class is a more modern way to define a structure for creating objects. It serves a similar purpose to a constructor function but uses clearer syntax.
The constructor() method runs automatically when
we create a new object with new. Other methods are
defined directly inside the class.
// Define a class
class Person {
constructor(firstName, lastName, age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
// A method shared by all Person objects
fullName() {
return `${this.firstName} ${this.lastName}`;
}
}
// Create two objects from the class
const person1 = new Person("Margie", "Pepper", 50);
const person2 = new Person("Henry", "Smith", 24);
// Access properties and call methods
console.log(person1.firstName);
console.log(person2.age);
console.log(person1.fullName());
console.log(person2.fullName());
Key distinction: An object literal creates one object directly. A constructor function or class provides a reusable structure for creating many objects.
Learn more about JSON and objects:
Open js-demo-object-class-lesson.html to see the examples in action.
Electronic Literature: 10% DUE: Week 11 — 11/04
Create a work of electronic literature that uses JavaScript to select, combine, transform, or rearrange letters, words, sentences, or passages. CSS should contribute meaningfully to the reading experience.
The piece should have a clear concept. It should feel like an intentional work of writing and design rather than only a technical demonstration. Possible influences include Oulipo, generative poetry, recombinant narrative, concrete poetry, and early net art.
Possible Approaches
- Generative poem or prose system
- Recombinant narrative
- Interactive or branching text
- Text-based game or puzzle
- Visual transformation of language through CSS
Minimum Requirements
- JavaScript-generated or transformed text
- Random selection, recombination, or another rule-based process
- Purposeful typography, spacing, color, and layout
- A project statement and numbered logic steps in JavaScript comments. At the top of the script.
- Documentation of any AI assistance (include your JS Mentor / ChatGPT chat URLs)
Development Steps
- Idea generation
- Conceptual breakdown
- Consult JS Mentor to see if your idea is possible
- Work out logic steps with JS Mentor
- JavaScript implementation, one step at a time
- Testing and refinement
- Styling with CSS
- Write the project statement*
- Final submission (Slack + Canvas)
*The Project Statement
A short paragraph at the top of your script (in a comment) naming your concept: what the piece does, the rule or process behind it, and what you want the reader to feel or notice. This is what turns a demo into a work.