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,13 +15,14 @@ export class DashboardComponent implements OnInit {
moneySpentThisMonth: any; moneySpentThisMonth: any;
pointsTotal: any; pointsTotal: any;
averageTransactionToday: any; averageTransactionToday: any;
shuffledArray: any;
constructor( constructor(
private http: Http, private http: Http,
private api: ApiService, private api: ApiService,
) { ) {
this.api.graph_data(undefined) this.shuffle = this.shuffledArray;
this.api.graph_data(undefined)
.subscribe( .subscribe(
result => { result => {
console.log(result); console.log(result);
@ -39,7 +40,7 @@ export class DashboardComponent implements OnInit {
this.barChart1Labels = this.pointsLastWeek.day; this.barChart1Labels = this.pointsLastWeek.day;
} }
), ),
this.api.breadcrumb_data(undefined) this.api.breadcrumb_data(undefined)
.subscribe( .subscribe(
result => { result => {
console.log(result); console.log(result);
@ -50,7 +51,31 @@ 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 brandPrimary = '#20a8d8';
public brandSuccess = '#4dbd74'; public brandSuccess = '#4dbd74';
public brandInfo = '#63c2de'; public brandInfo = '#63c2de';