am on function return value assessment 3, which says
Refactor the code that generates the random number into a separate function called random() , which takes as parameters two generic bounds that the random number should be between, and returns the result.
is it that they want me to specify a min and max parameters, that is two parameters will be inside the random function
Yes, that’s right.
The function should look something like this:
function random(min,max) {
const num = /* Your calculation goes here */
return num;
}
You have to think about if the bounds (min and max) are included or excluded. Either way is fine, but you have to make sure when calling it that all indexes could be returned. For example: random(2, 5) should return either 2, 3, 4 or 5 (upper bound included) or 2, 3 or 4 (upper bound excluded).
I hope that makes it clearer. Please tell me if you need more help.
i was able to solve it by looking up the syntax for calculating min, max. when i saw that the random function returns one digits when called, i used it output to access the array, can you please assess it
function random(min, max) {
const num = Math.floor(Math.random() * (max - min) + min);
return num;
}
function chooseName(name) {
// stored the value of calling the random func inside rdn
const rdn = random(2, 6);
para.textContent = names[rdn];
}
chooseName();
Your random() function is correct. That’s great!
Few things about chooseName():
random(2, 6) generates either 2, 3, 4 or 5. (max is excluded), but our array has indexes ranging from 0 to 7.
To be more flexible (like more names), we shouldn’t use a fixed number as our max value. It should depend on the length of the array
It’s better to have para.textContent outside of chooseName() for readability
It should look something like this:
function chooseName(names) {
// stored the value of calling the random func inside rdn
const rdn = random(/* min and max values to include all indexes of names */);
return names[rdn];
}
para.textContent = chooseName(names);