Skip to content
Advertisement

how to calculate zero values in array between two valid numbers?

I have an array of numbers with some 0 values. I want to fill 0 with the linear difference between two valid values.

let arr = [10,0,0,16,17,0,23] I am using the below code to calculate the linear difference between the two data points if they have zero between them like 10 and 16 have two indexes with 0, if need any further info regarding this data feel free to comment below any answers are accepted that can help.

JavaScript

Advertisement

Answer

Let’s loop through the array, once we find a zero, if it is the first one, remember the position, if it is the last, compute the step and fill:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement