Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

In my code, i get an output in the web of undefined

let x = " ";

var quotation = '';

const quotes = [

"Don't Overthink Shit!",

"IT'S A MINDSET GAME.",

"Stop Worrying About What Your Friends Have."

];

let length = quotes.length;

function print(quotation){

const div = document.querySelector('.quote');

div.innerHTML = quotation;

}

let randomNumber = Get(length);

function Get(length){

return Math.floor(Math.random() * length);

}

function random(randomNumber){

let x = quotes[randomNumber];

quotation = x;

print(quotation);

}

console.log(randomNumber);

random();

Just wrapped your code in backticks (javascript) to make it readable. I'll see if I can help you.

let x = " ";

var quotation = '';

const quotes = [

"Don't Overthink Shit!",

"IT'S A MINDSET GAME.",

"Stop Worrying About What Your Friends Have."

];

let length = quotes.length;

function print(quotation){

const div = document.querySelector('.quote');

div.innerHTML = quotation;

}

let randomNumber = Get(length);

function Get(length){

return Math.floor(Math.random() * length);

}

function random(randomNumber){

let x = quotes[randomNumber];

quotation = x;

print(quotation);

}

console.log(randomNumber);

random();

2 Answers

how did you wrap it with backticks?

Code Wrap your code with 3 backticks (```) on the line before and after. If you specify the language after the first set of backticks, that'll help us with syntax highlighting.

      ```javascript
      some code here
      ```
var quotation = '';

const quotes = [

"Don't Overthink Shit!",

"IT'S A MINDSET GAME.",

"Stop Worrying About What Your Friends Have."

];

let length = quotes.length;

function print(quotation){
  const div = document.querySelector('.quote');
  div.innerHTML = quotation;
}

let randomNumber = get(length);

function get(length){
  return Math.floor(Math.random() * length);
}

function random(index){
  quotation = quotes[index];
  print(quotation);
}

console.log(randomNumber);

random(randomNumber);

There, this works. The problem was that at the end you did not give any argument to your random() method. I did some other changes as well, to make it easier for me to read. Your variable x was not doing anything useful so I removed it.

See what I did with

function random(index){
  quotation = quotes[index];
  print(quotation);
}

index there is just a parameter and could be named anything.

simplified it a bit for fun:

const quotes = [

"Don't Overthink Shit!",

"IT'S A MINDSET GAME.",

"Stop Worrying About What Your Friends Have."

];

function print(msg){
  const div = document.querySelector('.quote');
  div.innerHTML = msg;
}

function random(){
  const randomNumber =  Math.floor(Math.random() * quotes.length);
  const quotation = quotes[randomNumber];
  print(quotation);
}

random();