Skip to content

Instantly share code, notes, and snippets.

@jfrites
Last active January 3, 2020 00:42
Show Gist options
  • Save jfrites/f9e14477164774adc8bb27a971d082d7 to your computer and use it in GitHub Desktop.
Save jfrites/f9e14477164774adc8bb27a971d082d7 to your computer and use it in GitHub Desktop.
05-Functions
//simple sum
function simpleSum (a, b) {
return a + b;
}
console.log(simpleSum(3,4));
//Default Greet
//`` lets you add the implicit stuff from the parameter with te ${} this is clean
const defaultGreet = (firstName, lastName = "Doe") => {
return `Hi ${firstName} ${lastName}!`;
}
console.log(defaultGreet("Jason", "Friedlander"));
//alt version 1
function defaultGreetAlt1 (firstName, lastName = "Doe") {
return `Hi ${firstName} ${lastName}!`;
}
console.log(defaultGreetAlt1("Ian", "Friedlander"));
//alt version 2
function defaultGreetAlt (firstName, lastName) {
if (lastName === undefined) {
lastName = 'Doe';
}
return firstName + ' ' + lastName;
}
console.log(defaultGreet("Nicole"));
//tax calculator
const taxCalulator = (amount, state) => {
switch (state) {
case "NY":
return amount * 1.04;
case "NJ":
return amount * 1.0625;
default:
return amount;
}
};
console.log(taxCalulator(100, "NJ"));
//Making a word from first letter of each word submitted
const myMnemonic = (firstWord = "", secondWord = "", thirdWord = "", fourthWord = "") => {
let mnemonic ='';
if (firstWord !== undefined) mnemonic += firstWord[0];
if (secondWord !== undefined) mnemonic += secondWord[0];
if (thirdWord !== undefined) mnemonic += thirdWord[0];
if (fourthWord !== undefined) mnemonic += fourthWord[0];
return mnemonic;
}
// for loop version
// Rest Operator - we're turning all the arguements coming into this function into an array.
const myMnemonic = (...words) => {
let mnemonic = '';
for (let i = 0; i < words.length; ++i) {
const currentWord = words[i];
const currentCharacter = currentWord[0];
mnemonic += currentCharacter;
}
return mnemonic;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment