Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
random element from an array is undefined

I've done my shot as a beginner JS coder and reworked a quote generator app from an array on the click of a button. I managed to display next element each time I press the button. However, in the tutorial the elements were displayed at random, so I followed the instructions, but instead of a random quote I get "undefined". I'm stuck. It seems to me that I've done everything correctly but I might have slipped somewhere. Can anyone help me?

let btn = document.getElementById("btn");
let output = document.getElementById("output");
let quotes = [
  "The greatest glory in living lies not in never falling, but in rising every time we   fall. -Nelson Mandela",
  "The way to get started is to quit talking and begin doing. -Walt Disney",
  "Your time is limited, so don't waste it living someone else's life. Don't be trapped by dogma – which is living with the results of other people's thinking. -Steve Jobs",
  "If life were predictable it would cease to be life, and be without flavor. -Eleanor Roosevelt",
  "If you look at what you have in life, you'll always have more. If you look at what you don't have in life, you'll never have enough. -Oprah Winfrey",
  "If you set your goals ridiculously high and it's a failure, you will fail above everyone else's success. -James Cameron",
  "Life is what happens when you're busy making other plans. -John Lennon",
];
btn.addEventListener("click", function() {
  let randomQuote = quotes[Math.floor(Math.random() * quotes.lenth)];
  output.innerHTML = randomQuote;
});
<h1>Quote Generator</h1>
<div class="content">
  <button id="btn">Press for a new Quote!</button>
  <p id="output">Press the button to generate a quote.</p>
</div>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your code perfect just quotes.lenth --> quotes.length :)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!