Functions Flashcards
What is a function ?
A function is a piece of code which we can use over and over in our entire code.
Example of Function ?
function logger() { console.log('My Name is Ram') }
logger()
logger()
logger()
Output My Name is Ram My Name is Ram My Name is Ram My Name is Ram
Can Function Send and Receive Data ?
Yes, Function can Send and Receive Data
Example of Function ( Data Send and Received )
function fruitProcessor(apples, oranges, bananas) { const juice = `juice with ${apples} apples and ${oranges} oranges and ${bananas} bananas.` return juice; } const appleJuice = fruitProcessor(3, 2, 4) console.log(appleJuice)
const bananaJuice = fruitProcessor(2, 1, 5) console.log(bananaJuice);
Coding Challenge ( Function ) LECTURE: Functions 1. Write a function called 'describeCountry' which takes three parameters: 'country', 'population' and 'capitalCity'. Based on this input, the function returns a string with this format:
‘Finland has 6 million people and its capital city is Helsinki’
- Call this function 3 times, with input data for 3 different countries. Store the returned values in 3 different variables, and log them to the console
function describeCountry(country, population, capitalCity) {
return ${country} has ${population} million people and
its capital city is ${capitalCity}
;
}
const descPortugal = describeCountry(‘Portugal’, 10,
‘Lisbon’);
const descGermany = describeCountry(‘Germany’, 83,
‘Berlin’);
const descFinland = describeCountry(‘Finland’, 6,
‘Helsinki’);
console.log(descPortugal, descGermany, descFinland);
Coding Challenge ( Function ) The Puppy Age Calculator
You know how old your dog is in human years, but what about dog years? Calculate it!
Write a function named calculateDogAge that: takes 1 argument: your puppy’s age.
calculates your dog’s age based on the conversion rate of 1 human year to 7 dog years.
outputs the result to the screen like so: “Your doggie is NN years old in dog years!”
Call the function three times with different sets of values.
Bonus: Add an additional argument to the function that takes the conversion rate of human to dog years.
function calculateDogAge(age) { var dogYears = 7*age; console.log("Your doggie is " + dogYears + " years old in dog years!"); }
calculateDogAge(1);
calculateDogAge(0.5);
calculateDogAge(12);
Coding Challenge ( Function ) The Lifetime Supply Calculator
Ever wonder how much a “lifetime supply” of your favorite snack is? Wonder no more!
Write a function named calculateSupply that:
takes 2 arguments: age, amount per day.
calculates the amount consumed for rest of the life (based on a constant max age).
outputs the result to the screen like so: “You will need NN to last you until the ripe old age of X”
Call that function three times, passing in different values each time.
Bonus: Accept floating point values for amount per day, and round the result to a round number.
function calculateSupply(age, numPerDay) { var maxAge = 100; var totalNeeded = (numPerDay * 365) * (maxAge - age); var message = 'You will need ' + totalNeeded + ' cups of tea to last you until the ripe old age of ' + maxAge; console.log(message); }
calculateSupply(28, 36);
calculateSupply(28, 2.5);
calculateSupply(28, 400);