Fisher-Yates shuffle code added

This commit is contained in:
piratefinn 2017-07-04 15:44:13 +01:00
parent 7c6f09e365
commit 36c0ee62a9

View file

@ -15,12 +15,13 @@ export class DashboardComponent implements OnInit {
moneySpentThisMonth: any;
pointsTotal: any;
averageTransactionToday: any;
shuffledArray: any;
constructor(
private http: Http,
private api: ApiService,
) {
this.shuffle = this.shuffledArray;
this.api.graph_data(undefined)
.subscribe(
result => {
@ -51,6 +52,30 @@ export class DashboardComponent implements OnInit {
)
}
// Fisher-Yates shuffle function
public shuffle(array) {
return new Promise(resolve => {
let counter = array.length;
// While there are elements in the array
while (counter > 0) {
// Pick a random index
let index = Math.floor(Math.random() * counter);
// Decrease counter by 1
counter--;
// And swap the last element with it
let temp = array[counter];
array[counter] = array[index];
array[index] = temp;
}
this.shuffledArray = array;
resolve(true);
});
}
public brandPrimary = '#20a8d8';
public brandSuccess = '#4dbd74';
public brandInfo = '#63c2de';