create pair foreach item in array 1

create pair foreach item in array

arr = [1, 2, 3, 4];

function pairwise(arr, func){
    for(var i=0; i < arr.length - 1; i++){
        func(arr[i], arr[i + 1])
    }
}

pairwise(arr, function(current, next){
    console.log(current, next)
})

Here is what the above code is Doing:
1. We have an array of numbers.
2. We have a function that takes two arguments.
3. We have a function that takes an array and a function as arguments.
4. We loop through the array and pass the current element and the next element to the function.

Similar Posts