随机侮辱生成器(随机化结果)
Random Insult Generator (Randomizing Results)
我写了一个随机侮辱生成器。它很好,并且很好地满足了我的需要。问题是,当我多次运行该程序时,除非我再次复制所有变量,否则每次的侮辱都是一样的。这是我的代码:
var bodyPart = ["face", "foot", "nose", "hand", "head"];
var adjective = ["hairy and", "extremely", "insultingly", "astonishingly"];
var adjectiveTwo = ["stupid", "gigantic", "fat", "horrid", "scary"];
var animal = ["baboon", "sasquatch", "sloth", "naked cat", "warthog"];
var bodyPart = bodyPart[Math.floor(Math.random() * 5)];
var adjective = adjective[Math.floor(Math.random() * 4)];
var adjectiveTwo = adjectiveTwo[Math.floor(Math.random() * 5)];
var animal = animal[Math.floor(Math.random() * 5)];
var randomInsult = "Your" + " " + bodyPart + " " + "is more" + " " + adjective + " " + adjectiveTwo + " " + "than a" + " " + animal + "'s" + " " + bodyPart + ".";
randomInsult;
"Your nose is more insultingly stupid than a warthog's nose."
randomInsult;
"Your nose is more insultingly stupid than a warthog's nose."
我想做的是,当我再次运行randomInsult;
时,我想要一个不同的结果。
使用函数:
function generateRandomInsult() {
// ... all of your existing code ...
return randomInsult;
}
generateRandomInsult(); // this is what you repeat each time
根据我上面的评论,您需要使用一个函数。
var bodyPart = ["face", "foot", "nose", "hand", "head"];
var adjective = ["hairy and", "extremely", "insultingly", "astonishingly"];
var adjectiveTwo = ["stupid", "gigantic", "fat", "horrid", "scary"];
var animal = ["baboon", "sasquatch", "sloth", "naked cat", "warthog"];
var randomInsult = (function() {
var bp, a, a2, an;
var bp = bodyPart[Math.floor(Math.random() * 5)];
var a = adjective[Math.floor(Math.random() * 4)];
var a2 = adjectiveTwo[Math.floor(Math.random() * 5)];
var an = animal[Math.floor(Math.random() * 5)];
var insult = "Your" + " " + bp + " " + "is more" + " " + a + " " + a2 + " " + "than a" + " " + an + "'s" + " " + bp + ".";
alert(insult);
});
document.getElementById('test').addEventListener('click', randomInsult, false);
<button id="test">Click me</button>
你必须做这样的事情来选择随机的bodyPart,形容词,形容词Two和动物在每次调用。
function randomInsult() {
var bodyPart = ["face", "foot", "nose", "hand", "head"];
var adjective = ["hairy and", "extremely", "insultingly", "astonishingly"];
var adjectiveTwo = ["stupid", "gigantic", "fat", "horrid", "scary"];
var animal = ["baboon", "sasquatch", "sloth", "naked cat", "warthog"];
var bodyPart = bodyPart[Math.floor(Math.random() * 5)];
var adjective = adjective[Math.floor(Math.random() * 4)];
var adjectiveTwo = adjectiveTwo[Math.floor(Math.random() * 5)];
var animal = animal[Math.floor(Math.random() * 5)];
return "Your" + " " + bodyPart + " " + "is more" + " " + adjective + " " + adjectiveTwo + " " + "than a" + " " + animal + "'s" + " " + bodyPart + ".";
}
在你的代码段中,你只生成一次"randomInsult"字符串。它需要在每次调用时生成,并具有新的随机值。因此,只需将代码嵌入到函数中即可。
这样称呼它:
randomInsult();
相关文章:
- $(document).height()在刷新时随机化值(Safari 5.1.10)
- 将键盘使用与 Qualtrics 中的答案随机化相结合
- 我怎样才能随机化 HTML 元素的颜色属性
- 通过AJAX调用PHP随机化器的输出
- 如何在JS中随机化backgroundPosition而不使用鼠标移动来制作万花筒的动画
- 使用Javascript对RGBA颜色进行光标控制的形状随机化
- 如何在一个随机化的有序列表中随机化多个无序列表
- 如何使用JavaScript随机化有序列表
- 使用JavaScript随机化html标记
- 可以'我的单词随机化器脚本不能正常循环
- 如果语句是假的 JavaScript,则再次随机化数字
- 函数随机化 - 我可以让它错过一个页面并且该页面上只有一个图像吗?可湿性粉剂
- 随机化网页上的图像和颜色对
- 随机侮辱生成器(随机化结果)
- Javascript:通过在while循环内推送/拼接来随机化数组
- 构建一个简单的随机化器,其中包括一个点、一个下划线、字母 A 和字母 B
- 随机化并在页面加载时显示给定数量的 if 元素
- jQuery Rotator是按字母顺序排列的,我怎样才能在我的主页上随机化
- 随机播放函数随机化字母
- 创建真/假游戏,随机化Q顺序