0

I want to get a random quote from an array of objects with quotes with their respective author name by clicking a button. So far I am in a roadblock and can't seem to understand how to proceed. Below is the markup Get a random code with the click of a button!


Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

John Doe

GET A NEW QUOTE!

<script type="text/javascript">
const allQuotes = [

{   
qid: 1,
quote: "Quote 1",
auth: "ABC DEF"
},

{       
qid: 2,
quote: "Quote 2",
auth: "XYZ ABC"
},

{       
qid: 3,
quote: "Quote 3",
auth: "XYZ ABC"
},

{       
qid: 4,
quote: "Quote 4",
auth: "XYZ ABC"
},

{       
qid: 5,
quote: "Quote 5",
auth: "XYZ ABC"
},

{   
qid: 6,
quote: "Quote 6",
auth: "XYZ ABC"
}
</script>

1 Answer 1

1
const allQuotes = [{qid: 1, quote: "Quote 1", auth: "ABC DEF"}, {qid: 2, quote: "Quote 2", auth: "XYZ ABC"}, {
  qid: 3,
  quote: "Quote 3",
  auth: "XYZ ABC"
}, {qid: 4, quote: "Quote 4", auth: "XYZ ABC"}, {qid: 5, quote: "Quote 5", auth: "XYZ ABC"}, {
  qid: 6,
  quote: "Quote 6",
  auth: "XYZ ABC"
}];

const randomObj = (arr = []) => {
  const num = Math.floor(Math.random() * arr.length);
  return arr[num]
};
console.log(randomObj(allQuotes));


Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.