Assessment wanted - Functions 3

Hi there :wave:,
Is my solution correct ?

const names = ['Chris', 'Li Kang', 'Anne', 'Francesca', 'Mustafa', 'Tina', 'Bert', 'Jada']
const para = document.createElement('p');

// Add your code here
function random(num) {
  return Math.floor(Math.random() * num);
}

function chooseName(arr) {
  return arr[random(arr.length)]
}

para.textContent = chooseName(names)


// Don't edit the code below here!

section.innerHTML = ' ';

section.appendChild(para);

Can i improve that ?, because I’m not sure :woozy_face:

“making it more flexible” means I can passed the argument with const anotherArray = ['a', 'b', 'c'] when invoke chooseName(anotherArray)and respective with the length. Is like that?

The instructions

Welcome back @rinaldo :wave:

Congratulations, your code is correct. :medal_sports:

Yes, that’s correct. Now, chooseName() can be used with other arrays. The code could even become more flexible by using an upper and a lower bound on random. Something like random(min, max). Of course, this isn’t necessary for this exercise, just to show you what’s possible. :slightly_smiling_face:

See you,
Michael

1 Like