javascript - 我如何从用户那里获取输入报价并将其打乱,然后将每个字母分开放在 JavaScript 的表格中
问题描述
我有以下打字稿。
它应该随机化用户输入的报价
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.这就是我在 html 中输入的内容,我想做的是随机引用并在每个表中放入 2 到 3 个字母
<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>
所以,我不确定为什么这没有给出输出。
解决方案
基于这个答案
你可以在你的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]);
}
}
}
然后
<td>
<tr *ngFor='let letter of randomQuote'>{{letter}}</tr>
</td>
在这里,您绝对需要有一个小于或等于引号长度的字母,还要确保避免使用空引号
推荐阅读
- python - 是否可以将各种状态代码设置为单个烧瓶错误处理函数?
- angular - 运行“ng extract-i18n”命令后的 Angular 12 XLF 文件添加了 node_modules 跨单元字符串
- playwright - Playwright - 针对不同的环境和不同的变量进行测试
- python - 使用 Python 中的列表理解读取 GPX 文件时出现 UTF 错误
- python-3.x - 如何使用海龟制作可选择的游戏菜单选项?
- python - 来自 Transformers 和多类问题的 BertForSequenceClassification 大小不匹配
- javascript - 材质 UI 主题未正确更改。黑暗主题与元素混淆
- bash - 如何在处理命令的输出时保持返回值?
- java - 连接USB时如何打开屏幕(android 10)
- python - 使用 tkinter python 中的列表框项目取消选中复选框并打印列表框的当前项目