Hi @Laura! Your code is certainly serving the purpose described in Functions 1, but the idea is that you create a function that will select a random name form the array and then print it to the paragraph.
The difference in Functions 3 is that there we are expecting you to refactor the function from Functions 1 into :
A generic random() function that takes as arguments two bounds, and returns a number between those two bounds.
A chooseName() function that takes as an argument the array to choose from, uses the random() function to choose the name, and returns it.
We then want you to print that random name to the paragraph.
Hi @chrisdavidmills, I have done the modification as below. Could you help to check if my code is correct for Function 1 test and Function 3 test? Thank you.
For Function 1:
function chooseName(){
return names[Math.floor(Math.random()*names.length)]
};
para.textContent=chooseName();
For Function 3:
function random(min, max){
min=Math.ceil(min);
max=Math.floor(max);
return Math.floor(Math.random()*(max-min+1))+min;
};
function chooseName() {
return names[random (0, 7)]
};
para.textContent=chooseName()
The second one works fine, although Iād update the chooseName function to the following, so that it will work with any array of names, not just this particular one:
Also, you should explicitly declare the min and max variables inside the random() function, and call them something different to the parameters, to avoid confusion and possible weird behavior:
function random(min, max){
let minRounded = Math.ceil(min);
let maxRounded = Math.floor(max);
return Math.floor(Math.random()*(maxRounded-minRounded+1))+minRounded;
};