I'm working with an API to gather information about books, including the title and author. However, I've noticed that some authors' data is returned as 'undefined'. I had the idea of using regular expressions (RegExp) to replace this undefined value with 'N/A', but I am encountering difficulties implementing it.
/* within a function */
function fetchingAsyncData() {
/* data fetching process */
const responseData = await response.json();
const foundBooks = [];
for (let i = 0; i < responseData.items.length; i++) {
let bookTitle = responseData.items[i].title;
let bookAuthor = responseData.items[i].author;
if (typeof bookTitle === 'undefined' || typeof bookAuthor === 'undefined') {
// Attempting to replace 'undefined' with 'N/A'
}
foundBooks.push(`${responseData.items[i].title} by ${responseData.items[i].authors}`);
}
return foundBooks;
}
An example of the current output: ["Cats by undefined"], however I am aiming for the desired result: ["Cats by N/A"].