Skip to content

Instantly share code, notes, and snippets.

@cesine
Created March 22, 2017 16:39
Show Gist options
  • Save cesine/269c82650b7ecc5c7eb8578fbcc35043 to your computer and use it in GitHub Desktop.
Save cesine/269c82650b7ecc5c7eb8578fbcc35043 to your computer and use it in GitHub Desktop.
Exported from Popcode. Four Functions starter Click to import: https://popcode.org/?gist=269c82650b7ecc5c7eb8578fbcc35043
<!DOCTYPE html>
<html>
<head>
<title>Four Functions</title>
</head>
<body>
<!-- Put your page markup here -->
</body>
</html>
{"enabledLibraries":["jquery"]}
/*
#1
Cubed
Directions: Make a program that returns the cubed value of the parameter. Ex: 2 --> 8
Instructions:
1. Declare a function called cubed.
2. Give the function a parameter named num.
2b. Call the function with 2 as the parameter
3. In the body of the function, make a return statement that multiplies the parameter by itself 3 times.
4. console.log the function call
*/
//To do: Insert you code below this line
function cubed(num) {
return num * 3;
}
console.log(cubed(2));
/*
#2
So Last Year Function
Directions: Make a program that tells you the grade and age you were last year
Instructions:
1. Declare a function called lastYear.
2. Give the function a parameter named year.
2b. Call the function with 2016 as the first parameter
3. In the body of the function, make a return statement that subtracts one from the parameter.
4. Create two console.log statements.
4a. one statement should print the grade you were in last year
4b. one statement should print the age you were in last year
*/
//To do: Insert you code below this line
function lastYear(year) {
return year - 1;
}
console.log('grade ' + lastYear(10));
console.log('age ' + lastYear(14));
/*
#3
Hello Name
Directions: Make a program that says hello to 3 different people.
Instructions:
1. Declare a function called greeting.
2. Give the function a parameter named person.
2b. Call the function with your name as a string.
3. In the body of the function, make a return statement with the string hello added to the parameter.
4. Create three console.log statements with a function call of the names of people from around the room.
*/
//To do: Insert you code below this line
function greeting(person) {
return "hello " + person;
}
console.log(greeting("gina"));
console.log(greeting("peter"));
console.log(greeting("brian"));
console.log(greeting("anthony"));
/*
#4
Directions: Make a function that computes the average of two numbers
Instructions:
1. Create a function called average with two parameters, x and y
2. Return the sum of x and y divded by 2
Test your function with console.log on 2 sets of inputs (numbers).
*/
//To do: Insert you code below this line
function average(x, y) {
return (x + y) / 2;
}
console.log(average(5, 10));
console.log(average(2, 3));
console.log(average(20, 10));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment