// forEach() = method used to iterate over the elements // of an array and apply a specified function (callback) // to each element // array.forEach(callback) // element, index, array are provided // -------------- EXAMPLE 1 -------------- const numbers = [1, 2, 3, 4, 5]; numbers.forEach(cube); numbers.forEach(display); function double(element, index, array){ array[index] = element * 2; } function triple(element, index, array){ array[index] = element * 3; } function square(element, index, array){ array[index] = Math.pow(element, 2); } function cube(element, index, array){ array[index] = Math.pow(element, 3); } function display(element){ console.log(element); } // -------------- EXAMPLE 2 -------------- let fruits = ["apple", "orange", "banana", "coconut"]; fruits.forEach(capitalize); fruits.forEach(display); function upperCase(element, index, array){ array[index] = element.toUpperCase(); } function lowercase(element, index, array){ array[index] = element.toLowerCase(); } function capitalize(element, index, array){ array[index] = element.charAt(0).toUpperCase() + element.slice(1); } /* function display(element){ console.log(element); } */
ok i got confused for about an hour here about why we use index array so i will leave this comment here if anyone have the same problem. foa we can write the above code and get the same result as following >>> function double (element) { element = element*2; console.log(element) } >>> the main difference here is this method will not change the content of the original array so i will use it if i want to keep the original array untouched. second small note that caused me so much headache... the order of the arguments matter. >>> //the right way to write it function whatever (element , index , array); //wrong way to write it function whatever (index , array , element) >>> hope this helps anyone or i am just super dumb idk xd.
bro, i don't get what you mean when you say the element, index and array are already provided for? please help me understand what you mean by it. and thanks for making such cool tutorials
Array is an object, So it has properties and methods. So when we create an array variable it will have the properties and methods. Like it automatically counts the number of elements and assign it to the element property.
@@wintalyrics1226 hey yoo i like your explanation . i think your from ethiopia, i'm either so we can help each other out in this coding journey so if you down leave me your email or phone number.
@@wintalyrics1226 hey yoo i like your explanation. i think your from ethiopia, i'm either so we can help each other out in this coding journey if you down leave me your email or your phone number.
When I write .forEach(double) after the calling function ". forEach(display)" it shows origanl array of 1,2,3,4. Can you explain why sequence matters here. Thanks.
// forEach() = method used to iterate over the elements
// of an array and apply a specified function (callback)
// to each element
// array.forEach(callback)
// element, index, array are provided
// -------------- EXAMPLE 1 --------------
const numbers = [1, 2, 3, 4, 5];
numbers.forEach(cube);
numbers.forEach(display);
function double(element, index, array){
array[index] = element * 2;
}
function triple(element, index, array){
array[index] = element * 3;
}
function square(element, index, array){
array[index] = Math.pow(element, 2);
}
function cube(element, index, array){
array[index] = Math.pow(element, 3);
}
function display(element){
console.log(element);
}
// -------------- EXAMPLE 2 --------------
let fruits = ["apple", "orange", "banana", "coconut"];
fruits.forEach(capitalize);
fruits.forEach(display);
function upperCase(element, index, array){
array[index] = element.toUpperCase();
}
function lowercase(element, index, array){
array[index] = element.toLowerCase();
}
function capitalize(element, index, array){
array[index] = element.charAt(0).toUpperCase() + element.slice(1);
}
/*
function display(element){
console.log(element);
}
*/
so these videos are made 1 month back and set on auto upload at specific time . nice of 🫵
index and array are built in functions to provide the index values and array collection of fruits and numbers..?
array[index] can also write “return” ??
This is my seal. I have watched the entire video, understood it, and I can explain it in my own words, thus I have gained knowledge. This is my seal.
your seal is cute
Hey it is just incredible how you can just explain these concepts in just 8 minutes! thanks so much for makeing these videos ! :)
amazing explanation ! understood!
forEach also has thisArg option.
very concise, thanks bro
ok i got confused for about an hour here about why we use index array so i will leave this comment here if anyone have the same problem.
foa we can write the above code and get the same result as following
>>>
function double (element) {
element = element*2;
console.log(element)
}
>>>
the main difference here is this method will not change the content of the original array so i will use it if i want to keep the original array untouched.
second
small note that caused me so much headache... the order of the arguments matter.
>>>
//the right way to write it
function whatever (element , index , array);
//wrong way to write it
function whatever (index , array , element)
>>>
hope this helps anyone or i am just super dumb idk xd.
you can actually just make function inside if you're not aware of that, but this helps to use what you learned on callback ✈✈✈✈
Great video 👍 thank you 😊
bro, i don't get what you mean when you say the element, index and array are already provided for? please help me understand what you mean by it. and thanks for making such cool tutorials
i suggest u do it on paper.
Array is an object, So it has properties and methods. So when we create an array variable it will have the properties and methods.
Like it automatically counts the number of elements and assign it to the element property.
@@wintalyrics1226 hey yoo i like your explanation .
i think your from ethiopia, i'm either so we can help each other out in this coding journey so if you down leave me your email or phone number.
@@wintalyrics1226 hey yoo i like your explanation.
i think your from ethiopia, i'm either so we can help each other out in this coding journey if you down leave me your email or your phone number.
Tenku tenku ❤❤turkey izmir
thank!! love that!
When I write .forEach(double) after the calling function ". forEach(display)" it shows origanl array of 1,2,3,4. Can you explain why sequence matters here. Thanks.
make sure that you declared the function separately before implementing it in the function call
Define double first
Love you bro ❤❤
thanks bro :)
❤❤❤
Supereb