dimanche 25 juin 2023

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

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("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
// commit changes to git
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;
});

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.




Aucun commentaire:

Enregistrer un commentaire