0

I'm trying to write a cisco webex bot which get all people in the space(room) and randomly write only one name. I have this code

framework.hears("daily host", function (bot) {
  console.log("Choosing a daily host");
  responded = true;
  // Use the webex SDK to get the list of users in this space
  bot.webex.memberships.list({roomId: bot.room.id})
    .then((memberships) => {
      for (const member of memberships.items) {
        if (member.personId === bot.person.id) {
          // Skip myself!
          continue;
        }

        let names = (member.personDisplayName) ? member.personDisplayName : member.personEmail;
        let arrays = names.split('\n');
        var array = arrays[Math.floor(Math.random()*items.length)];
        console.log(array)
        bot.say(`Hello ${array}`);

       }
})
    .catch((e) => {
      console.error(`Call to sdk.memberships.get() failed: ${e.messages}`);
      bot.say('Hello everybody!');
    });
});

But this doesn't work. Also name after i use let arrays = names.split('\n'); separated by space and don't have comma. I think because of what code doesn't work Output of console log:

[ 'George Washington' ]

[ 'John' ]

[ 'William Howard Taft' ]

Main question now how to turn output to array?

  • I don't think you've defined `items` ? Is it meant to be `memberships.items.length` ? – JBS Nov 23 '21 at 14:23
  • Hello JBS. Thanks for answer. Now i get `Hello undefined` 3 times after i change items to memberships.items.length – stevemayster Nov 23 '21 at 14:31

2 Answers2

0

That's because arrays[Math.floor(Math.random()*items.length)] only assigns an array with length 3. You need to randomise the index and push to array or use a sort function on the original array

 var array = arrays.sort((a,b)=>{
    return Math.floor(Math.random()*arrays.length);
 });

if you are looking to get the output as per you question you can use reduce instead of sort.

var arrays = [ 'George Washington', 'John',  'William Howard Taft'];
var array = arrays.reduce((a,i)=>{
    if(!a) a = [];
        a.splice(Math.floor(Math.random()*arrays.length), 0, [i]);
    return a;
 },[]);
  • Hello Rajasanthosh. Thanks for answer. I think it works, but output all 3 names, and i need only one. Please help to achive this. – stevemayster Nov 23 '21 at 18:03
  • @stevemaysterj have updated the answer. Is this what you are looking for? – Rajsanthosh Sreenivasan Nov 24 '21 at 03:55
  • Hello, @Rajsanthosh Sreenivasan. Thanks for your answer. But unfortunatelly this not works as expected. I stiil got 3 names and i need only one( – stevemayster Nov 24 '21 at 06:45
  • Just add this: `var name = array[0].toString()` – JBS Nov 24 '21 at 11:11
  • Hello @JBS. Thanks for answer. But still same 3 names on bot says. I thinks its because ```let names = (member.personDisplayName) ? member.personDisplayName : member.personEmail;``` return 3 names as one array. Like this [ 'George Washington' ] [ 'John' ] [ 'William Howard Taft' ] – stevemayster Nov 24 '21 at 14:13
0

Here is how to get a single name from your data, and ensuring it is a string. There are only four names in the array, so run the snippet several times if you keep getting the same name.

// A list of names. Notice that Arraymond is an array; the other names are strings.
const names = [ 'George Washington', 'John',  'William Howard Taft', ['Arraymond'] ];

// Randomize the names
const randomNames = names.sort(() => Math.random() - 0.5);

// Get the first name. Make sure the name is a string (not an array)
const name = randomNames[0].toString();
 
console.log(name)

A tip: don't name your array "array" or "arrays" - it is not meaningful. Use good naming conventions and meaningful variable names that help others understand what the code is doing.

JBS
  • 639
  • 7
  • 17
  • Hello @JBS. I got 3 array then. console.log, as I mentioned before instead of 1 array do 3 array. Like this [ 'George Washington' ] [ 'John' ] [ 'William Howard Taft' ] .Main question is - how I can convert 3 names to 1 string? – stevemayster Nov 26 '21 at 06:56
  • @stevemayster What is the final output you want? [ 'George Washington' ] [ 'John' ] [ 'William Howard Taft' ] or [ 'George Washington' , 'John', 'William Howard Taft' ] or just the first name as string 'George Washington' or the first name in array [ 'George Washington' ]. You must be very clear about your data model and the above output can be transformed to your required data model easily. – Rajsanthosh Sreenivasan Nov 26 '21 at 07:17
  • Hi @stevemayster if you run the code snippet above you'll see one name. This means the names array in your version must be structured differently. Please can you add `console.log({arrays}) ` in your original code, and paste the result here? – JBS Nov 26 '21 at 07:18
  • @stevemayster the answer to converting an array to a string can also be picked up in the code above :) – JBS Nov 26 '21 at 07:20
  • @JBS Output is { arrays: [ 'George Washington' ] } { arrays: [ 'John' ] } { arrays: [ 'William Howard Taft' ] } – stevemayster Nov 26 '21 at 08:09
  • Hello @Rajsanthosh Sreenivasan. I think we have 3 arrays as JBS find out. So i need to combine it to one, and randomize it and get only one random name – stevemayster Nov 26 '21 at 08:13