意外的类型错误 - Javascript,多维数组
Unexpected Type Error - Javascript, multidimensional arrays
我正在学习编写Javascript代码。我正在尝试创建一个随机报价生成器。这个想法是我有一个创建多维数组的方法,每个元素数组都有一个引号和作者的名字。此方法返回多维数组。
我将这个返回的多维数组分配给一个变量并选择一个随机元素数组。它在控制台中给了我一个"意外的类型错误"。
<script>
var colors = ['#16a085', '#27ae60', '#2c3e50', '#f39c12', '#e74c3c', '#9b59b6',
'#FB6964', '#342224', '#472E32', '#BDBB99', '#77B1A9', '#73A857'];
console.log("Hi!");
function getQuote(){
var quotesAndAuthors =
[
['But suicides have a special language. Like carpenters, they want to know "which tools", they never ask "why build"', "Anne Sexton"],
['Here is the world. Beautiful and terrible things will happen. Dont be afraid', 'Frederick Buechner'],
['All grown-ups were once children, but only a few of them remember it', 'Alexander de Saint-Exupery'],
['It was love at first sight, at last sight, at ever ever sight', 'Vladimir Nabokov'],
['A paradise the color of hell flames, but a paradise nonetheless', 'Vladimir Nabokov'],
['There is nothing like the deep breaths after laughing that hard. Nothing in the world like a sore stomach for the right reasons','Stephen Chbosky'],
['And then suddenly, something is over','Louise Gluck'],
['Adventure is out there!', 'Up (Pixar)'],
['The strong do what they can, and the weak suffer what the must', 'Thucydides'],
['But who prays for Satan? Who, in eighteen centuries, has had the common humanity to pray for the one sinner that needed it most?', 'Mark Twain'],
['I stopped explaining myself when I realized people only understand from their level of perception', 'Unknown'],
['Unexpressed emotions will never die. They are buried alive and will come forth in uglier ways', 'Sigmund Freud'],
['Genius might be the ability to say a profound thing in a simple way', 'Charles Bukowski']
];
return quotesAndAuthors;
}
function generate(){
var pickColor = Math.floor(Math.random * colors.length);
$('html body').animate({
backgroundColor: colors[pickColor]
}, 1000);
$('#text #author').animate({
color: colors[pickColor]
}, 500);
$('.buttons').animate({
backgroundColor: colors[pickColor]
},500);
var quotes = getQuote();
var n = Math.floor(Math.random * quotes.length);
var quoteText = quotes[n][0];
var quoteAuthor = quotes[n][1];
$('#text').text(quoteText);
$('#author').text(quoteAuthor);
}
$(document).ready(function(){
generate();
alert("Testing");
$('#quoteButton').on('click', generate());
});
</script>
此外,有关如何更有效地存储报价的建议将不胜感激。
随机是一个函数而不是一个属性。你应该使用偏执狂,比如
var n = Math.floor(Math.random() * quotes.length);
此外,在添加事件侦听器时,不应使用paranthesis,因为这会在单击事件之前调用该方法。只需给出函数名称即可。
$('#quoteButton').on('click', generate);
此外,最好在您的情况下使用对象的排列,如下所示:
var quotesAndAuthors = [
{
"quote" : "But suicides have a special language",
"athour" : "Anne Sexton"
},
{
"quote" : "All grown-ups were once children",
"athour" : "Alexander de Saint-Exupery"
}
];
您可以使用以下任一方法访问报价:
console.log(quotesAndAuthors[0]["quote"]);
console.log(quotesAndAuthors[0].quote);
.random 不是 Math 对象的属性。Math.random() 是一个方法。
你想调用Math.random()
,因为这是一个函数(注意括号)。这产生了你的错误。
相关文章:
- 如何遍历包含对象的数组-javascript
- 保存数组javascript
- 查找数组javascript中包含的元素类型
- 算法:从数组(javascript/angular)中按当前日期获取上一个和下一个事件
- 从多维数组javascript中提取特定值
- 如何在数组javascript中选择伪随机值
- 拆分字符串数组(JavaScript)后未定义
- 从数组JavaScript中删除并返回最后n个项的最快方法
- 使用条件for循环更新数组-Javascript
- 从数组javascript创建新对象
- 用数组(javascript)中的值替换regex捕获
- 从数组[Javascript]的总长度中减去一个干净的数字
- 将一个字符串数组解析为一个新的数组javascript
- 如何将对象转换为对象数组javascript
- 赢得't循环数组javascript
- 从不同的数组 JavaScript 中获取值
- 多维数组 JAVASCRIPT 出了点问题
- 可以't分配给一个对象数组javascript
- 比较数组JavaScript中的对象
- 如何完成缺少(连续)元素的数组|Javascript