Remove the unnecessary branch in _.shuffle()

The non-zero case works properly for 0 as well.
This commit is contained in:
Jakub Wieczorek
2012-04-03 15:23:38 +02:00
parent 73e6e87858
commit a40001a358
2 changed files with 7 additions and 7 deletions

View File

@@ -67,4 +67,8 @@
return _.range(1000);
});
JSLitmus.test('_.shuffle()', function() {
return _.shuffle(numbers);
});
})();

View File

@@ -250,13 +250,9 @@
_.shuffle = function(obj) {
var shuffled = [], rand;
each(obj, function(value, index, list) {
if (index == 0) {
shuffled[0] = value;
} else {
rand = Math.floor(Math.random() * (index + 1));
shuffled[index] = shuffled[rand];
shuffled[rand] = value;
}
rand = Math.floor(Math.random() * (index + 1));
shuffled[index] = shuffled[rand];
shuffled[rand] = value;
});
return shuffled;
};