-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
55 lines (47 loc) · 1.99 KB
/
main.js
File metadata and controls
55 lines (47 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const { sampleAstronauts, sampleShips } = require('./astronautsData.js');
function lookupSpaceship(shs, sn) {
return shs.find((spaceship) => spaceship.name === sn);
}
function assignToSpaceships(astronauts, spaceships) {
const notYetAssigned = [];
const sortedAstronauts = astronauts.sort((a1, a2) =>
a1.applicationReceived < a2.applicationReceived);
sortedAstronauts.forEach((index, a) => {
const firstChoiceShip = lookupSpaceship(spaceships, a.preferredShip[0]);
if (firstChoiceShip.capacity <= firstChoiceShip.assignedAstronauts.length) {
notYetAssigned.push(a);
} else {
firstChoiceShip.assignedAstronauts.push(a);
}
});
notYetAssigned.forEach((index, a1) => {
const secondChoiceShip = lookupSpaceship(spaceships, a.preferredShip[1]);
if (secondChoiceShip.capacity === secondChoiceShip.assignedAstronauts.length) {
const thirdChoiceShip = lookupSpaceship(spaceships, a.preferredShip[2]);
thirdChoiceShip.assignedAstronauts.push(a);
} else {
secondChoiceShip.assignedAstronauts.push(a);
}
});
}
function formatNotification(astronaut, spaceship) {
return `Congratulations ${astronaut}. You have been assigned to ` +
`journey into the heart of the universe on the Spaceship ${spaceship.name}. ` +
`Please be prepared to start your journey on ${spaceship.expeditionDate.getMonth() + 1}/` +
`${spaceship.expeditionDate.getDate()}/${spaceship.expeditionDate.getFullYear()}.`;
}
function notifySpaceshipAssignments(spaceships, astronauts) {
assignToSpaceships(astronauts, spaceships);
spaceships.forEach((spaceship) => {
spaceship.assignedAstronauts.forEach((astronaut) => {
console.log(formatNotification(astronaut, spaceship));
});
});
}
/* Calling the main function */
notifySpaceshipAssignments(sampleAstronauts, sampleShips);
/* exports */
module.exports = {
assignToSpaceships,
formatNotification
};