Skip to content

Instantly share code, notes, and snippets.

@leonidkuznetsov18
Last active May 2, 2021 21:28
Show Gist options
  • Select an option

  • Save leonidkuznetsov18/ca14338d80173f609303314767fff20d to your computer and use it in GitHub Desktop.

Select an option

Save leonidkuznetsov18/ca14338d80173f609303314767fff20d to your computer and use it in GitHub Desktop.
Children on the Flight
// Task 1. Children on the Flight.
// We are building Web application for the airlines.
// There is a page where we show list of passengers
// for the given flights.
// There is an age restriction:
// All children younger than 5 years old should have
// at least one adult parent or guardian.
// Parent/guardian should be at least 18 years old.
// We want to highlight all children who are not
// allowed to be on the flight.
// Tasks:
// 1. Please suggest backend API (interface only) to
// return list of passengers for this screen.
// 2. Write the function to find all children who are not
// allowed to be on the flight.
interface Passenger {
id: number;
name: string;
year: number;
guardian: number;
}
const mockData: Passenger[] = [
// case 1 with guardian
{
id: 1,
name: "Ivan",
year: 4,
guardian: 2,
},
{
id: 2,
name: "Leo",
year: 20,
guardian: -1,
},
// case 2 without guardian
{
id: 3,
name: "Julia",
year: 4,
guardian: -1,
isRestricted: true,
},
{
id: 4,
name: "Egor",
year: 20,
guardian: -1,
isRestricted: true,
},
// case 3 young guardian
// child
{
id: 5,
name: "Svetlana",
year: 4,
guardian: 6,
isRestricted: true,
},
// guard
{
id: 6,
name: "Vadim",
year: 17,
guardian: -1,
},
];
const res = mockData.filter((p) => p.isRestricted);
const case1 = [];
const case2 = mockData.filter((p) => p.year < 5 && p.guardian === -1);
const case3 = [];
const obj = mockData.reduce((acc, curr) => {
acc.set(curr.guardian, {id: curr.id, name: curr.name, year: curr.year});
// acc[curr.guardian] = curr;
return acc;
}, new Map());
mockData.forEach((p) => {
if (p.guardian !== -1 && p.year < 5) {
const guard = obj.get[p.guardian];
if (guard.year <= 18) {
case3.push(p);
}
}
});
console.log("case1", case1);
console.log("case2", case2);
console.log("case3", case3);
console.log("res", res);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment