Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Functions Create Reusable Code with Functions Creating a Function

Im stuck on this. can someone help me with the answer please.

script.js
function sayHi() {
   alert (sayHi) ;
}


alert sayHi();
index.html
<!DOCTYPE HTML>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>JavaScript Basics</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

1 Answer

Hi James, There are few issues with your code:

1) second line -> you are passing variable sayHi as an argument to alert method. You have already used this as a name for your function. You could pass any variable to alert method as long as you have defined this variable and this variable contained a value. In this challenge all you have to do is to pass a string to alert method. In this case string 'Hi'.

2) Now you need to call function - we call functions in javascript by using their name following parentheses - like this: sayHi(); Putting keyword in front of function call with produce error and you don't have to alert the whole function again as you are using alert method in your function already.

Check the code below and see the differences. Hope this helps.

function sayHi() {
  alert('Hi');
}

sayHi();