Ask coding questions

← Back to all posts
JS Assignment 8: Scope and the Return Statement
skudeva (27)

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!

Answered by JustARatherRidi (211) [earned 5 cycles]
View Answer
JustARatherRidi (211)

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 (27)

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

JustARatherRidi (211)

@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 (211)

@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!

DocELB (0)

@JustARatherRidi reading the question explains the answer! Thank you so much this was the only reason I was getting an error. This helped so much!

Shif (2)

I'm new here too, but I struggled through this and happy to share my solution:

The key was to declare a new Var that would include the "Hello..." in this case:
let printName = ("Hello, my name is " + myName);

making sure to include a return statement for that new Var:
return printName;

And finally: calling the function passing in your name as an argument:


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

JwalyaOza (3)

is there anyone who can help me or let me correct myself... can guide me in order to complete the js assignment 8 right from exercise 1 part a and b i really need some guidance to finish that assignment

Shif (2)


Sometimes the more direct answers can stump us!

In this case, we refer to the code already written for us which referenced the global var for school declared up top (outside the function)

let school = 'Lambda';

and the var is referenced inside the function:

function nameMySchool(){
  let className = "Web Development Fundamentals"

on line 7:


The instructions:

// Exercise One: 
// Part One: Create a variable called 'inFunction' and 
//           assign it to the value that will be logged
//           on line 7.

The solution:

let inFunction = "Lambda";
JwalyaOza (3)

@Shif . Thanks a lot ma'am now i got able to understand what exactly they were expecting me to do in this particular assignment. now i'll be able to finish it with ease thanks a lot for your guidance ma'am and if i'd get any kind of quarries in further assignments then i'll surely contact you and i hope you'll also help me... thanks a lot once again ma'am. it was pleasure.

eankeen (1410)

Hi! I reformatted your question to make it a bit easier to understand :)

so when you call the function sayMyName, you have to pass in a parameter, or some value to it.

Kinda like this


You can also pass in a variable


Inside of your function, I'd recommend not using myName as an argument. Meaning, instead of something like this

function sayMyName(myName) {
// ...

I'd do this instead

function sayMyName(locallyScopedName) {
// ...

Then, within your function, you can use locallyScopedName

Hopefully I could help =P

skudeva (27)

@eankeen Thank you very much!😊