Search Item in a Mart Given an array
mart of objects in the prefilled code and categoryOfItem, item as inputs, create a JS promise,
resolve with "Item Found" text, if the
categoryOfItem matches with the category and the corresponding items list includes the item
reject with "Category Not Found" text, if the
categoryOfItem does not match with any catergory in the mart
reject with "Item Not Found" text, if the
items list does not include item
Use async/await and try/catch blocks.
Quick Tip
You can use array methods find() and includes().
Input
The first line of input contains a string categoryOfItem
The second line of input contains a number item
Sample Input 1
pulses
green gram
Sample Output 1
Item Found
function main() {
const categoryOfItem = readLine();
const item = readLine();
const mart = [
{
category:"pulses",
items: ["green gram", "green peas", "Turkish gram"]
},
}
let mart = [];
let categoryOfItem;
let numberItem;
async promise = new Promise((resolve, reject) => {
try {
if (mart.includes(categoryOfItem)) {
numberItem = mart.find(categoryOfItem);
resolve("Item Found");
} else {
throw new Error();
}
} catch ( error ) {
reject("Item Not Found");
}
})
Comments
Leave a comment