JS: How to create function that return list of all restaurants with same price range?

Viewed 157

I have a program that have functions for creating restaurants and price ranges, and I need to return in function all restaurants that are with same price range, I am not sure how? This is my code, and name of function is findRestaurantsByPriceRange, I am trying to check if range category is same, but not sure how? NB: Filter is not Allowed to use. I would like to do it with for or foreach, if it is possible?

function createRestaurant(name, address, averageMealPrice, numberOfTable, category, priceRange, size) {
    return { name, address, averageMealPrice, numberOfTable, category, priceRange, size };
  }
  
  
  function createCategory(type) {
    return { type };
  }
  
  function createSizeOfRestaurant(size) {
    return { size };
  }
  
  
  let category1 = createCategory(['Chinese', 'International']);
  let category2 = createCategory(['Fast food', 'Middle Eastern']);
  let category3 = createCategory(['Balkan', 'Indian']);
  
  let size1 = createSizeOfRestaurant('small');
  let size2 = createSizeOfRestaurant('medium');
  let size3 = createSizeOfRestaurant('big');
  
  //THIS IS RANGE
  let range = ['inexpensive', 'moderate', 'expensive'];
  
  let restaurant1 = createRestaurant('Rosetto', 'Bulevar Oslobodjenja 2', 500, 20, category1, range[0], size1);
  let restaurant2 = createRestaurant('Rosetto', 'Bulevar Oslobodjenja 2', 500, 20, category2, range[1], size2);
  let restaurant3 = createRestaurant('Rosetto', 'Bulevar Oslobodjenja 2', 500, 20, category3, range[2], size3);
  
  let restaurants = [restaurant1, restaurant2, restaurant3];
  
  function findRestaurantsByPriceRange(restaurantByPrice, category) {
    const result = [];
  
    for (let i = 0; i < restaurantByPrice.length; i++) {
      let valid = false;
      const restaurant = restaurantByPrice[i];
      // To break the loop without it going through all the items in the array, I check if valid has been set to true. If so, the loop breaks
      for (let j = 0;
        (j < restaurant.category.type.length && valid === false); j++) {
        const restaurantCategory = restaurant.category.type[j];
        if (restaurantCategory === category) {
          valid = true;
        }
      }
  
      if (valid === true) result.push(restaurant)
    }
    
    return result;
  }
  
  function findRestaurantByCategory(restaurantsByCategory, category) {
    const result = [];
  
    for (let i = 0; i < restaurantsByCategory.length; i++) {
      let valid = false;
      const restaurant = restaurantsByCategory[i];
      for (let j = 0; (j < restaurant.category.type.length && valid === false); j++) {
        const restaurantCategory = restaurant.category.type[j];
        if (restaurantCategory === category) {
          valid = true;
        }
      }
  
      if (valid === true) result.push(restaurant)
    }
    
    return result;
  }

  function findRestaurantsBySize(restaurantsBySize, size){
    const result = [];
  
    for (let i = 0; i < restaurantsBySize.length; i++) {
      let valid = false;
      const restaurant = restaurantsBySize[i];
      for (let j = 0; (j < restaurant.size.type.length && valid === false); j++) {
        const restaurantSize = restaurant.size.type[j];
        if (restaurantSize === size) {
          valid = true;
        }
      }
  
      if (valid === true){ 
        result.push(restaurant)
      }
    }
    
    return result;
  }

  console.log(findRestaurantByCategory(restaurants, 'Chinese'))
  console.log(findRestaurantsBySize(restaurants, 'small'));

1 Answers

Array#includes and Array#some were used with the first two methods

Using Array#filter

function findRestaurantsByPriceRange(restaurantByPrice, category){
    return restaurantByPrice.filter(({ category: restaurantCategory }) => restaurantCategory.some(item => category.type.includes(item)));
}

Using Array#reduce

function findRestaurantsByPriceRange(restaurantByPrice, category) {
  return restaurantByPrice.reduce((acc, restaurant) => {
  acc.push(restaurant.category.some(item => category.type.includes(item)) ? restaurant : ...[])
  return acc;
  }, []);
}

Using a for loop

Here, you have to loop through the restaurantByPrice array and its category array, using a flag to check if any of the restaurant's category matches the one passed to the function.

function findRestaurantsByPriceRange(restaurantByPrice, category) {
  const result = [];
  
  for (let i = 0; i < restaurantByPrice.length; i++) {
    let valid = false;
    const restaurant = restaurantByPrice[i];
    
    // To break the loop without it going through all the items in the array, I check if valid has been set to true. If so, the loop breaks
    for (let j = 0; (j < restaurant.category.length && valid === false); j++) {
      const restaurantCategory = restaurant.category[j];

      if (restaurantCategory === category) {
        valid = true;
      }
    }

    if (valid === true) result.push(restaurant)
  }

function createRestaurant(name, address, averageMealPrice, numberOfTable, category, priceRange, size) {
  return { name, address, averageMealPrice, numberOfTable, category, priceRange, size };
}


function createCategory(type) {
  return { type };
}

function createSizeOfRestaurant(size) {
  return { size };
}


let category1 = createCategory(['Chinese', 'International']);
let category2 = createCategory(['Fast food', 'Middle Eastern']);
let category3 = createCategory(['Balkan', 'Indian']);

let size1 = createSizeOfRestaurant('small');
let size2 = createSizeOfRestaurant('medium');
let size3 = createSizeOfRestaurant('big');

//THIS IS RANGE
let range = ['inexpensive', 'moderate', 'expensive'];

let restaurant1 = createRestaurant('Rosetto', 'Bulevar Oslobodjenja 2', 500, 20, category1, range[0], size1);
let restaurant2 = createRestaurant('Rosetto', 'Bulevar Oslobodjenja 2', 500, 20, category2, range[1], size2);
let restaurant3 = createRestaurant('Rosetto', 'Bulevar Oslobodjenja 2', 500, 20, category3, range[2], size3);

let restaurants = [restaurant1, restaurant2, restaurant3];

function findRestaurantsByPriceRange(restaurantByPrice, category) {
  const result = [];

  for (let i = 0; i < restaurantByPrice.length; i++) {
    let valid = false;
    const restaurant = restaurantByPrice[i];
    // To break the loop without it going through all the items in the array, I check if valid has been set to true. If so, the loop breaks
    for (let j = 0;
      (j < restaurant.category.type.length && valid === false); j++) {
      const restaurantCategory = restaurant.category.type[j];
      if (restaurantCategory === category) {
        valid = true;
      }
    }

    if (valid === true) result.push(restaurant)
  }
  
  return result;
}

console.log(findRestaurantsByPriceRange(restaurants, 'Chinese'))


To check for size

function findRestaurantsByPriceRange(restaurantByPrice, size) {
  const result = [];

  for (let i = 0; i < restaurantByPrice.length; i++) {
    const restaurant = restaurantByPrice[i];
    if (restaurant.size.size === size) result.push(restaurant);
  }
  
  return result;
}
Related