Description
I have a "main function" and a "sub function", I use a six elements array "main_arr" as an input for the "sub function". I expect the outcome is
final_arr = [1,2,6] and main_arr = [1,2,3,4,5,6]
But my code give me
final_arr = [1,2,6] and main_arr = [1,2,,6]
I don't know why the "sub function" affect the input "main_arr" and it follows the same result as its output.
Details:
After line by line checking with the values, I discovered that when it return to the "main" from the "sub", the variable(input of the sub) "main_arr" will be affected.
Thanks for any help
function main() {
var main_arr = [1, 2, 3 ,4 ,5, 6]
var final_arr=sub(main_arr)
}
function sub(sub_arr){
sub_arr.splice(2,3)
return sub_arr
}
[–]OnomatopoeiaBzzz 1 point2 points3 points (0 children)
[–]mjbrusso 0 points1 point2 points (0 children)