Trent Fowler
Trent Fowler

Reputation: 103

Why is this simple .js for loop stopping after one iteration?

I'm trying to make a simple little .js program that'll randomize an array of English words so that I can translate them into the Russian equivalent:

var vocab_array_1 = ["end/distance", "hour/o'clock", "voice/vote", "city", "water", "table", "child", "force/strength", "father", "woman"];

for (var i = 0; i < vocab_array_1.length + 3; i++){
    var random_index = vocab_array_1[Math.floor(Math.random() * vocab_array_1.length)];

/*random_array.push(random_index);*/
    var random_array = [];
    random_array[i] = random_index;
}

But it's simply returning the result of random_array[i] after a single iteration. You can see that I tried to use the .push() method to build a new array but realized that this method returns the array and thus stops the for loop.

Having removed that, I can't figure out why the for loop stops running after a once through.

NOTE: I'm sure javascript has methods for randomizing arrays; I'm trying to hand code a method for learning purposes.

EDIT:

I performed the recommended changes but couldn't get the random array to log to console. Here is the revised code:

var vocab_array_1 = ["end/distance", "hour/o'clock", "voice/vote", "city", "water", "table", "child", "force/strength", "father", "woman"];
var random_array = [];

for (var i = 0; i < vocab_array_1.length + 3; i++){
    var random_index = vocab_array_1[Math.floor(Math.random() * vocab_array_1.length)];
    random_array.push(random_index);
}

console.log(random_array);

Upvotes: 0

Views: 782

Answers (5)

Bottinator22
Bottinator22

Reputation: 11

Pretty sure you can use array.forEach() instead of a for, if you want to execute code for every element of an array unless there's something you need to do that requires a for.

vocab_array_1.forEach(
    function (element, index, array) {
        var random_index = vocab_array_1[Math.floor(Math.random() * vocab_array_1.length)];
   random_array.push(random_index);
});

Upvotes: 1

Ramesh Pareek
Ramesh Pareek

Reputation: 1669

My Answer is for people who came here searching something similar to "loop stopped after one execution"

If your loop stopped after one execution, chances are that you have nested loops, and are scoped wrongly: See example

functionA = async()=>{
  for (i = 0; i<count; i++) {
    ....
    ....
  }
}


functionB = async () =>{
  for (i=0;i<length; i++){
    result.push(await functionA());
  }
}
functionB();

Now in function B i has a global scope in both functions; If the case resembles yours, scope it properly and it should work.

functionB = async () =>{
      for (var i=0;i<length; i++){    //    < ---------------- Note 'var'
        result.push(await functionA());
      }
    }

Upvotes: 0

Orr Siloni
Orr Siloni

Reputation: 1308

As said in a previous answer move array initialization before the for loop:

 var random_array = [];

But also change

random_array[i] = random_index;

to

random_array.push(random_index);
// or
random_array[random_array.length] = random_index;

To get the shuffle result you're looking for.

Upvotes: 1

user1846747
user1846747

Reputation:

Your code should be like this

var vocab_array_1 = ["end/distance", "hour/o'clock", "voice/vote", "city", "water", "table", "child", "force/strength", "father", "woman"];

var random_array = [];

for (var i = 0; i < vocab_array_1.length + 3; i++){
    var random_index = vocab_array_1[Math.floor(Math.random() * vocab_array_1.length)];
    random_array.push(random_index);    
    //random_array[i] = random_index;
}

Initialize the array outside of your loop.

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386550

Move this line

var random_array = [];

to top, because it gets initialised on every round.

Upvotes: 3

Related Questions