First element in an array returning 'undefined'

Hello everyone. My issue is not so serious but I am more curious as to why it’s happening. I am building a random quote machine, and no matter what quote is the first in my array, it will return undefined. (I have tried changing the quote) The rest are working just fine, just that first one is having a problem.
my full javascript

const button = document.querySelector('button');
const quote = document.querySelector('quote');
const quotelist = [ 
  'Our enemies are innovative and resourceful, and so are we. They never stop thinking about new ways to harm our country and our people, and neither do we.',
  'I know how hard it is for you to put food on your family.',
  'Rarely is the question asked: Is our children learning?', 'Too many good docs are getting out of the business. Too many OB/GYNs aren’t able to practice their love with women all across the country.',
  'You teach a child to read, and he or her will be able to pass a literacy test.',
  'Neither in French nor in English nor in Mexican.',
  'I’m the decider, and I decide what is best. And what’s best is for Don Rumsfeld to remain as the secretary of defense.',
  'See, in my line of work you got to keep repeating things over and over and over again for the truth to sink in, to kind of catapult the propaganda.',
  'And so, General, I want to thank you for your service. And I appreciate the fact that you really snatched defeat out of the jaws of those who are trying to defeat us in Iraq.',
  'There’s an old saying in Tennessee—I know it’s in Texas, probably in Tennessee—that says, fool me once, shame on—shame on you. Fool me—you can’t get fooled again.'
];

button.addEventListener('click', changeQuote)

function changeQuote () {
  const QuoteIndex= parseInt(Math.random()*quotelist.length+1);
  span = document.getElementById("quote");
  txt = document.createTextNode(quotelist[QuoteIndex]);
  $("span").html(txt);
}

and in case that’s not the problem, a link to the full code:


I hope this doesn’t take too much of your time, I unfortunately cannot find someone with the exact same problem online.

try getting rid of the + 1 in line 11

1 Like

Your quotes array’s length is 10, but since arrays are zaro based, the last quote is in index 9. By adding one you are now generating a random number with 1 higher than your original array. So you really dont need to add one to it. Hope that makes sense.

1 Like

Solved, thank you.
I’m not sure why that +1 was in there in the first place