[[TOC]]
Arrow Functions
Arrow functions are preferred because they are easier to read and write Normal functions have not been deprecated.
Default Parameters
What is returned if nothing is sent to the function?
What is the difference between an argument and a parameter?
- Argument - the item that is passed into the function
- Paramter - the item that accepts being passed into a function
function addOne(number) {
return number + 1;
}
addOne(2); // 3
addOne(5); // 6
addOne(); // what is returned? NaN is returned.
// fix
function addOne(number = 0) {
return number + 1;
}
addOne(2); // 3
addOne(5); // 6
addOne(); // 1 Well? You passed in nothing and we added 1 to nothing. idk what you want me to do here.
// another example
function welcomeUser(name = "random user") {
return `Hello ${name}`;
}
welcomeUser("Solomon"); // "Hello Solomon"
welcomeUser(); // "Hello random user"
Fix sum function
function sum(a = 0, b = 0) {
return a + b;
}
// Sample usage - do not modify
console.log(sum(2, 4)); // 6
console.log(sum(3, 6)); // 9
console.log(sum(3)); // 3
console.log(sum(5)); // 5
console.log(sum()); // 0
Matching the type with what is wanted
function logUserIds(userIds = []) {
userIds.forEach(function(userId) {
console.log(userId);
});
}
// Sample usage - do not modify
logUserIds([10, 15, 14]); // should log every userId to the console
logUserIds(); // should NOT fail
Intro to Arrow functions
const sum = (a, b) => {
return a + b;
}
// can be written as
const sum = function(a, b) {
return a + b;
}
// which can also be written as
const sum = (a, b) => {
return a + b;
}
Examples
function triple(value) {
return value * 3;
}
//can be rewritten as
const triple = (int) => {return int * 3}
//Sum function
const sum = (v1, v2) => {
return v1+v2;
}
console.log(sum(1,5))
More arrow functions
Foreach
/**
* @param {number[]} grades
*/
const sumGrades = (grades) => {
let sum = 0;
grades.forEach(grade => {
sum = sum + grade;
});
return sum;
}
// Sample usage - do not modify
console.log(sumGrades([15, 5, 10])); // 30
console.log(sumGrades([12, 10, 13, 19])); // 54
//or
/**
* @param {string[]} countries
*/
export const getDropdown = (countries) => {
let htmlstring = `<option value="">Please select</option>"`
countries.forEach(country => {
htmlstring +=`<option value="${country.toLowerCase()}">${country}</option>`
});
console.log(htmlstring)
return htmlstring;
}
Filter
This one confused me a bit...
Using an arrow function, complete the function getPositiveTemperatures such that it returns an array containing the positive temperatures (the temperatures that are above 0).
/**
* @param {number[]} temperatures
*/
const getPositiveTemperatures = (temperatures) => {
return temperatures.filter((temperature) => {
return temperature > 0;
});
}
// Sample usage - do not modify
console.log(getPositiveTemperatures([-5, 12, 3])); // [12, 3]
console.log(getPositiveTemperatures([1, -3, -2, 4, 10])); // [1, 4, 10]
Chapter Recap:
- A parameter is a variable in a function definition. When a function is called, the arguments are the data you pass into the method's parameters.
- When you call a function without providing a value for an expected argument, the latter will default to undefined.
- Default parameters allow you to give a default value for one or more parameters that have not been provided when the function is called.
- Arrow functions are shorter to write.
- Arrow functions always start with the parameters, followed by the arrow => and then the function body.
- When an arrow function has one parameter without a default value, you are allowed to drop the parentheses around that parameter.