Skip to content

Instantly share code, notes, and snippets.

@BugKiller-tech
Last active September 13, 2022 11:58
Show Gist options
  • Select an option

  • Save BugKiller-tech/5db4bc6242cc1629c48b99a2202ff8c4 to your computer and use it in GitHub Desktop.

Select an option

Save BugKiller-tech/5db4bc6242cc1629c48b99a2202ff8c4 to your computer and use it in GitHub Desktop.
/*
  below there is a data collection of transport items objects. 
  inside the data collection there are random transport items.
  some of the transports has other related transport items, 
  each transport item data object has amount of cargo items and price.
  
  solve the following 2 tasks. 
  any web page on the internet can be used as helping reference for these tasks.
  main objective is to supply a working solution !
  next objectives are the readability of code, elegance of implementation and efficiency.
*/

let transport_items = [
  {id: 4, parent_id: null, transport_price: 190, cargo_items: 1},
  {id: 2, parent_id: 7, transport_price: 200, cargo_items: 2},
  {id: 7, parent_id: null, transport_price: 210, cargo_items: 1},
  {id: 5, parent_id: 9, transport_price: 100, cargo_items: 2},
  {id: 9, parent_id: 6, transport_price: 100, cargo_items: 2},
  {id: 12, parent_id: null, transport_price: 200, cargo_items: 2},
  {id: 24, parent_id: null, transport_price: 450, cargo_items: 2},
  {id: 54, parent_id: 4, transport_price: 220, cargo_items: 1},
  {id: 49, parent_id: null, transport_price: 280, cargo_items: 2},
  {id: 88, parent_id: null, transport_price: 240, cargo_items: 2},
  {id: 100, parent_id: 5, transport_price: 100, cargo_items: 2},
  {id: 22, parent_id: null, transport_price: 200, cargo_items: 2},
  {id: 6, parent_id: null, transport_price: 100, cargo_items: 2},
];

/* 

	[15 minutes]
  1. 	filter the collection for transport items which has number of cargo items 
  		bigger than the avarage of all cargo items iniside transport_items collection.
      after getting the filtered data collection update html label - number_of_transport_items_after_filter_process
      and populate the label with current available number of transport items (using jQuery).
*/

// code implementation ... ->
let total_cargo_items_count = 0;
transport_items.map(item => {
	total_cargo_items_count += item.cargo_items
})
const average_cargo_items_count = total_cargo_items_count / transport_items.length;
const filtered_transport_items = transport_items.filter(item => item.cargo_items > average_cargo_items_count)
const number_of_filtered_transport_items = filtered_transport_items.length;


$('#number_of_transport_items_after_filter_process').text(number_of_filtered_transport_items)
/*

	[45 minutes]
  2. 	first build a logic which will calculate the longest chain of related transport items inside the filtered 
  		transport_items collection, calculate the longest chain of transports 
      than aggregate the sum of prices for the whole longest chain 
      and populate the label with total price (using jQuery).
*/

// code implementation ... ->
const getRoutes = (filtered_transports, pointId, routes) => {
	const found = filtered_transports.find(item => item.id == pointId);
  if (found ) {
  	routes.push([found.id, found.transport_price]);
    if (found.parent_id) {
    	getRoutes(filtered_transports, found.parent_id, routes);
    }
  }
}

const getPriceOfLongestChainOfTransports = (filtered_transports) => {
  let longestRoutes = [];
	for (const item of filtered_transports) {
  	let routes = []
  	getRoutes(filtered_transports, item.id, routes)
    if (routes.length >= longestRoutes.length) {
    	longestRoutes = routes;
    }
  }
  const totalPrice = longestRoutes.reduce((rlt, item) => rlt + item[1], 0);
  $('.total_price_for_longest_chain').text(totalPrice)
}

const totalPriceOfLongestChain = getPriceOfLongestChainOfTransports(filtered_transport_items);











Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment