Assessment wanted for Functions 3 activity

Hey everyone,

I want to be sure that I’m understanding the instructions correctly. This code seems to work, but I’d like some feedback to make sure that I’m not doing anything I’m not supposed to be doing.

Code:

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

// My code:
function random(low, high) {
  return Math.floor(Math.random()*high);
}

function chooseName(names) {
  let newVar = random(names[0], names.length);
  para.textContent = names[newVar];
}

chooseName(names);
// End of my code

section.innerHTML = ' ';

section.appendChild(para);

Original instructions

Welcome back, @RiscloverYT!

Your are currently ignoring the lower bound of the random(low, high) function. This doesn’t matter in this specific case since we always want the lower bound to be 0.
The task asks to write the random(low, high) more generic. So it should be possible to call random(3, 5) to get an integer between 3 (included) and 5 (excluded).

As a side note: Currently your are assigning the string ‘Chris’ to the low parameter and not a number. :slightly_smiling_face:

I hope that helps and happy coding!
Michael

Hello @RiscloverYT and @mikoMK

i will extend what @mikoMK said a little bit that chooseName should return the element from the array then use that return value to set the para.textContent value

so get the last line of the chooseName method out of it and add a return statement instead

and have a nice day both of you :slight_smile:

1 Like

Hello again @mikoMK, and hello @justsomeone!

I think I understand what you both are saying. Maybe. Sort of. You tell me.

I believe that, according to you miko (Michael? Mikey Mike?), I need to change low to 0. Does that mean I need to actually change low to 0, or does that mean I can keep low but change the parameter to 0 when I call the function? I think it’s confusing me because I was under the impression that “generic” meant “in general terms”, so like, doing what I did in my original code instead of using numbers. So is that incorrect?

However, your comment about not including ‘Chris’ helped it make sense I think… Since I was using names[0], I wasn’t starting at 0 but at Chris?

Okay, I’m still doubting whether I’ve fixed it properly or not. However, the code works (I made sure that all names can show up), so I’m crossing my fingers.

And I appreciate the further input @justsomeone. I was brainfarting on that part of the instructions, but your comment actually helped me see clearly.

Here’s my revision. I’m still confused about the word “generic”, so feel free to correct me further.

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

// My code
function random(low, high) {
  return Math.floor(Math.random()*high);
}

function chooseName(names) {
  let newVar = random(0, names.length);
  return names[newVar];
}

para.textContent = chooseName(names);
// End of my code

section.innerHTML = ' ';

section.appendChild(para);

(please oh please let me be closer and not further :smile: I’m having trouble with JS because it’s my first real programming language, but I feel like I’m slooowly starting to understand it.)

you very close :joy: and do not worry everyone get confused at beginning but with commitment and practice you will get there

about my comment you fixed it correctly you can celebrate :partying_face:

will try to give you a little hint for what generic mean

check your random do you see in this method body did you used the low value you only used the high one
so in another way what if we want random value between 3 and 9 your method would not work cause it currently get random number starting from 0 to high (i am talking about the method it self not how you call it)

to dive into it
Math.random() return a random value between 0 and 1 (1 is exclusive) so it from 0 to just a little bit less than 1 check this one Math.random() - JavaScript | MDN
so math.random()*high return value from 0 to little bit lower than high

hope that help you till @mikoMK be back

and have a nice day both of you :slight_smile:

Mikey Mike is back :grin:

What @justsomeone wrote would have been pretty much my response, too. Since this work has already been done (Thanks, friend :slightly_smiling_face:), I will expand on the term “generic”.

First, apologies for throwing in this word without explaining it. In this context it means “flexible”, “for a general purpose” or “usable in different situations”.
When we step back from this specific task and look at why we use functions at all, two things come to my mind:

  • Encapsulation: We give a function an input, it does something with this input and gives us its output back. When we just use a function like random(0, names.length) we don’t always have to think about how the function works in detail. We only need to know that we will get back an integer between the bounds.
  • Reusability: We can write code once, put it into a function and call this function wherever and how many times we like. When we need to create ten random numbers we just want to call our function ten times and not write out its code every time.

To achieve the second goal of reusability we need our functions to be flexible (generic). In our task we could write this function:

function random(){
  return Math.floor(Math.random() * 8);
}

This would also work because we will get an integer between 0 and 7. But what if we want to use our function for an array that has 14 names? We need to write another random() function that calculates an integer between 0 an 13. Instead of doing that we improve our old function to make the upper bound flexible. (That’s mainly the code you wrote since the lower bound isn’t used):

function random(upper){
  return Math.floor(Math.random() * upper);
}

We can use our function for every array length with random(names.length). Much better! Now imagine we don’t always want to start from the beginning of the array (index 0). We want to call random(5, names.length) to get 5, 6 or 7. That’s not possible with our current function. We again need to make it more flexible. This is the solution code for the random() function of this task. It’s pretty tricky code, but as an exercise try to understand what’s going on inside the function.

function random(min,max) {
  const num = Math.floor(Math.random() * (max - min)) + min;
  return num;
}

I hope this gave you a deeper understanding about the general idea of functions. When learning a programming language for the first time it’s hard to understand and apply the concepts. I promise you when you keep learning, things will start making more and more sense. Just don’t get discouraged if you don’t understand something at the first glance. It happens to all of us. :slightly_smiling_face:

All the best!
Michael

1 Like