I'm really struggling to understand this concept :/
- (Eliminating Negative Numbers) Suppose you have an array X containing various values (e.g. [-3,5,1,3,2,10]). Develop a program that removes all negative numbers from the array. By the end of your program, X should solely consist of positive integers. Ensure that you accomplish this task without utilizing a temporary array and only utilize the pop method to eliminate values from the array.
My initial idea was to create a loop through the given array. When X[i] is identified as negative, initiate another loop to swap X[j] and X[j+1] until reaching the end of the array in order to maintain its original order, then use the pop() method.
Upon executing the script, it seems that the loop runs indefinitely. Additionally, if there are two consecutive negative values, the second one might be skipped during the subsequent iteration of i. Is there a more straightforward approach to tackle this problem?
var X = [1,-6,-7,8,9];
//test= [1,-7,8,-6,9]
temp = 0
for (i = 0;i<X.length-1;i++){
if (X[i]<0){
for (j = i;j<=X.length-1;j++){
X[j] = temp
X[j] = X[j+1]
X[j+1] = temp
}
if(X[X.length-1] < 0){X.pop()}
}
};
console.log(X);