Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement shuffle-array utility library #1

Open
kgish opened this issue Jun 4, 2016 · 0 comments
Open

Implement shuffle-array utility library #1

kgish opened this issue Jun 4, 2016 · 0 comments

Comments

@kgish
Copy link
Owner

kgish commented Jun 4, 2016

import Ember from 'ember';

function shuffleArray(array) {
    var m = array.get('length'),
        shuffled_n = [], shuffled_array = Ember.A();

    for (var i = 0; i < m; i++) {
        shuffled_n.push(i);
    }
    shuffled_n = shuffle(shuffled_n, m);

    // Use shuffled array of digits to randomize array object elements
    for (i = 0; i < m; i++) {
        shuffled_array.pushObject(array.objectAt(shuffled_n[i]));
    }

    return shuffled_array;
}

// Shuffle array of digits
function shuffle(n, m) {
    var t, i;

    // While there remain elements to shuffle
    while (m) {

        // Pick a remaining element
        i = Math.floor(Math.random() * m--);

        // And swap it with the current element.
        t = n[m];
        n[m] = n[i];
        n[i] = t;
    }
    return n;
}

export {
    shuffleArray
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant