A FUNCTION is a NAME for a piece of code
function greetByName(someName) {
return 'Hello there! It is nice to meet you ' + someName + '.';
}
This is similar to how a variable is a name for a piece of data
Here are some reasons why this is useful:
Here's an example function:
function add(firstNum, secondNum) {
let sum = firstNum + secondNum;
return sum;
}
function
means, define a functionadd
is the name of the functionfirstNum, secondNum
are the parameters to the function, also called arguments
sum
is a local variable of the functionsum
is also the return value of the functionYou call a function by referencing the name followed by parentheses:
function add(firstNum, secondNum) {
let sum = firstNum + secondNum;
return sum;
}
add(2, 3) // returns 5
add(12, 30) // returns 42
This is also known as executing the function.
A number is divisible by another if when you divide them, the remainder is 0
.
Write a function called divisible
that:
true
if the first number is divisible by the second number, and false
otherwisefunction divisible(firstNum, secondNum) {
// write your code here
}
divisible(100, 10) // true
divisible(100, 7) // false
divisible(3333, 11) // true
divisible(99, 12) // false
If you write the solution in a file, use
console.log(divisible(100, 7))
to print thereturn
value.
if(/*this expression evaluates true*/) {
//do this
} else {
//otherwise do this
}
function divisible(operator, operand) {
if(operator % operand) {
return true
} else {
return false
}
}
divisible(100, 10) // => true
Here is a function that takes a String as input, and it return
s a shouted version of that String.
function shouter(someString) {
let loudString = someString.toUpperCase();
return loudString + '!!!';
}
shouter('i like pizza');
// 'I LIKE PIZZA!!!'
The variable
loudString
is called a local variable and can only be used inside the function.
Write a function called capitalize
that:
function capitalize(someWord) {
// your code here
}
capitalize('tomato')
// 'Tomato'
Remember there are many string operations
let firstLetter = string[0]
let restOfString = string.slice(1)
function capitalize(word) {
let firstLetter = word[0];
let restOfWord = word.slice(1);
return firstLetter.toUpperCase() + restOfWord.toLowerCase();
}
console.log(capitalize('smith'));
console.log(capitalize('MACGUYVER'));
When you pass a variable to a function, that variable value is assigned to a parameter.
let nameToShout = 'Grace Hopper';
shouter(nameToShout);
// 'GRACE HOPPER!!!'
The variable name and parameter name DO NOT need to match
let nameToShout = 'Grace Hopper';
function shouter(someString) {
let loudString = someString.toUpperCase();
return loudString + '!!!';
}
let result = shouter(nameToShout);
Outside | Inside | Value |
---|---|---|
nameToShout |
someString |
'Grace Hopper' |
loudString |
'GRACE HOPPER' |
|
result |
'GRACE HOPPER!!!' |
Write a function named ageInSeconds
that:
return
s the person's age in seconds
let age = 27;
function ageInSeconds(num) {
// your code here
}
ageInSeconds(age);
// 'You are 852055200 seconds old'
Now write a reverse function that:
return
s the age in years, or fractions of a yearlet ageInSeconds = 852055200;
function ageInYears(seconds) {
// your code here
}
ageInYears(ageInSeconds);
// 'You are 27 years old'
Here's one solution for the ageInSeconds calculator:
let age = 27
function ageInSeconds(num) {
let secondsInMin = 60
let minInHour = 60
let hrInDay = 24
let dayInYr = 365.25
let secInYr = secondsInMin * minInHour * hrInDay * dayInYr
let ageInSec = num * secInYr
return ageInSec
}
console.log(ageInSeconds(age))
Write a function named supplyCalc
that:
return
s 'You will need Number Items to last the rest of your life.' e.g.supplyCalc(20, 3, 'cookie')
// 'You will need 87600 cookies to last the rest of your life'
supplyCalc(99, 3, 'cakes')
// 'You will need 1095 cakes to last the rest of your life'
supplyCalc(0, 3, 'pies')
// 'You will need 109500 pies to last the rest of your life'
Inspired by the Lifetime Supply Calculator lab designed for the Girl Develope It! curriculum. The original can be found here
let amountPerYear = amountPerDay * 365
let numberOfYears = 100 - age
function supplyCalc(age, amountPerDay, item) {
let amountPerYear = amountPerDay *365;
let numberOfYears = 100 - age;
let totalNeeded = amountPerYear* numberOfYears;
return 'You will need' + totalNeeded + ' ' + item + 's to last the rest of your life';
}
Write a function named titleize
that:
return
s a string with the first letter of each word capitalized e.g.titleize('all dogs are good dogs');
// 'All Dogs Are Good Dogs'
titleize('eveRY green bus drives fAst');
// 'Every Green Bus Drives Fast'
titleize('FRIDAY IS THE LONGEST DAY');
// 'Friday Is The Longest Day'
function capitalize(word) {
let firstLetter = word[0].toUpperCase();
let restOfWord = word.slice(1).toLowerCase();
return firstLetter + restOfWord;
}
let wordArray = string.split(' ');
function capitalize(word) {
let firstLetter = word[0].toUpperCase();
let restOfWord = word.slice(1).toLowerCase();
return firstLetter + restOfWord;
}
function titleize(string) {
let wordArray = string.split(' ');
let newString = '';
let wordsModified = 0;
while (wordsModified < wordArray.length) {
let currentWord = wordArray[wordsModified];
let newWord = capitalize(currentWord);
newString = newString + ' ' + newWord;
wordsModified = wordsModified + 1;
}
return newString.trim();
}
/