Assessment & help wanted for Test your skills: Functions

Hi :wave:
I tried Test your skills: Functions 1 - 4, and I’d appreciate if I could have your feedback and help! :slight_smile:

I think I did okay-ish for 1, 2 and 4, but I got completely stuck with 3. :sob:

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. :sob: Could you please give me a hint?

Functions 4

Test page

Thank you in advance! :blush:

Hi again, @Risa

Here are my comments:

  • Task 1: :white_check_mark: Correct.
  • Task 2: :white_check_mark: 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. :grin:
  • Task 4: :white_check_mark: Correct. I see you have learned about another array function I mentioned earlier. :+1:

See you,
Michael

2 Likes

Hi @mikoMK,

Thank you for your help! :blush:
Considering the hints you gave me, I revised Task3. Could you please see if I did it correctly?

Thank you!
Risa

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.

1 Like

Happy to hear I finally did it correctly!

Thank you so much for giving me hints not just answers, it was really nice to try to find the answer by myself based on the hints! :blush:

2 Likes

Hey @Risa !

I was reviewing your code and noticed your “chooseItem” function has a small mistake.

When refreshing the page, I noticed the name “Jada” was never being shown. I noticed that it was at the end of the array.

In your function you had a max parameter of array.length-1. If you remove the “-1” you’ll get Jada to display again.

The code block should look like this:

function chooseItem(array) {
      return array[random(0, array.length)]       
    }

Hope that makes sense!

3 Likes