可能重复:
从JavaScript数组中获取随机值
好的,所以我这里有三个变量,每个变量都是摇滚,纸张或剪刀.使用JavaScript,我如何随机生成其中一个单词?
到目前为止它是这样的:
<!DOCTYPE html>
<html>
<body>
<button type="button" onclick="myFunction()"> Click me</button>
<script>
function myFunction()
{
var c="Rock";
var d="Paper";
var e="Scissors";
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
然后我将有一个名为K的变量,它将是岩石纸或剪刀中的随机单词.所以它会是这样的:
alert("The computer chose: " + k);
Run Code Online (Sandbox Code Playgroud)
那么如何让JavaScript在三个变量之间随机选择c,d和e?
我正在制作一个可以回复我的消息的机器人.
如果我发送Hi!到机器人,它将回答Well, hello there!.我只是想知道,我该如何为机器人提供多种答案选择呢?有没有办法使用JavaScript从响应数组中选择一个随机项?
我在下面使用ng-repeat:
<div ng-repeat="quote in quotes">
{{quote.value}}
</div>
Run Code Online (Sandbox Code Playgroud)
迭代引号变量:
app.controller("MainController", function($scope){
$scope.quotes = [
{
value: "q1"
},
{
value: "q2"
},
];
});
Run Code Online (Sandbox Code Playgroud)
我想从这个引号变量中只选择一个随机元素,而不是遍历整个数组.在angualrjs中是否有指令可用于实现此目的?
假设我们有一个这样的数组:
var MrArray = new Array(5);
MrArray['one']='oneValue';
MrArray['two']='twoValue';
MrArray['three']='threeValue';
MrArray['four']='fourValue';
MrArray['five']='fiveValue';
Run Code Online (Sandbox Code Playgroud)
好?数组是关联的.我们有字符串键和字符串值.现在!我如何从中选择一个随机值? 编辑:我想这样使用:
<A href="Array value Here">Array Key Here</a>
Run Code Online (Sandbox Code Playgroud)
问候萨姆
我想使用 javascript 创建 1000 个单词的虚拟文本。我的想法是使用这样的数组
var words =["The sky", "above", "the port","was", "the color of television", "tuned", "to", "a dead channel", ".", "All", "this happened", "more or less","." ,"I", "had", "the story", "bit by bit", "from various people", "and", "as generally", "happens", "in such cases", "each time", "it", "was", "a different story","." ,"It", "was", "a pleasure", "to", "burn"];
Run Code Online (Sandbox Code Playgroud)
然后使用javascript随机输出1000个单词。我将如何使用 javascript 做到这一点?还有其他方法吗?
我想用jquery动画函数设置背景颜色,我需要从数组中选择一个随机元素.
$(".menu li").hover(function() {
$(this).animate({borderBottomColor:"#81C6DD"}, 200)
}, function() {
$(this).animate({borderBottomColor:"#D8D9DC"}, 200)
});
Run Code Online (Sandbox Code Playgroud) 我有一个包含一些函数的数组,它看起来像这样:
var all_questions = [
show_question(1, 1),
show_question(2, 1),
show_question(3, 1),
];
Run Code Online (Sandbox Code Playgroud)
我想随机将这些函数运行到该数组中.我怎样才能做到这一点?