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 Basics (Retired) Creating Reusable Code with Functions Creating a Function

Hi

Inside the sayHi() function add the code to make an alert appear with the word "Hi" in it. The alert command should go inside the function's code block.

script.js
function sayHi() {
 alert ("Hi");
} 
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>
geoffrey
geoffrey
28,736 Points

Hey Abe Layee , good answer, but try to move it as answer. This way that can be marked as best answer ;-).

5 Answers

Abe Layee
Abe Layee
8,378 Points

You have to call the function in order for it to work

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

  sayHi();//this is calling the function.

Hi Abe Layee, not sure if you were able to change this to an answer, so I've done that for you (as a mod).

Abe Layee
Abe Layee
8,378 Points

ok. I am still getting use to this forum . If you don't mind me asking, how do I submit it an answer?

Abe Layee
Abe Layee
8,378 Points

lol never mind, The answer is at the bottom of the page. I got it.

Create a variable for the 'Hi'. It will run.

function sayHi() { var message = "Hi"; alert(message); } sayHi();

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

//worked for me