0

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.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • Your assignment says "add the person if he/she is not in the list". Can you tell me where exactly you check if they are in the list already? – Roope Oct 24 '19 at 09:13
  • Hmm, I should probably make a copy of the original array (the input) and compare the guests who are coming with it to see if there are any repetitions of names? Do you think that would work? – VictoriaTodorova Oct 24 '19 at 09:14
  • You don't need to make a copy of anything. Just make the line that adds the person to the guest list conditional on that the person is not in the guest list. You already check if a string includes something. Checking if an array includes something works the same way https://stackoverflow.com/a/1473742/1425689 – Roope Oct 24 '19 at 09:24

4 Answers4

0

Your question is about the puzzle's logic and how to form steps to solve it. The ability to form the steps necessary is more of a way of thinking than anything else, it comes with practice. As for the logic, the solution can be implemented as follows:

  • create a collection (ie: array) of names that are going to the party
  • iterate over the input array
  • extrapolate from the message both the name of the person and if they are going or not.
  • if they are going and their name is not in the collection add their name to the collection
  • if they are not going and the name is in the collection then make sure their name is removed
  • after collecting your guests, iterate over your finalized collection and log your guests names to the console
foobar2k19
  • 84
  • 4
0

**Are you looking this?**Please follow the explanation from the @foobar2k19's answer.

function houseParty(input) {

    let guestsList = [];

    for (let i = 0; i < input.length; i++) {
        let nameOfThePerson = input[i].split(" ")[0];
        if (input[i].includes('not')) {
            if (guestsList.indexOf(nameOfThePerson) > -1) {
                guestsList.splice(guestsList.indexOf(nameOfThePerson), 1);
            }
        } else if(guestsList.includes(nameOfThePerson)) {
            guestsList.push(nameOfThePerson + ' is already in the list!');
        } else {
            guestsList.push(nameOfThePerson);
        }
    }
    return guestsList;
}

const inputArr = ['Tom is going!',
'Annie is going!',
'Tom is going!',
'Garry is going!',
'Jerry is going!'];

console.log(houseParty(inputArr));
Amit
  • 1,540
  • 1
  • 15
  • 28
0

Try using Array#prototype#reduce to build a frequency list first, then mapping it to the response you want.

function houseParty(input) {
 const res = Object.entries(input.reduce((acc, curr) => {
  const name = curr.split(' ')[0];
  if (!acc[name]) {
   acc[name] = 1;
  } else {
   acc[name] += 1;
  }

  return acc;
 }, {}))
 .map(x => {
  if (x[1] > 1) {
   return `${x[0]} is already in the list`;
  } else {
   return x[0];
  }
 });
 return res;
}

const result = houseParty(['Allie is going!',
    'George is going!',
    'John is not going!',
    'George is not going!'
]);

console.log(result);
Kunal Mukherjee
  • 5,775
  • 3
  • 25
  • 53
0

I'll give you more 'easy to understand' way.

Note 1:

better to check 'not going' match in string, because of the name may contain 'not' - there are a lot of strange names around the world (For example Knott).

Note 2:

You have to remove a person from the list if his/her name repeated in input with different status.

function houseParty(input) {
  let guestList = [];
  let notComing = [];
  
  let name, going;
  
  //loop through input
  for(let i = 0; i < input.length; i++) {
   
    //get persons name
    name = input[i].split(' ')[0];
    
    //set going status
    going = !input[i].includes('not going'); 
    
    
    if (going) {
      //check name in going list
      if (guestList.indexOf(name) > -1) {
        //this person is already in list
        console.log(`${name} is in the list`);
      }
      else {
        //add person in list
        guestList.push(name);
      }
      
      //check if person was in not going list, remove from it
      if (notComing.indexOf(name) > -1) {
          //remove from not going list
          notComing.splice(notComing.indexOf(name), 1);
      }
    }
    else {
      //check if name is in not going list
      if (notComing.indexOf(name) > -1) {
        console.log(`${name} is in the list`);
      }
      else {
        notComing.push(name); 
      }
      
      //check if person was in going list before
      if (guestList.indexOf(name) > -1) {
          guestList.splice(guestList.indexOf(name), 1);
      }
    }
  }
  
  //you have both lists filled now
    console.log("Guests: ", guestList);
    console.log("Not coming: ", notComing); 
}

let input = [
  'Allie is going!',
  'George is going!',
  'John is not going!',
  'George is going!',
  'George is not going!',
  'Knott is going!' 
];


//test
houseParty(input);
zur4ik
  • 6,072
  • 9
  • 52
  • 78