JS Assignment 8: Scope and the Return Statement
skudeva (26)

Hi everyone,

I just started learning JS so I am still pretty new at this. I've been struggling with the JS Assignment 8: Scope and the Return Statement (pre-course work) exercise #3. Please if anyone knows the answer explain how do I get it, that'd be highly appreciated!

// Exercise Three:
// Create a function called 'sayMyName'. It will take one parameter. Call this
// parameter 'myName'. Return the phrase "Hello, my name is " and the myName parameter.
// eg: if name is 'Dan' it should return the string: 'Hello, my name is Dan'.

let myName ="Dan";
function sayMyName(myName){
return "Hello, my name is"; 



Thank you!

You are viewing a single comment. View All
JustARatherRidi (203)

Think of a function as a machine, with a conveyor with supplies going into it, and a finished product coming out of it.

All this question is asking you to do is to make such a machine, or 'define a function', in Javascript terms.

So let's make our machine. The first step is to decide its name, which is sayMyName in this case, and begin our definition like so:

function sayMyName

Once you've named your machine, you need to decide what supplies it's going to take in. Supplies in Javascript are called parameters. Think of parameters as boxes, each with a name. So in our case, we'll have our machine take only one box of supplies, called name going into it.

function sayMyName(name)

Next up, we'll worry about what to actually do with our supplies. In Javascript, everything that happens in a function needs to be enclosed within curly braces ({ and }), which come right after the name and parameters, like so

function sayMyName(name) {


We know we are going to get one box called name, and we can assume there will be someone's name inside the box. The question asks us to take what is inside this box, and use it to make a greeting that looks like Hello, my name is <name>.

Let's go ahead and make a variable with this greeting inside the function.

function sayMyName(name) {
    let greeting = "Hello, my name is " + name;

Notice how I added the name parameter to the end of the string. What this does is take whatever is inside our name box, and attach it to the end of "Hello, my name is ".

Finally, let's give out, or return our finished product

function sayMyName(name) {
    let greeting = "Hello, my name is " + name;
    return greeting;

And we're done!

We now have a fully working machine, but how do we use it? You use or 'call' a function like so:


What we're doing here is putting in a value of 'Dan' into the box called name of our sayMyName function. This value we assign our parameter to is called an argument. This acts as the supplies for our function.

If we run this, what we get as the finished product is "Hello, my name is Dan".

By the way your link doesn't really work, you'd have to copy your code into a Node.js repl and put a link to it here if you want others to see it.

I hope this helped, let me know if I lost you anywhere :)

skudeva (26)

@JustARatherRidi Thank you so much! You definitely made it way easier for me to understand it.🙂

JustARatherRidi (203)

@skudeva Anytime! If you have a preferred answer, you should go ahead mark it as the correct answer :P

Ejiroghene (0)

@JustARatherRidi hi! I was having issues with this assignment as well. After following your solution, I'm getting this as my finished product, 'Hello, my name isDan'
The words is and Dan are appearing as 1 word. I cannot figure out what the problem is. I will appreciate your help please. Thanks!

JustARatherRidi (203)

@Ejiroghene You're so close! If you look at my solution carefully, you'll see that there's a space between the is and the ". Add that space in there, and your string should look like this

"Hello, my name is "

Once you do that, you should be good!

cyncerelycyra (0)

@skudeva @JustARatherRidi Thank you both so much for posting this AND @Ridi for the answer and the analogy. I have been looking for the right explanation to make it understandable for me. And you just did! Thank you sooooo very much! I will be forever grateful to you!