Професионална програма
Loading...
+ Нов въпрос
Iskren.1990 avatar Iskren.1990 6 Точки

JS Fundamentals 03. House Party

Здравейте,

   Гърмят ми първият и четвърти тест. Благодаря предварително!

https://pastebin.com/JGPd1mVj

1.   House Party

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.

Examples

Input

Output

['Allie is going!',

'George is going!',

'John is not going!',

'George is not going!']

John is not in the list!

Allie

['Tom is going!',

'Annie is going!',

'Tom is going!',

'Garry is going!',

'Jerry is going!']

Tom is already in the list!

Tom

Annie

Garry

Jerry

Тагове:
0
Programming Fundamentals
willystyle avatar willystyle 2156 Точки
Best Answer

Както е казано в условието ако го имаш в списъка печаташ (а не да вкарваш стринга в листа с unshift):

function houseParty(arr) {
    let guestList = [];
    arr.forEach(token => {
        let a = token.split(` `);
        let name = a[0];
        if (token.includes(`not`)) {
            if (guestList.includes(name)) {
                guestList.splice(guestList.indexOf(name), 1);
            } else {
                console.log(`${name} is not in the list!`);
            }
        } else {
            if (guestList.includes(name)) {
                console.log(`${name} is already in the list!`);
            } else {
                guestList.push(name);
            }
        }
    });
    guestList.forEach(element => {
        console.log(element);
    });
}

 

0
Iskren.1990 avatar Iskren.1990 6 Точки

Еее благодаря колега, мислех, че ако има два стринга не е много редно да се вкарват по обратен ред, но и не бях много сигурен. Мислех да сложа каунтър който да брои стринговете и да сплайсва масива, но не стигнах до там.

1