write code according to requirement: Requirements: Create new page in pages folder Add page into the pages.jsx under main For path property use /search/* In page use const location = useLocation(); to determine the search term Display list of matching results Each course should link to /course/courseCode If the search results are empty write not found message. Implement the above using the following input: const searchResults = [ // array will be empty if no matches were found. { courseCode: string, description: string }, { courseCode: string, description: string }, { courseCode: string, description: string }, { ... }, { ... }, { ... }, ... ];
GPT-3.5
GPT-6 Astra
1. Create pages/Search.jsx
This assumes search URLs look like /search/javascript.
import { Link, useLocation } from "react-router-dom";
export default function Search({ searchResults = [] }) {
const location = useLocation();
const encodedTerm = location.pathname.replace(/^\/search\/?/, "");
let searchTerm;
try {
searchTerm = decodeURIComponent(encodedTerm);
} catch {
searchTerm = encodedTerm;
}
return (
<section>
<h1>Search results{searchTerm ? ` for "${searchTerm}"` : ""}</h1>
{searchResults.length === 0 ? (
<p>No matching courses found.</p>
) : (
<ul>
{searchResults.map(({ courseCode, description }) => (
<li key={courseCode}>
<h2>
<Link to={`/course/${encodeURIComponent(courseCode)}`}>
{courseCode}
</Link>
</h2>
<p>{description}</p>
</li>
))}
</ul>
)}
</section>
);
}
2. Add the route in pages.jsx under the existing main route
The following uses React Router v6. Keep your existing main layout import and other routes.
import { Route, Routes } from "react-router-dom";
import Main from "./layouts/Main"; // Use your existing main layout path.
import Search from "./pages/Search";
export default function Pages({ searchResults = [] }) {
return (
<Routes>
<Route path="/" element={<Main />}>
{/* Other existing main routes */}
<Route
path="/search/*"
element={<Search searchResults={searchResults} />}
/>
</Route>
</Routes>
);
}
Pass your provided results array into Pages:
<Pages searchResults={searchResults} />
The main layout must render an <Outlet /> for nested pages. This implementation treats searchResults as the already-matched results for the current search term.
What changed
The sharpest difference is how each read the input. The prompt says the array “will be empty if no matches were found,” meaning the results arrive already matched. GPT-3.5 filters them a second time anyway, and it calls toLowerCase() on a search term that would be undefined at a bare /search URL. Astra says outright that it “treats searchResults as the already-matched results” and simply renders them. One could argue GPT-3.5’s filtering at least gives useLocation a real job, since Astra uses the term only for a heading. But Astra’s reading fits the stated input.
Astra also follows the requirements more closely. It nests the route “under the existing main route” and notes that Main needs an <Outlet />. GPT-3.5 instead invents Home, About and Contact routes. Astra uses <Link> rather than GPT-3.5’s plain <a href>, which reloads the whole page, and it adds URL decoding and encoding. GPT-3.5’s React Router v5 Switch was reasonable in early 2023, so that difference is about timing, not a mistake.