I am having difficulties solving the following programming problem:
Write a function that keeps track of guests that are going to a house party. You will be given an array of strings. Each string will be one of the following:
- {name} is going!
- {name} is not going!
If you receive the first type of input, you have to add the person if he/she is not in the list (If he/she is in the list print: {name} is already in the list! If you receive the second type of input, you have to remove the person if he/she is in the list (if not, print:{name} is not in the list!).
At the end print all the guests each on a separate line.
The assignment is to solve it with array methods, for loops, for each, for of…anything that works.
I know it might be too simple for this website and I’m sorry but I have been struggling with it for too many hours and unfortunately this is as far as I can go with the code… My problem is that I can't seem to divide it into small steps and execute them with array methods and loops...
function houseParty(input) {
let guestsList = [];
let notComing = [];
for (let i = 0; i < input.length; i++) {
if (input[i].includes('not')) {
notComing.push(input[i]);
} else {
guestsList.push(input[i]);
}
}
}
houseParty(['Allie is going!',
'George is going!',
'John is not going!',
'George is not going!'
])
This is an example of an input:
[Tom is going!,
Annie is going!,
Tom is going!,
Garry is going!,
Jerry is going!]
And this is the expected output:
Tom is already in the list!
Tom
Annie
Garry
Jerry
I would be very happy if you could explain to me the logic behind the programming problem and how you guys 'translate' it into small steps so that the program does what needs to be done.