1

I have the following typscript.

it's supposed to randomize the quote that the user input

quoteReady: boolean = false;

randomizQuote(array);

{
  this.quoteReady = true;

  this.quote = array;

  for (var i = array.length - 1; i > 0; i--) {
    var j = Math.floor(Math.random() * (i + 1));
    var temp = array[i];
    array[i] = array[j];
    array[j] = temp;
  }
}

2.And this is what i put in the html and what i want to do is to take the randomized quote and put 2 to 3 lettres of it in each table

<section *ngIf="quoteReady">
  <table class="table table-bordered table-hover table-responsive">
    <thead>
      <td>
        <tr *ngFor="let quo of quote">{{ citation }}</tr> //i want to insert a
        randome 2 to 3 //letters from the quote here
      </td>

      <td>
        <tr *ngFor="let quo of quote">{{ array }}</tr>
      </td>

      <td>
        <tr *ngFor="let quo of quote">{{ array }}</tr>
      </td>

      <td>
        <tr *ngFor="let quo of quote">{{ array }}</tr>
      </td>

      <td>
        <tr *ngFor="let quo of quote">{{ array }}</tr>
      </td>
    </thead>
  </table>
</section>

So, I am not sure why this is not giving the output.

Jay
  • 2,648
  • 4
  • 29
  • 58
Karim
  • 39
  • 6

1 Answers1

0

based on this answer

you could do in your ts

import { Component, VERSION, OnInit} from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
randomQuote = [];
numberOfLetters = 3
quote = "abcdefghijklmonopqrstuwxyz".split('');
quoteIndexes = {}

 ngOnInit(){
  while (this.randomQuote.length < this.numberOfLetters){
    let index = -1;
    do {
      index = Math.floor(Math.random() * this.quote.length);
      this.quoteIndexes[index] = true;
    } while (!(index in this.quoteIndexes))
    this.randomQuote.push(this.quote[index]);
  }
 }
}

then

<td>
  <tr *ngFor='let letter of randomQuote'>{{letter}}</tr>
</td>

here you absolutely need to have a number of letter less or equal to the quote length also make sure to avoid empty quotes

stackBlitz demo

JSmith
  • 4,519
  • 4
  • 29
  • 45