-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
70 lines (58 loc) · 2.73 KB
/
script.js
File metadata and controls
70 lines (58 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* ====================================================
LiveLab 10 – AI Recommendation Engine
Student Starter Code
YOUR MISSION: Complete the TODOs below to build
your AI-powered recommendation engine!
======================================================== */
// STEP 1: Select DOM Elements (COMPLETED FOR YOU)
const button = document.getElementById("askBtn");
const input = document.getElementById("userInput");
const responseDiv = document.getElementById("response");
// STEP 2: Add Event Listener (COMPLETED FOR YOU)
button.addEventListener("click", async () => {
const userQuestion = input.value.trim();
// ====================================================
// TODO STEP 3: Implement Error Handling
// Check if the input is empty. If it is, show a
// friendly message and return early.
//
// HINT: Use an if statement to check if userQuestion is falsy
// HINT: Set responseDiv.textContent to a friendly message
// HINT: Use return to stop execution
// ====================================================
// YOUR CODE HERE
// Show loading message while waiting for AI
responseDiv.textContent = "Thinking...";
// ====================================================
// TODO STEP 4: Connect to the AI
// Complete the fetch request below to send the user's
// question to your Cloudflare Worker and display the response.
// ====================================================
try {
// TODO: Replace YOUR_CLOUDFLARE_WORKER_URL with your actual Worker URL
const res = await fetch("YOUR_CLOUDFLARE_WORKER_URL", {
// TODO: Add method: "POST"
// TODO: Add headers: { "Content-Type": "application/json" }
// TODO: Add body: JSON.stringify({ message: userQuestion })
});
// TODO: Check if response is ok. If not, throw an error
// HINT: if (!res.ok) throw new Error(`HTTP error! Status: ${res.status}`);
// TODO: Parse the JSON response and store it in a variable called 'data'
// HINT: const data = await res.json();
// TODO: Display the AI's response in responseDiv
// HINT: responseDiv.textContent = data.reply;
// NOTE: Your Worker might return data.reply or data.message - check your Worker code!
} catch (error) {
// TODO: Handle errors - show a friendly message and log the error
// HINT: console.error("Error:", error);
// HINT: responseDiv.textContent = "Oops! Something went wrong. Try again.";
}
});
/* =====================================
NEXT STEPS AFTER COMPLETING TODOs:
1. Test your app with sample queries
2. Add custom styling to match your concept
3. OPTIONAL: Display multiple responses (last 3)
4. OPTIONAL: Add chat bubble styling
5. OPTIONAL: Add emojis or themed design
===================================== */