Parcourir la source

added randomize()

Kr Younger il y a 6 ans
Parent
révision
ebd4d62b85
3 fichiers modifiés avec 29 ajouts et 0 suppressions
  1. 1
    0
      src/pages/home/home.html
  2. 7
    0
      src/pages/home/home.ts
  3. 21
    0
      src/providers/flash-cards/flash-cards.ts

+ 1
- 0
src/pages/home/home.html Voir le fichier

@@ -9,6 +9,7 @@
9 9
 <ion-content>
10 10
     <button ion-button color="secondary" (click)='reset();'>Reset</button>
11 11
     <button ion-button color="secondary" (click)='next();'>Next >></button>
12
+    <button ion-button color="secondary" (click)='randomize();'> Random! </button>
12 13
   <ion-slides #slides *ngIf="cardSupply && cardSupply.length" (ionSlideDidChange)="slideChanged()" (ionSlideNextEnd)="nextSlide()" (ionSlidePrevEnd)="prevSlide()"
13 14
     [initialSlide]="0">
14 15
 		<ion-slide *ngFor="let current of cardSupply">

+ 7
- 0
src/pages/home/home.ts Voir le fichier

@@ -45,4 +45,11 @@ export class HomePage {
45 45
     let idx = this.slides.getActiveIndex() + 1;
46 46
     this.slides.slideTo(idx, 500);
47 47
   }
48
+
49
+  randomize() {
50
+    this.cardSupply = this.prov.randomCards();
51
+
52
+    this.navCtrl.setRoot(this.navCtrl.getActive().component);
53
+
54
+  }
48 55
 }

+ 21
- 0
src/providers/flash-cards/flash-cards.ts Voir le fichier

@@ -40,6 +40,27 @@ export class FlashCardsProvider {
40 40
   allCards() {
41 41
     return this.defaultCards
42 42
   }
43
+  shuffle(array) {
44
+    var currentIndex = array.length, temporaryValue, randomIndex;
45
+  
46
+    // While there remain elements to shuffle...
47
+    while (0 !== currentIndex) {
48
+  
49
+      // Pick a remaining element...
50
+      randomIndex = Math.floor(Math.random() * currentIndex);
51
+      currentIndex -= 1;
52
+  
53
+      // And swap it with the current element.
54
+      temporaryValue = array[currentIndex];
55
+      array[currentIndex] = array[randomIndex];
56
+      array[randomIndex] = temporaryValue;
57
+    }
58
+  
59
+    return array;
60
+  }
61
+  randomCards() {
62
+    return this.shuffle(this.defaultCards)
63
+  }
43 64
 
44 65
   testCards =
45 66
     [