Given an array mart of objects in the prefilled code and categoryOfItem, item as inputs, create a JS promise.

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

Output

The output should be a single line string with the appropriate message

Sample Input 1
pulses
green gram

Sample Output 1
Item Found

Sample Input 2
detergents
tide

Sample Output 2
Category Not Found

Answer

"use strict";

process.stdin.resume();
process.stdin.setEncoding("utf-8");

let inputString = "";
let currentLine = 0;

process.stdin.on("data", (inputStdin) => {
  inputString += inputStdin;
});

process.stdin.on("end", (_) => {
  inputString = inputString.trim().split("\n").map((str) => str.trim());
  main();
});

function readLine() {
  return inputString[currentLine++];
}

/* Please do not modify anything above this line */

function main() {
  const categoryOfItem = readLine();
  const item = readLine();

  const mart = [
    {
      category:"pulses",
      items: ["green gram", "green peas", "Turkish gram"]
    },
    {
      category:"soaps",
      items:["santoor", "dove", "lux", "pears"]
    }, 
    {
      category:"oil",
      items: ["sunflower oil", "grapeseed oil", "soybean oil"]
    }, 
    {
      category:"cereals",
      items: ["wheat", "rice", "maize", "oat"]
    }, 
    {
      category:"spices",
      items: ["cumin", "coriander", "black pepper", "clove"]
    }
  ];
  
  /* Write your code here */
  const checkCategoryItems = () => {
      return new Promise((resolve, reject) => {
        const isMartHasCategory = mart.find(obj => obj.category === categoryOfItem);
        
        if (isMartHasCategory !== undefined) {
            let {category,items} = isMartHasCategory;
            let itemFound = items.includes(item);
                if(itemFound) {
                    resolve('Item Found');
                } else {
                    reject('Item Not Found');
                }
        } 
        else {
            reject('Category Not Found');
        }
  });
  }
  
  
  const myPromise = async () => {
      try {
          const response = await checkCategoryItems();
          console.log(response);
      } catch (error) {
          console.log(error);
      }
  }

  myPromise();
  
}