Any help with this javascript?

Hi i have this javascript, that takes a list of numbers input.input1 and another variable, duration and outputs a new list of numbers,
the thing is, that i cant figure out how to implement the output, because, now when i ran the script, even though i dont get any output, is automatically changes the input list with the 'new list.

Any help is appreciated

var arr= inputs.input1;

var begin, a;

var rantebou= inputs.duration;

begin=1;

var j;

a=1;

for (let i = 1; i < arr.length; i++) {

if(arr[i-1]+1==arr[i])

    a++;

else{

    if(a<rantebou)

        j=begin;

    else

        j=i-(rantebou-1);

    for(j;j<i;j++)

        arr[j]=-1;

    begin=i;

    a=1;

}

}

actually i added this
return { result: arr };
in the end, but again, it changes the input list too, do you know what i should change in order to not change the input list?

please show your example data of input.input1 and input.duration

ex.
input.input list of number
[1,2,3,4]

input.duration number
5

OUTPUT:
and what output you really want?

1 Like

Hi,
the input looks like
[1,2,3,4,5,0] or [2,5,8,9,10,0] (random integers the only thing is that always the next is greater than the previous and the last one is 0)

duration
integer

and the output should look like this
ex
[1,2,-1,-1,5,-1,6,-1,0]

and the problem as i said, is that in the end the output, is the same as the input

ex at start
input.input1
[2,5,8,9,10,0]

at end
input.input1
[1,2,-1,-1,5,-1,6,-1,0]
output
[1,2,-1,-1,5,-1,6,-1,0]

from what i understand, this is the problem, but i dont know what to do instead.
image

In the first line, you say that arr is a reference to your input list. It does not copy your list, therefore if you change your list, it also changes the input.

You could try to copy your input list instead:

var arr = [...inputs.input1];
2 Likes

Thats the solution, thank you very much!

1 Like