1

Task Instructions
In this activity, you will define a variable within a JavaScript function. You'll learn more about functions later. For now, we'll focus on variables.

To accomplish this task, you need to do the following:

  • Open the Variables-01 folder and add code to the variables01.js file to do the following:
  • Create a variable called myFriend using one of the variable declarations described above.
  • Set the variable you created to contain your friend's name.
  • Inside the greetings function, return the string: "Greetings [your-friend's-name]."

I have no idea what I am missing in this code but it still comes up as wrong.

/*Instructions
 - Greet your friend by printing a message to the console.
*/
function greetings(greetings ,{myFriend}) {
  var myFriend = 'Mike';
  `greetings ${myFriend}`;
  console.log(myFriend);
  

}

//leave this line unchanged to console log the results
console.log('results: ', greetings());

//don't change this line
if (typeof module !== 'undefined') {
  module.exports = greetings;
}
4
  • Put return in front of `greetings? Commented Jul 19, 2021 at 17:11
  • 1
    Return the template literal AFTER logging to the console. Commented Jul 19, 2021 at 17:13
  • The instructions tell you exactly what you need to do: Inside the greetings function, return the string: Commented Jul 19, 2021 at 17:13
  • You could also just make it function greetings(){ as you are not using the arguments. Commented Jul 19, 2021 at 17:15

2 Answers 2

1

You need a return statement:

https://www.w3schools.com/jsref/jsref_return.asp

ex.

/*Instructions
 - Greet your friend by printing a message to the console.
*/
function greetings() {
  var myFriend = 'Mike';
  console.log(myFriend);
  return `greetings ${myFriend}`;
}

//leave this line unchanged to console log the results
console.log('results: ', greetings());

//don't change this line
if (typeof module !== 'undefined') {
  module.exports = greetings;
}
Sign up to request clarification or add additional context in comments.

3 Comments

If it's automarking, make sure it matches exactly: capitalize "greetings" and add a period at the end.
It says 'TypeError: Cannot read property 'myFriend' of undefined'
See my edit. You don't need arguments on the greetings() function. The error occurs because of the {myFriend} you had as an argument for greetings()
0

A few issues with the code:

  • The function greetings does not return a value
  • You use console.log multiple times, but you could log the return value of the function a single time
  • The function arguments greetings(greetings ,{myFriend}) are not used

If you want to write a function without an explicit return statement, you could shorten the code using a single parameter myFriend with an arrow function.

const greetings = myFriend => `greetings ${myFriend}`;
console.log('results: ', greetings("Mike"));

if (typeof module !== 'undefined') {
  module.exports = greetings;
}

const greetings = myFriend => `greetings ${myFriend}`;
console.log('results: ', greetings("Mike"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.