vikkktor

Arrays_Advanced-houseParty

Oct 20th, 2021 (edited)
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2. /*Write a function that keeps track of guests that are going to a house party.
  3. You will be given an array of strings. Each string will be one of the following:
  4. -   "{name} is going!"
  5. -   "{name} is not going!"
  6. 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!").
  7. 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!").
  8. At the end print all the guests each on a separate line.*/
  9.  
  10. function myFunct(arrayOfStrings) {
  11.     let arr = arrayOfStrings;
  12.     let guestList = [];
  13.     let not_going = [];
  14.     let going = [];
  15.  
  16.     for (el of arr) {
  17.         if (el.includes('not')) {
  18.             not_going.push(el.split(' ')[0]);
  19.         }
  20.         else {
  21.             going.push(el.split(' ')[0]);
  22.         }
  23.     }
  24.  
  25.     for (i = 0; i < going.length; i++) {
  26.         if (!guestList.includes(going[i])) {
  27.             guestList.push(going[i])
  28.         } else {
  29.             console.log(going[i], "is already in the list!");
  30.         }
  31.     }
  32.     for (i = 0; i < not_going.length; i++) {
  33.         if (guestList.includes(not_going[i])) {
  34.             let removeThisGuestIndex = guestList.indexOf(not_going[i]);
  35.             guestList.splice(removeThisGuestIndex, 1)
  36.         } else {
  37.             console.log(not_going[i], "is not in the list!")
  38.         }
  39.     }
  40.  
  41.     console.log(guestList.join('\n'))
  42. }
Add Comment
Please, Sign In to add comment