-1

This may seem like a very basic question but I can't figure out how to attempt this. I want to synchronize two arrays where the first string of the first var is paired with the first string on the second var. I want to change to the next var on both every 5 seconds that is linked to a paragraph tag on my html. I believe I am on the right track but don't quite know if I need the concat step.

I am new to javascript so thank you for your help!

var quotation = ["If your actions inspire others to dream more, learn more, do 
more and become more, you are a leader",
"A leader is one who knows the way, goes the way, and shows the way", 
"Leadership is practiced not so much in words as in attitude and in actions",
"Obstacles are things a person sees when he takes his eyes off his goal", 
"Innovation distinguishes between a leader and a follwer"];


var quotations = ["John Quincy Adams", " John C Maxwell", "Harold S Geneen", 
"Joseph Cossman", "Steve Jobs"];

var authors = quotation.concat(quotations);
  • 1
    I'm a little fuzzy on what you want to accomplish. Do you want an array that is the combination of `quotation` and `quotations` where the first element is a quote, the second is the author, the third is a quote, the fourth is the author, etc...? – mrogers Oct 06 '17 at 16:52
  • 1
    Possible duplicate of [Merge keys array and values array into an object in Javascript](https://stackoverflow.com/questions/1117916/merge-keys-array-and-values-array-into-an-object-in-javascript) – Get Off My Lawn Oct 06 '17 at 16:52
  • Have you posted all relevant code or do you have other JavaScript/HTML? – mrogers Oct 06 '17 at 17:04

2 Answers2

0

From your question it's VERY difficult to understand what you are trying to accomplish.

I think one problem you are having is that there is no relationship between the two arrays. Rather than have two different arrays containing related data, why not use an array of javascript objects? Doing this will keep your data together to begin with

let quotations = [
  {
    author: 'John Quincy Adams',
    quote: 'If your actions inspire others to dream more, learn more, do 
more and become more, you are a leader'
  }, 
  {
    author: 'John C Maxwell',
    quote: 'A leader is one who knows the way, goes the way, and shows the way'
  }
];

I'm not sure what is causing the data to change every 5 seconds because you did not post that code, but wherever that is happening, instead of adding the quote to one array, and the author to another, just add a quote object to the quotations array:

quotations.push({ author: 'Some Guy', quote: 'Some quote.' });
instantaphex
  • 981
  • 1
  • 9
  • 20
0

Here is something that might help you get started. Let me know if this isn't what you were looking for or if anything is unclear. The example below switches the quote every five seconds. The quote is initially empty in my example so you'll have to wait five seconds for it to populate but you could fill it with an initial value and start current at 1, for example.

var quotations = ["If your actions inspire others to dream more, learn more, do more and become more, you are a leader",
"A leader is one who knows the way, goes the way, and shows the way", 
"Leadership is practiced not so much in words as in attitude and in actions",
"Obstacles are things a person sees when he takes his eyes off his goal", 
"Innovation distinguishes between a leader and a follwer"];

var authors = ["John Quincy Adams", " John C Maxwell", "Harold S Geneen", 
"Joseph Cossman", "Steve Jobs"];

var current = 0;

setInterval(function() {
  document.getElementById("quote").innerText = quotations[current] + " - " + authors[current];
  current = (current + 1) % quotations.length;
},5000);
<p id="quote">
</p>
mrogers
  • 1,187
  • 2
  • 15
  • 22
  • mrogers thank you so much for your patience and help. I want to learn this language and I'm trying to practice. This really helped me understand how to be able to do this! – tamempalma Oct 06 '17 at 17:44