Skip to content

Instantly share code, notes, and snippets.

@vidjuheffex
Created June 6, 2017 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vidjuheffex/656b4cefa9a5f8c40d5b3d0b98836eec to your computer and use it in GitHub Desktop.
Save vidjuheffex/656b4cefa9a5f8c40d5b3d0b98836eec to your computer and use it in GitHub Desktop.
Function Expression Individual Assesment
// 1.
// Define a function max() that takes two numbers as arguments and returns the largest of them. Use the if-then-else construct available in JavaScript.
// A:
function max(num1, num2){
return (num1 > num2) ? num1 : num2;
}
// 2.
// Define a function maxOfThree() that takes three numbers as arguments and returns the largest of them.
// A:
function maxOfThree(num1, num2, num3){
return max(num1, max(num2, num3));
}
// 3.
// Write a function that takes a character (i.e. a string of length 1) and returns true if it is a vowel, false otherwise.
// A:
function isVowel(char){
var vowelArray = ["a", "e", "i", "o", "u"];
return vowelArray.indexOf(char.toLowerCase()) != -1 ? true : false;
}
// 4.
// Write a function called `sum` that takes two parameters and returns the sum of those 2 numbers.
// A:
function sum(num1, num2){
return num1 + num2;
}
// 5.
// Write a function named `avg` that takes 3 parameters and returns the average of those 3 numbers.
// A:
function average(num1, num2, num3){
return (num1 + num2 + num3)/3;
}
// 6.
// Write a function called `getLength` that takes one parameter (a string) and returns the length
// A:
function getLength(string){
return string.length;
}
// 7.
// Write a function called `greaterThan` that takes two parameters
// and returns `true` if the second parameter is greater than the first.
// Otherwise the function should return `false`.
// A:
function greaterThan (num1, num2){
return num2 > num1 ? true : false;
}
// 8.
// Write a function called `greet` that takes a
// single parameter and returns a string that
// is formated like "Hello, Name!" where *Name*
// is the parameter that was passed in.
// A:
function greet(name){
return `Hello, ${name}!`;
}
// 9.
// Write a function called `madlib` that takes 4 or more parameters (words).
// The function should insert the words into a pre-defined sentence.
// Finally the function should return that sentence.
// Note: When I say words and sentence I mean strings. For example:
// words: "quick", "fox", "fence"
// sentence: "quick brown fox jumps over the fence"
// A:
function madlib(adjective, person, verb, adverb, place){
return `My ${adjective} friend, ${person}, ${verb} ${adverb} to the ${place}.`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment