M
M
Maxim Boyko2021-06-16 22:08:10
JavaScript
Maxim Boyko, 2021-06-16 22:08:10

How to remove the computational error in an array?

Array.prototype.plusNext = function() {
            return this.map(function(el, i, arr) {
                return (
                    el + arr[i + 1]
                )
            })
        }

There is such a method, it adds the next element to each element of the array, and since the last element has nothing to take - it gives a computational error, what can be done with this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Page-Audit.ru, 2021-06-16
@Avenant

You can try something like this (not a zhabist, so I can write with an error)
replace el + arr[i + 1] with
el + ( typeof arr[i + 1] != "undefined" ? arr[i + 1] : 0 )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question