Skip to content

Instantly share code, notes, and snippets.

@annajmorton
Created June 3, 2016 20:53
Show Gist options
  • Save annajmorton/b8df417d64cf982cd349af5bdf2454cd to your computer and use it in GitHub Desktop.
Save annajmorton/b8df417d64cf982cd349af5bdf2454cd to your computer and use it in GitHub Desktop.
Intro to JavaScript Functions

Intro to JavaScript Functions

Please follow the instructions below.

  1. Download functions.js and save it to the js folder within the ~/vagrant-lamp/sites/codeup.dev/public folder on your Mac.

  2. Create an HTML file named functions_js.html in your public directory.

    <!DOCTYPE html>
    
    <html>
    
    	<head>
    	
    		<title>3.4 Functions</title>
    		
    		<meta charset="utf8">
    
    	</head>
    
    
    
    	<body>
    
    		<script src="functions.js"></script>
    	</body>
    
    </html>
  3. Add a <script> tag to your HTML to link up functions.js to your file.

  4. Open functions.js in Sublime and follow the instructions marked TODO:.

    "use strict";
    
    var myNameIs = 'Anna'; // TODO: Fill in your name here.
    
    // TODO:
    // Create a function called 'sayHello' that takes a parameter 'name'.
    // When called, the function should log a message that says hello from the passed in name.
    function sayHello(someName){
    	console.log("hello "+someName+"!");
    }
    
    // TODO: Call the function 'sayHello' passing the variable 'myNameIs' as a parameter.
    sayHello(myNameIs);
    
    
    // Don't modify the following line
    // It generates a random number between 1 and 100 and stores it in random
    var random = Math.floor((Math.random()*100)+1);
    
    // TODO:
    // Create a function called 'isOdd' that takes a number as a parameter.
    // The function should use the ternary operator to log a message.
    // The log should tell the number passed in and whether it is odd or not.
    function isOdd(num){
    	var msg = (num%2==0) ? num+" is even steven!": num+" - it's an odd'n";
    	console.log(msg);
    }
    
    // TODO: Call the function 'isOdd' passing the variable 'random' as a parameter.
    isOdd(random);
  5. Finally, save your work, commit the changes to your git repository, and push to GitHub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment