I can’t seem to understand this loop.
for (i = 1; i < 50; i++) {
rand = Math.ceil(Math.random() * 49);
temp = nums[i];
nums[i] = nums[rand];
nums[rand] = temp;
}
It is part of a larger code. nums[i]
is an array of 1-49 filled with their respective index numbers, then it is run through this for
loop and filled with random numbers.
I don’t understand why the temp variable is created, how the nums[rand]=temp;
line of code works and what does it do, and why is it not even initialized without the var
keyword.
Can someone explain how this loop works?