بتل کد Battle Codeدارد باز می‌شود…

جواب «Nothing Found» چالش 4 از جلسه‌ی 24

سؤال

جست‌وجوی حیوان‌ها کار می‌کند. دو چیز به آن اضافه کن که هر دو از روی shown حساب می‌شوند:
- یک p با id برابر info که بنویسد چند مورد از چند مورد دیده می‌شود: 4 of 4
- وقتی هیچ موردی پیدا نشد، یک p با id برابر empty و متن Nothing found. در بقیه‌ی وقت‌ها روی صفحه نباشد.

راهنما

<p id="info">{shown.length} of {animals.length}</p> و {shown.length === 0 && <p id="empty">Nothing found</p>}

یک جواب درست

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
ul { list-style: none; padding: 0; }
li { margin: 6px 0; }
</style>

<div id="root" dir="ltr"></div>

<script type="text/jsx">
import { useState } from "react";
import { createRoot } from "react-dom/client";

function App() {
  const animals = ["cat", "dog", "cow", "fox"];
  const [query, setQuery] = useState("");

  const shown = animals.filter((animal) => animal.includes(query.toLowerCase()));

  return (
    <div>
      <input id="search" placeholder="Search" value={query} onChange={(e) => setQuery(e.target.value)} />
      <p id="info">{shown.length} of {animals.length}</p>
      {shown.length === 0 && <p id="empty">Nothing found</p>}
      <ul id="list">
        {shown.map((animal) => (
          <li key={animal}>{animal}</li>
        ))}
      </ul>
    </div>
  );
}

createRoot(document.getElementById("root")).render(<App />);
</script>

اول خودت امتحان کن؛ جواب‌های دیگر هم قبول است. برگرد و خودت حلش کن