从一个数组中挑选并 return 一对值到两个不同的输出中。 JavaScript
Pick and return a pair of values from an array into two different outputs. JavaScript
我有一个函数,它从数组中获取一个随机元素,然后按下按钮,return将随机值发送到输出。代码如下:
const questions = ["What is your name?", "How old are you?", "Where are you from?"];
const randomQuestion = () => {
let random = questions[Math.floor(Math.random() * questions.length)];
document.getElementById("question").innerHTML = random;
};
<div>
<output id="question" class="question"></output>
</div>
<button id="questionBtn" class="questionBtn" onclick="randomQuestion();">Ask</button>
到目前为止代码运行良好,return输出“问题”数组中的随机值。但是,我需要更进一步。我需要从数组中 return 两个不可分割的值,并且这些值中的每一个都需要在不同的输出中进行 returned(“问题”值转到“问题”输出,而“示例”值转到“示例”输出)
因此,我需要将我的“randomQuestion”函数转换为 returns,例如,输出“question”的值“What is your name”,以及输出“question”的值“Matheus”输出“示例”等。
const questions = [{
question: "What is your name?",
example: "Matheus"
},
{
question: "How old are you?",
example: "27"
},
{
question: "Where are you from?",
example: "Berlin"
}
];
const randomQuestion = () => {
let random = questions[Math.floor(Math.random() * questions.length)];
document.getElementById("question").innerHTML = random;
};
<div>
<output id="question" class="question"></output>
<output id="example" class="example"></output>
</div>
<button id="questionBtn" class="questionBtn" onclick="randomQuestion();">Ask</button>
你快到了
const questions = [{
question: "What is your name?",
example: "Matheus"
},
{
question: "How old are you?",
example: "27"
},
{
question: "Where are you from?",
example: "Berlin"
}
];
const randomQuestion = () => {
let random = questions[Math.floor(Math.random() * questions.length)];
document.getElementById("question").innerHTML = random.question;
document.getElementById("example").innerHTML = random.example;
};
<div>
<output id="question" class="question"></output>
<output id="example" class="example"></output>
</div>
<button id="questionBtn" class="questionBtn" onclick="randomQuestion();">Ask</button>
我有一个函数,它从数组中获取一个随机元素,然后按下按钮,return将随机值发送到输出。代码如下:
const questions = ["What is your name?", "How old are you?", "Where are you from?"];
const randomQuestion = () => {
let random = questions[Math.floor(Math.random() * questions.length)];
document.getElementById("question").innerHTML = random;
};
<div>
<output id="question" class="question"></output>
</div>
<button id="questionBtn" class="questionBtn" onclick="randomQuestion();">Ask</button>
到目前为止代码运行良好,return输出“问题”数组中的随机值。但是,我需要更进一步。我需要从数组中 return 两个不可分割的值,并且这些值中的每一个都需要在不同的输出中进行 returned(“问题”值转到“问题”输出,而“示例”值转到“示例”输出)
因此,我需要将我的“randomQuestion”函数转换为 returns,例如,输出“question”的值“What is your name”,以及输出“question”的值“Matheus”输出“示例”等。
const questions = [{
question: "What is your name?",
example: "Matheus"
},
{
question: "How old are you?",
example: "27"
},
{
question: "Where are you from?",
example: "Berlin"
}
];
const randomQuestion = () => {
let random = questions[Math.floor(Math.random() * questions.length)];
document.getElementById("question").innerHTML = random;
};
<div>
<output id="question" class="question"></output>
<output id="example" class="example"></output>
</div>
<button id="questionBtn" class="questionBtn" onclick="randomQuestion();">Ask</button>
你快到了
const questions = [{
question: "What is your name?",
example: "Matheus"
},
{
question: "How old are you?",
example: "27"
},
{
question: "Where are you from?",
example: "Berlin"
}
];
const randomQuestion = () => {
let random = questions[Math.floor(Math.random() * questions.length)];
document.getElementById("question").innerHTML = random.question;
document.getElementById("example").innerHTML = random.example;
};
<div>
<output id="question" class="question"></output>
<output id="example" class="example"></output>
</div>
<button id="questionBtn" class="questionBtn" onclick="randomQuestion();">Ask</button>