Imagine a stable helper that knows four things perfectly: how to greet you, what horses like, whether carrots make good treats, and one truly terrible horse joke. It is not guessing and it is not using AI. You write every answer yourself, store the answers under useful names, and let the program fetch the right one.
A prompt is the message a person sends to a program. In this lesson, it is whatever you type into WhinnyBot's input box. Our first bot is deliberately simple, so a good prompt is a short keyword that matches something the bot knows.
carrotIn JavaScript, a dictionary is usually called an object. It stores pairs: a key is the lookup word, and its value is the information stored under that word.
const answers = {
"hello": "Neigh there! Welcome to the stable.",
"horse": "Horses can sleep standing up or lying down.",
"carrot": "Carrots are treats, not a whole horse dinner.",
"joke": "Why did the pony whisper? It was a little horse!",
"default": "I do not know that one yet. Teach me!"
};
The colon means “stores this value”, and the comma separates one pair from the next. To look up an answer, put the prompt inside square brackets:
let answer = answers[prompt];
If prompt contains "carrot", JavaScript fetches the value stored at answers["carrot"].
Write the object above your functions so every part of the program can use it.
createInput() makes a place to type. createButton() makes the send button. This bot needs no canvas, so use noCanvas().
toLowerCase() makes HORSE match horse. trim() removes accidental spaces.
Ask the dictionary for answers[prompt]. If the result is undefined, use answers.default.
const answers = {
"hello": "Neigh there! Welcome to the stable.",
"horse": "Horses can sleep standing up or lying down.",
"carrot": "Crunchy, orange, and best as a small treat!",
"joke": "Why did the pony whisper? It was a little horse!",
"default": "I do not know that one yet. Teach me!"
};
let promptInput;
let replyText;
function setup() {
noCanvas();
promptInput = createInput("");
promptInput.attribute("placeholder", "Try: hello, horse, carrot, joke");
let sendButton = createButton("Ask WhinnyBot");
sendButton.mousePressed(answerPrompt);
replyText = createP("Neigh there! Ask me something.");
}
function answerPrompt() {
let prompt = promptInput.value().toLowerCase().trim();
let answer = answers[prompt];
if (answer === undefined) {
answer = answers.default;
}
replyText.html(answer);
promptInput.value("");
}
drawing, martial arts, and a secret password."best horse". Remember that a key containing spaces needs quotation marks.keyPressed() or an event listener fits best.It always uses the fallback: check that the typed prompt exactly matches a key. Look for capital letters, spaces, and spelling.
The code stops immediately: check every comma, colon, quote, brace, and parenthesis. The browser console will usually point near the first broken symbol.
The button does nothing: write mousePressed(answerPrompt), without parentheses after answerPrompt. You are giving p5.js the function to run later.