logic of ----------> Math.random()*(i+1)); ------------> can you please explain why you multiplied *(i+1)
@constantinmunteanu35193 жыл бұрын
Math.random() generates a number between 0 and 0.999 not including 1. Remember that any random generated number * number of last index in array starting with 0 will equal a number that is less than the last index in the array. For example const nums = [10, 20, 30, 40, 50, 60]. Last index is 5. If math.Random() is 0.999 then 0.999 * 5 = 4 after Math.floor() , 4 being the randomIndex. On the first loop, 60 will get swapped with 50 in this case. You could write just ()* i without the + 1, but without the +1, the last value of an array will never have a chance to stay the same, 60 to just stay 60 like it could here (0.999 * (5 + 1)) where a randomIndex will be 5 and last index is also 5. Both * i and (i + 1) are ok in most cases.
@whoami55834 жыл бұрын
Mam i can't understand. Can you taught this in hindi