chatRoom counter

Hello everyone
i have this challenge which is require me to `

If there is no one, return “no one online”.
If there 1 person, return “[user1] online”.
If there are 2 people, return [user 1] and [user 2] online".
If there are n>2 people, return the first two names and add “and n-2 more online”.

For example, if there are 5 users, return:

“[user1], [user2] and 3 more online”
`
And This is my code so far

function chatRoomStatus(n,x) {
    if (n.length == 0 ) {
       return "No one is online"
    } else if (n.length === 1) {
       return `${n} online`
    } else if (n ===  2 ) {
       return `${n} and ${x} online.`
    } 
}
console.log(chatRoomStatus([]))

when i left the array empty it returns No one is online
and also when i add one user it returns john online
but when i pass more that one user to the function
its not return it as i wanted it to be

ali,john and undefined online.

any help will be appreciated :grinning:

If 2 users then you should write the two names and 0 more online, correct?

So you should be figuring out how many people are in the array and subtracting 2 from that to return the other value.

What are the parameters n and x supposed to represent? Can you show us what a few sample calls to the function look like and what you expect them to return?

1 Like

Hey
the n and x are represent the users
the expected result should be
ali and john online.
but what im getting is
ali,john and undefined online.

I think n is the array. You are not passing any x

You did not answer my question. Show an actual call to the function. What if there are 10 users passed to the function, what does the call to the function look like?

hey
if two users its should return [user1] and [user2] online

That is fine. The users are in the n array though.

I think the x value is not being used at all.

Do you know how to use arrays?

1 Like

thats my call to the function console.log(chatRoomStatus(["ali","john"]))

That is one array.

You have defined a function with 2 inputs, but actually the one array you have is more than enough to solve this.

im just getting started with javascript but yeah i know how to work with them

Do you know how to access the values inside the array?

1 Like

as i remember its array[0] with index

Okay so you need to put what you know to use.

You have an array input (drop the other input value)

You know how to check the length
You know how to access the values inside

So what you need is to change your logic to print out the userid values when the conditions of array length are met.

1 Like

thank you sir :pray:
I figured it out Im totally forgot that I should access the exact array index position

1 Like