JavaScript forEach() method in 8 minutes! ➿

preview_player
Показать описание
00:00:00 introduction
00:00:18 example 1
00:03:58 example 2
00:07:37 conclusion

// forEach() = method used to iterate over the elements
// of an array and apply a specified function (callback)
// to each element

// element, index, array are provided

// -------------- EXAMPLE 1 --------------

const numbers = [1, 2, 3, 4, 5];

function double(element, index, array){
array[index] = element * 2;
}

function triple(element, index, array){
array[index] = element * 3;
}

function square(element, index, array){
}

function cube(element, index, array){
}

function display(element){
}

// -------------- EXAMPLE 2 --------------

let fruits = ["apple", "orange", "banana", "coconut"];

function upperCase(element, index, array){
}

function lowercase(element, index, array){
}

function capitalize(element, index, array){
}

/*
function display(element){
}
*/
Рекомендации по теме
Комментарии
Автор

// 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.slice(1);
}

/*
function display(element){
console.log(element);
}
*/

BroCodez
Автор

<*_> 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. <_*>

piotrmazgaj
Автор

Hey it is just incredible how you can just explain these concepts in just 8 minutes! thanks so much for makeing these videos ! :)

vctorroferz
Автор

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

UncannyPotato