Hi
I tried Test your skills: Functions 1 - 4, and I’d appreciate if I could have your feedback and help!
I think I did okay-ish for 1, 2 and 4, but I got completely stuck with 3.
My work Functions 1
Functions 2
Functions 3
It kind of looks working, but the test requirement was to use the array as a parameter of chooseName() function (if I understood correctly), and I couldn’t figure out how to do that. Could you please give me a hint?
Task 2: Correct. You could also replace the last three line in the function with ctx.fillRect(x, y, width, height);
Task 3: The idea is to not use the names variable directly inside the two functions and to not set min and max inside, but give them as parameters. Here is kind of a scaffolding to help you find the correct solution:
function random(min, max) {
/* Your calculation is correct,
just don't set "min" and "max",
they'll come from outside */
}
function chooseItem(array) {
/* This function should return a random value from the "array" parameter */
return array[ /* Call the random function with the right parameters for "min" and "max" */ ];
}
para.textContent = chooseItem(names); /* This is the only place we use "names" */
I hope that gets you further. Just ask, if I messed up and now it feels more complicated than before.
Task 4: Correct. I see you have learned about another array function I mentioned earlier.
Ahhh yes! That’s perfect.
Now you can see the power of functions: Both functions are independent from the surrounding code and could be used with other arrays.