Day 26 - JS advanced functions
by Nino · 30 things on Twos
- Nesting Conditionals 📝
- ---
- Using Conditions and Functions 📝
- ---
- Stopping Functions with Return
- return statements stops the function, even in the middle of the code
- anything after a return statement doesn't execute
- if there are multiple return statements, whichever one runs first exits the function
- return statements at the end of the function and outside of any conditional statement are run only if no conditions have been met
- ---
- Adding Loops to Functions
- loops repeat the instructions inside their braces
- if we have to repeat the same instructions more than once, we can add a loop inside a function
- to change what a loop displays, we use parameters to do things like adding a string right after the counter variable
- pass values as parameters to control how many times a loop gets executed
- summonBeetlejuice("Beetlejuice", 3);
> Beetlejuice
Beetlejuice
Beetlejuice
- start and end values:
- function displayInterval(start, end) {
for (let i = start; i < end; i++) {
console.log(i);
}
}
displayInterval(26,30);
> 26
27
28
29
- we control a loop's starting point by using a parameter to set the counter variable
- ---
- Looping Over Arrays
- if we want to access elements stored in multiple arrays using the same function, we pass the arrays as arguments when calling the function
- const grades = [92, 66, 77, 84];
const grades2 = [50, 60, 70, 80];
function searchGrade(grades, grades2) { }
searchGrade(grades, grades2);
- to retrieve each array element inside a for loop:
- retrieve array elements one by one using array[i] within a for loop: