How can i link a specific array item from one list, to a corresponding array item from a different array list?

huangapple go评论67阅读模式
英文:

How can i link a specific array item from one list, to a corresponding array item from a different array list?

问题

我正在使用VSCode创建一个受Star Wars启发的随机引用生成器,它会提供一句随机的引用,同时给出一个现实生活中可以使用的场景(建议),但我遇到了一个应该很容易解决的问题。

我有两个不同的数组,一个是“quotes”数组,另一个是“advice”数组。当我从“quotes”数组中随机调用一句引用时,我希望出现的引用会触发“advice”数组中对应的一句建议,以便它们一起显示。例如,引用“1”与建议“1”相配。我成功地做到了当我在网站上按下“获取引用”按钮时,它触发了一个随机的引用,但是同时触发了一句随机的建议,而不是我想要显示的建议。

我该如何将一个数组中的引用与另一个数组中的建议配对,并且在调用随机引用时,也将与之配对的建议输出到页面上?
希望我已经足够清楚地解释了这个问题,这是我第一次发布。提前感谢。以下是JavaScript代码。

let btn = document.getElementById("btn");
let output = document.getElementById("output");
let quotes = [
  /*1*/ '"Its not my fault." - Han Solo',
  /*2*/ '"Your focus determines your reality." - Qui-Gon Jinn',
  /*3*/ '"Do. Or do not. There is no try." - Yoda',
  /*4*/ '"Somebody has to save our skins." - Leia Organa',
  /*5*/ '"In my experience there is no such thing as luck." - Obi-Wan Kenobi',
  /*6*/ '"I find your lack of faith disturbing." - Darth Vader',
  /*7*/ '"Ive got a bad feeling about this." - Basically Everyone',
  /*8*/ '"Its a trap!" - Admiral Ackbar',
  /*9*/ '"So this is how liberty dies...with thunderous applause." - Padmé Amidala',
  /*10*/ '"Your eyes can deceive you. Dont trust them." - Obi-Wan Kenobi',
  /*11*/ '"Never tell me the odds." - Han Solo',
  /*12*/ '"RWAAARWWR!" - Chewbacca',
  /*13*/ '"Stay on target." - Gold Five',
  /*14*/ '"This is a new day, a new beginning." - Ahsoka Tano',
  /*15*/ '"This is the way." - The Mandalorian',
];

let advice = [
  /*1*/ "Use when anything goes wrong, even if it is totally your fault.",
  /*2*/ "Use in pep talks to encourage positivity and to remind others to take control of their fate.",
  /*3*/ "Use when someone needs a little tough love.",
  /*4*/ "Use when you jump in and solve a problem without breaking a sweat.",
  /*5*/ "Use to remind others hard work pays off and sitting around waiting for chance does not.",
  /*6*/ "Use anytime others doubt your plans.",
  /*7*/ "Use when walking into a situation that could end poorly.",
  /*8*/ "Use anytime you suspect something is too good to be true.",
  /*9*/ "Use sarcastically whenever anyone institutes a new policy that looks appealing on the surface but has negative repercussions.",
  /*10*/ "Use when a friend needs to be reminded to go with his or her gut feeling.",
  /*11*/ "Use whenever you are told a task cant be done.",
  /*12*/ "Use when you move a chair",
  /*13*/ "Use to keep yourself or others focused.",
  /*14*/ "Use to cheer a pal up and remind him or her that every day brings new opportunities.",
  /*15*/ "Use when one makes the right decision",
];

// combine quote and advice
btn.addEventListener("click", function () {
  var randomQuote = quotes[Math.floor(Math.random() * quotes.length)];
  var randomAdvice = advice[Math.floor(Math.random() * advice.length)];
  output.innerHTML = randomQuote + "<br>" + randomAdvice;
});

我尝试将引用1分配给建议1,如下所示:

quote[0] = advice[0];

等等,但是这只是复制了引用两次,而没有将建议1分配给引用1。

英文:

So i am creating a Star Wars inspired random quote generator on vscode which gives a random quote and also gives a real life scenario in which it could be used (advice), but i have encountered an issue which should be simple enough to fix.

I have two different arrays, a 'quotes' array and a 'advice' array. When i call one of the quotes at random from 'quotes' array, i want that quote that appeared, to trigger a corresponding line from the 'advice' array so that they appear together. For example quote '1' pairs with advice '1'. I managed to get to the point where when i press the 'Get Quote' button on my site, it triggers a random quote as it should, but also a random line of advice rather than the one i would like to appear.

How can i pair a quote from one array with an advice from the other array, and when the random quote is called, also have the advice that pairs with it, also output to the page?
Hope i have explained this well enough, first time posting. Thanks in advance. JavaScript Code Blow.

let btn = document.getElementById(&quot;btn&quot;);
let output = document.getElementById(&quot;output&quot;);
let quotes = [
/*1*/ &#39;&quot;Its not my fault.&quot; - Han Solo&#39;,
/*2*/ &#39;&quot;Your focus determines your reality.&quot; - Qui-Gon Jinn&#39;,
/*3*/ &#39;&quot;Do. Or do not. There is no try.&quot; - Yoda&#39;,
/*4*/ &#39;&quot;Somebody has to save our skins.&quot; - Leia Organa&#39;,
/*5*/ &#39;&quot;In my experience there is no such thing as luck.&quot; - Obi-Wan Kenobi&#39;,
/*6*/ &#39;&quot;I find your lack of faith disturbing.&quot; - Darth Vader&#39;,
/*7*/ &#39;&quot;Ive got a bad feeling about this.&quot; - Basically Everyone&#39;,
/*8*/ &#39;&quot;Its a trap!&quot; - Admiral Ackbar&#39;,
/*9*/ &#39;&quot;So this is how liberty dies...with thunderous applause.&quot; - Padm&#233; Amidala&#39;,
/*10*/ &#39;&quot;Your eyes can deceive you. Dont trust them.&quot; - Obi-Wan Kenobi&#39;,
/*11*/ &#39;&quot;Never tell me the odds.&quot; - Han Solo&#39;,
/*12*/ &#39;&quot;RWAAARWWR!&quot; - Chewbacca&#39;,
/*13*/ &#39;&quot;Stay on target.&quot; - Gold Five&#39;,
/*14*/ &#39;&quot;This is a new day, a new beginning.&quot; - Ahsoka Tano&#39;,
/*15*/ &#39;&quot;This is the way.&quot; - The Mandalorian&#39;,
];
let advice = [
/*1*/ &quot;Use when anything goes wrong, even if it is totally your fault.&quot;,
/*2*/ &quot;Use in pep talks to encourage positivity and to remind others to take control of their fate.&quot;,
/*3*/ &quot;Use when someone needs a little tough love.&quot;,
/*4*/ &quot;Use when you jump in and solve a problem without breaking a sweat.&quot;,
/*5*/ &quot;Use to remind others hard work pays off and sitting around waiting for chance does not.&quot;,
/*6*/ &quot;Use anytime others doubt your plans.&quot;,
/*7*/ &quot;Use when walking into a situation that could end poorly.&quot;,
/*8*/ &quot;Use anytime you suspect something is too good to be true.&quot;,
/*9*/ &quot;Use sarcastically whenever anyone institutes a new policy that looks appealing on the surface but has negative repercussions.&quot;,
/*10*/ &quot;Use when a friend needs to be reminded to go with his or her gut feeling.&quot;,
/*11*/ &quot;Use whenever you are told a task cant be done.&quot;,
/*12*/ &quot;Use when you move a chair&quot;,
/*13*/ &quot;Use to keep yourself or others focused.&quot;,
/*14*/ &quot;Use to cheer a pal up and remind him or her that every day brings new opportunities.&quot;,
/*15*/ &quot;Use when one makes the right decision&quot;,
];
// combine quote and advice
// commit changes to git
btn.addEventListener(&quot;click&quot;, function () {
var randomQuote = quotes[Math.floor(Math.random() * quotes.length)];
var randomAdvice = advice[Math.floor(Math.random() * advice.length)];
output.innerHTML = randomQuote + &quot;&lt;br&gt;&quot; + randomAdvice;
});

I tried assigning quote 1 to advice 1 like so

quote[0] = advice[0];

And so on and soforth, however this just replicated the quote twice instead of asigning advice 1 to quote 1.

答案1

得分: 0

生成随机索引的代码行 (Math.floor(Math.random()...) 被调用了两次,导致你为每个数组都有不同的随机索引。你需要将随机索引分配给一个变量,然后在分配后使用它。

由于引语和建议的长度都相等,所以你可以使用任何一个数组来生成随机数,如下所示:

let btn = document.getElementById('btn');
let output = document.getElementById('output');
let quotes = [
    /*1*/ '"Its not my fault." - Han Solo',
    /*2*/ '"Your focus determines your reality." - Qui-Gon Jinn',
    /*3*/ '"Do. Or do not. There is no try." - Yoda',
    /*4*/ '"Somebody has to save our skins." - Leia Organa',
    /*5*/ '"In my experience there is no such thing as luck." - Obi-Wan Kenobi',
    /*6*/ '"I find your lack of faith disturbing." - Darth Vader',
    /*7*/ '"Ive got a bad feeling about this." - Basically Everyone',
    /*8*/ '"Its a trap!" - Admiral Ackbar',
    /*9*/ '"So this is how liberty dies...with thunderous applause." - Padmé Amidala',
    /*10*/ '"Your eyes can deceive you. Dont trust them." - Obi-Wan Kenobi',
    /*11*/ '"Never tell me the odds." - Han Solo',
    /*12*/ '"RWAAARWWR!" - Chewbacca',
    /*13*/ '"Stay on target." - Gold Five',
    /*14*/ '"This is a new day, a new beginning." - Ahsoka Tano',
    /*15*/ '"This is the way." - The Mandalorian',
];

let advice = [
    /*1*/ "Use when anything goes wrong, even if it is totally your fault.",
    /*2*/ "Use in pep talks to encourage positivity and to remind others to take control of their fate.",
    /*3*/ "Use when someone needs a little tough love.",
    /*4*/ "Use when you jump in and solve a problem without breaking a sweat.",
    /*5*/ "Use to remind others hard work pays off and sitting around waiting for chance does not.",
    /*6*/ "Use anytime others doubt your plans.",
    /*7*/ "Use when walking into a situation that could end poorly.",
    /*8*/ "Use anytime you suspect something is too good to be true.",
    /*9*/ "Use sarcastically whenever anyone institutes a new policy that looks appealing on the surface but has negative repercussions.",
    /*10*/ "Use when a friend needs to be reminded to go with his or her gut feeling.",
    /*11*/ "Use whenever you are told a task cant be done.",
    /*12*/ "Use when you move a chair",
    /*13*/ "Use to keep yourself or others focused.",
    /*14*/ "Use to cheer a pal up and remind him or her that every day brings new opportunities.",
    /*15*/ "Use when one makes the right decision",
];

// 合并引语和建议
btn.addEventListener('click', function () {
    // 将随机数存储到变量中,而不是调用两次
    const randomIndex = Math.floor(Math.random() * quotes.length);

    var randomQuote = quotes[randomIndex];
    var randomAdvice = advice[randomIndex];
    output.innerHTML = randomQuote + " " + randomAdvice;
})

希望这有所帮助。

英文:

the line that generate random index (Math.floor(Math.random()...) is called twice, resulting you have different random index for each array. You need to assign the random index to a variable and use it after the assignment.

Since the length for both the quote and advice are equal, then you can generate the random number using any of the array like below.

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->

let btn = document.getElementById(&#39;btn&#39;);
let output = document.getElementById(&#39;output&#39;);
let quotes = [
/*1*/ &#39;&quot;Its not my fault.&quot; - Han Solo&#39;,
/*2*/ &#39;&quot;Your focus determines your reality.&quot; - Qui-Gon Jinn&#39;,
/*3*/ &#39;&quot;Do. Or do not. There is no try.&quot; - Yoda&#39;,
/*4*/ &#39;&quot;Somebody has to save our skins.&quot; - Leia Organa&#39;,
/*5*/ &#39;&quot;In my experience there is no such thing as luck.&quot; - Obi-Wan Kenobi&#39;,
/*6*/ &#39;&quot;I find your lack of faith disturbing.&quot; - Darth Vader&#39;,
/*7*/ &#39;&quot;Ive got a bad feeling about this.&quot; - Basically Everyone&#39;,
/*8*/ &#39;&quot;Its a trap!&quot; - Admiral Ackbar&#39;,
/*9*/ &#39;&quot;So this is how liberty dies...with thunderous applause.&quot; - Padm&#233; Amidala&#39;,
/*10*/ &#39;&quot;Your eyes can deceive you. Dont trust them.&quot; - Obi-Wan Kenobi&#39;,
/*11*/ &#39;&quot;Never tell me the odds.&quot; - Han Solo&#39;,
/*12*/ &#39;&quot;RWAAARWWR!&quot; - Chewbacca&#39;,
/*13*/ &#39;&quot;Stay on target.&quot; - Gold Five&#39;,
/*14*/ &#39;&quot;This is a new day, a new beginning.&quot; - Ahsoka Tano&#39;,
/*15*/ &#39;&quot;This is the way.&quot; - The Mandalorian&#39;,
];
let advice = [
/*1*/ &quot;Use when anything goes wrong, even if it is totally your fault.&quot;,
/*2*/ &quot;Use in pep talks to encourage positivity and to remind others to take control of their fate.&quot;,
/*3*/ &quot;Use when someone needs a little tough love.&quot;,
/*4*/ &quot;Use when you jump in and solve a problem without breaking a sweat.&quot;,
/*5*/ &quot;Use to remind others hard work pays off and sitting around waiting for chance does not.&quot;,
/*6*/ &quot;Use anytime others doubt your plans.&quot;,
/*7*/ &quot;Use when walking into a situation that could end poorly.&quot;,
/*8*/ &quot;Use anytime you suspect something is too good to be true.&quot;,
/*9*/ &quot;Use sarcastically whenever anyone institutes a new policy that looks appealing on the surface but has negative repercussions.&quot;,
/*10*/ &quot;Use when a friend needs to be reminded to go with his or her gut feeling.&quot;,
/*11*/ &quot;Use whenever you are told a task cant be done.&quot;,
/*12*/ &quot;Use when you move a chair&quot;,
/*13*/ &quot;Use to keep yourself or others focused.&quot;,
/*14*/ &quot;Use to cheer a pal up and remind him or her that every day brings new opportunities.&quot;,
/*15*/ &quot;Use when one makes the right decision&quot;,
];
// combine quote and advice
// commit changes to git 
btn.addEventListener(&#39;click&#39;, function () {
// store the random number to a variable instead of calling it twice
const randomIndex = Math.floor(Math.random() * quotes.length);
var randomQuote = quotes[randomIndex];
var randomAdvice = advice[randomIndex];
output.innerHTML = randomQuote + &quot; &quot; + randomAdvice;
})

<!-- end snippet -->

Hope it helps

答案2

得分: 0

以下是翻译好的部分:

最好的方法是使用对象来将特定问题与答案配对您可以创建一个对象数组其中每个对象可以包含特定的引用和建议类似于这样

let quotesArray = [
    {quote: '"这不是我的错。" - 汉·索洛', advice: "无论发生什么事情,即使完全是你的错,也可以使用。"},
    {quote: '"你的专注决定了你的现实。" - 奎-贡·吉恩', advice: "在鼓励积极性并提醒他人掌控自己命运时使用鼓励谈话中。"},
    {quote: '"做。或者不做。没有尝试。" - 尤达', advice: "当有人需要一点点 tough love 时使用。"},
    {quote: '"有人必须拯救我们的皮肤。" - 莉娅·奥加纳', advice: "当你毫不费力地解决问题时使用。"},
    {quote: '"在我看来,运气根本不存在。" - 奥比-万·克诺比', advice: "用来提醒其他人,辛勤工作会有回报,而坐在那里等待机会则不会。"},
    {quote: '"我发现你缺乏信仰令人不安。" - 达斯·维德', advice: "每当其他人对你的计划表示怀疑时使用。"},
    {quote: '"我对此感到不安。" - 基本上每个人', advice: "当走进一个可能会以失败告终的情况时使用。"},
    {quote: '"这是个陷阱!" - 阿克巴上将', advice: "每当你怀疑某事过于美好以至于不真实时使用。"},
    {quote: '"如此自由的消亡...伴随着雷鸣般的掌声。" - 帕德梅·阿米达拉', advice: "每当有人实施一项表面上看起来吸引人但有负面影响的新政策时,以讽刺的方式使用。"},
    {quote: '"你的眼睛会欺骗你。不要相信它们。" - 奥比-万·克诺比', advice: "当朋友需要被提醒跟随内心直觉时使用。"},
    {quote: '"永远不要告诉我几率。" - 汉·索洛', advice: "每当有人告诉你一个任务做不成时使用。"},
    {quote: '"RWAAARWWR!" - 切瓦卡', advice: "当你挪动椅子时使用"},
    {quote: '"保持目标稳定。" - 金五', advice: "用来保持你自己或他人的专注。"},
    {quote: '"今天是新的一天,一个新的开始。" - 阿索卡·塔诺', advice: "用来鼓励朋友并提醒他或她,每一天都带来新的机会。"},
    {quote: '"这就是方法。" - 曼达洛人', advice: "当一个人做出正确的决定时使用"},
];

 randomQuotes 数组中存储了带有引用和建议键的对象您可以像这样访问此对象的值

// 提交更改到 git
btn.addEventListener("click", function () {
  var randomQuoteObject = quotesArray[Math.floor(Math.random() * quotesArray.length)];

  output.innerHTML = randomQuoteObject.quote + "<br>" + randomQuoteObject.advice;
});
英文:

Best approach here will be to use objects in order to pair specific question with answer. You can create array of objects where each object can contain specific quote and advice, something like this:

let quotesArray = [
{quote: &#39;&quot;Its not my fault.&quot; - Han Solo&#39;, advice: &quot;Use when anything goes wrong, even if it is totally your fault.&quot;},
{quote: &#39;&quot;Your focus determines your reality.&quot; - Qui-Gon Jinn&#39;, advice: &quot;Use in pep talks to encourage positivity and to remind others to take control of their fate.&quot;},
{quote: &#39;&quot;Do. Or do not. There is no try.&quot; - Yoda&#39;, advice: &quot;Use when someone needs a little tough love.&quot;},
{quote: &#39;&quot;Somebody has to save our skins.&quot; - Leia Organa&#39;, advice: &quot;Use when you jump in and solve a problem without breaking a sweat.&quot;},
{quote: &#39;&quot;In my experience there is no such thing as luck.&quot; - Obi-Wan Kenobi&#39;, advice: &quot;Use to remind others hard work pays off and sitting around waiting for chance does not.&quot;},
{quote: &#39;&quot;I find your lack of faith disturbing.&quot; - Darth Vader&#39;, advice: &quot;Use anytime others doubt your plans.&quot;},
{quote: &#39;&quot;Ive got a bad feeling about this.&quot; - Basically Everyone&#39;, advice: &quot;Use when walking into a situation that could end poorly.&quot;},
{quote: &#39;&quot;Its a trap!&quot; - Admiral Ackbar&#39;, advice: &quot;Use anytime you suspect something is too good to be true.&quot;},
{quote: &#39;&quot;So this is how liberty dies...with thunderous applause.&quot; - Padm&#233; Amidala&#39;, advice: &quot;Use sarcastically whenever anyone institutes a new policy that looks appealing on the surface but has negative repercussions.&quot;},
{quote: &#39;&quot;Your eyes can deceive you. Dont trust them.&quot; - Obi-Wan Kenobi&#39;, advice: &quot;Use when a friend needs to be reminded to go with his or her gut feeling.&quot;},
{quote: &#39;&quot;Never tell me the odds.&quot; - Han Solo&#39;, advice: &quot;Use whenever you are told a task cant be done.&quot;},
{quote: &#39;&quot;RWAAARWWR!&quot; - Chewbacca&#39;, advice: &quot;Use when you move a chair&quot;},
{quote: &#39;&quot;Stay on target.&quot; - Gold Five&#39;, advice: &quot;Use to keep yourself or others focused.&quot;},
{quote: &#39;&quot;This is a new day, a new beginning.&quot; - Ahsoka Tano&#39;, advice: &quot;Use to cheer a pal up and remind him or her that every day brings new opportunities.&quot;},
{quote: &#39;&quot;This is the way.&quot; - The Mandalorian&#39;, advice: &quot;Use when one makes the right decision&quot;},
];

In randomQuotes array are stored objects with keys quote and advice. You can access to values of this object like this:

// commit changes to git
btn.addEventListener(&quot;click&quot;, function () {
var randomQuoteObject = quotesArray[Math.floor(Math.random() * quotesArray.length)];
output.innerHTML = randomQuoteObject.quote + &quot;&lt;br&gt;&quot; + randomQuoteObject.advice;
});

huangapple
  • 本文由 发表于 2023年6月26日 07:20:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552776.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定