check if user input matches any item in array via index


Tue Aug 08 2023 01:32:52 GMT+0000 (Coordinated Universal Time)

Saved by @thecowsays #react.js

// vvv--- shown to aide explanation

// const Library = (props) => {
//  const books = [
//  "The Adventures of Herlock Sholmes",
//  "The Life and Lessons of Manny Mole",
//  "My Brother's Book"
//  ];

const response = (books.indexOf(props.request) > -1)
 ? <p>We have that book!</p>
 : <p>Sorry, we don't have that book.</p>

// vvv--- shown to aide explanation of props.request, don't include it

// ReactDOM.render(
// <Library // request={"The Adventures of Herlock Sholmes"} // />,
//  document.getElementById("root")
// );

The book being sought is *"The Adventures of Herlock Sholmes"* and it does indeed exist in the `books` array and can be accessed via `books[0]`. The conditional statement checks the array for any index which has a value matching the `props.request`. Recall that the first index of an array is 0 and so `(props. Request) > -1`. *Courtesy Mimo Bootcamp*