Ask coding questions

← Back to all posts
been working on this one for a while now i need help would be greatly appreciated
washingtonwill (0)

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'.

Answered by raghavm (73) [earned 5 cycles]
View Answer
Commentshotnewtop
raghavm (73)
// The sayMyName function
function sayMyName(myName){
   return "Hello, my name is " + myName;
}

// Calling the function:
let greeting = sayMyName("William");

// Printing it to the console:
console.log(greeting);

If this worked for you, please mark it as the answer. If you need more info, comment and I'd be more than happy to help. :)

d_d (1)

You're on the right track, but you're defining myName twice by including it as a parameter to sayMyName() and then explicitly declaring it with let myName = 'William';. You can remove the let declaration and pass in the value that you'd like for myName when you invoke the function: sayMyName('William')

washingtonwill (0)

thank you very much for responding. still trying to get javascript to say william... it only says hello my name is...

function sayMyName(William){
return 'Hello, my name is';
}
sayMyName();

@d_d

d_d (1)

There are two things going on in your code sample: function declaration (where you use the function keyword, give your function a name, define its parameters, and define what it does), and function invocation (where you invoke your function and pass in the name that you want it to respond with).

In your function declaration, you should use myName as a parameter name, since that's what your exercise asks for. The function return value should include myName, since otherwise your function doesn't know that you want it to use the name: return 'Hello, my name is' + myName;
If you're confused by this line, look up string concatenation.

After you've declared your function, you can use it by referring to its name, sayMyName(). If you don't put anything in the parentheses, you're not passing anything to the myName parameter. If you put a string in the parentheses -'William', for example - that string will take the place of myName inside of your function declaration.

@washingtonwill

washingtonwill (0)

This is what i got and its giving error...

function sayMyName(myName){
let myName = 'William';
return 'Hello my name is', myName;
}
sayMyName();