-6

The problem is the - operator does not working (in 8th line). See my code below:

array = [0,0,0,0,3,0,0,0,0],
n = 0;

for(var i = 0; i < array.length; i++){
    if(n < 9){ //the "n" variable there's only for don't crash the browser with a infinite loop
        if(array[i] == 3){
            array[i] = 0;
            array[i - 1] = 3; //I believe that here is the problem
        }
    }
n++;
}

console.log(array);

So... I want to move the "3" value to the beginning of the array. But it only work if I use the + operator(in 8th line). Consequently, if I use the + one, the "3" value goes to the end of the array.

Anyone know why the - operator does not working in this case and the + works?

KikeH
  • 1
  • 2
  • 3
    This sort of problem can be debugged easily with paper and pencil. Keep track of the current value of `i`, the contents of the array, etc., and go through the code step by step. – Patricia Shanahan May 29 '14 at 00:18
  • The problem is that for i = 0 , i-1 is not a valid index for the array. – iCantSeeSharp May 29 '14 at 00:22
  • *Please* test your code and challenge your own assumptions before you ever post a question. You're asking other people to expend their valuable time to help you, so make every effort to understand what is going on *before* you ask. – Matt Coubrough May 29 '14 at 01:50

5 Answers5

5

If you change line 8 to:

array[i+1] = 3;

then the number 3 will go all the way to the end of the array (well, beyond the end of the array and I'll be damned to find out what Javascript does then!). This is because the loop traverses the array in increasing order and the position i+1 will be checked right next.

On the other hand, with your current line 8, number 3 goes one position backwards (which has already been checked), so it doesn't go all the way to the beginning of the array, just one position. If you want it to go to all the way in the same fashion, you should reverse the loop (make it traverse the array in descending order of the position i).

nickie
  • 5,608
  • 2
  • 23
  • 37
1

What do you think happens when i is 0 and you do - 1?

AShelly
  • 34,686
  • 15
  • 91
  • 152
0

In your first iteration of the loop, when i is zero, (i - 1) is -1, so you're trying to access array[-1], which is invalid.

Dan Korn
  • 1,274
  • 9
  • 14
  • 1
    It's not invalid, it counts backwards from the end. – AShelly May 29 '14 at 00:03
  • It may count backwards, but that's almost surely not what is intended in this case. – Dan Korn May 29 '14 at 00:05
  • Also, whether it actually counts backwards is non-standard. In my interpreter, [1,2,3,4,5,6][-1] is undefined. – Dan Korn May 29 '14 at 00:08
  • @AShelly—no, it creats a property '-1' and assigns the value. Array's are essentially plain objects with a special length property. The properties are strings, not numbers, – RobG May 29 '14 at 00:08
  • No, i think. You can see that there's this condition: if(array[i] == 3) So I'm acessing the "3" index, in this case would be: 4 4 - 1 = 3 Well... there's a loop, therefor the loop will do: 4 - 1 = 3, after 3 - 1 = 2, and so on. You can see that it happen if you try to do array[i + 1], therefor: 4 + 1 = 5, 5 + 1 = 6 and so on... – KikeH May 29 '14 at 00:10
  • Rob is right, it does create a named property. However, just because you have added a property named "-1" does not mean that anything "counts backwards from the end." – Dan Korn May 29 '14 at 00:11
  • See my other answer, which I think meets the stated requirement, which is to move the number 3 to the beginning of the array. – Dan Korn May 29 '14 at 00:20
0

Okay, instead of answering "Anyone know why the - operator does not working in this case and the + works?", I will answer what I think is the real question, as stated in the original post: I want to move the "3" value to the beginning of the array. I think this does what is desired:

var array = [0,0,0,0,3,0,0,0,0];
var first_val = 3;

var index = array.indexOf(first_val);
if (index > 0) {
    array.splice(index, 1);
    array.unshift(first_val);
}

console.log(array);

Inspired by this.

Community
  • 1
  • 1
Dan Korn
  • 1,274
  • 9
  • 14
0

I want to move the "3" value to the beginning of the array

Use the correct answer provided in here. Then use .indexOf() to move it.

Array.prototype.move = function (old_index, new_index) {
    if (new_index >= this.length) {
        var k = new_index - this.length;
        while ((k--) + 1) {
            this.push(undefined);
        }
    }
    this.splice(new_index, 0, this.splice(old_index, 1)[0]);
    return this; // for testing purposes
};

var array = [0,0,0,0,3,0,0,0,0];

var result = array.move(array.indexOf(3),0);

console.log(result);

JSFiddle Demo

Community
  • 1
  • 1
jhyap
  • 3,779
  • 6
  • 27
  • 47