I would like to use the same var randomQuote for all 3 functions (newQuote, postFB and tweet) and that does not seem to work.
If I put it inside the newQuote function only this function will work, if only outside - only share functions work and newQuote will only change the quote once on click (?).
The only way I can make all 3 functions to work is if I repeat it - one inside and one outside the newQuote function - is there any way to make it “DRY-er” ?
var randomQuote = Math.floor(Math.random() * (quotes.length)); //outside
document.getElementById("refresh").onclick = newQuote;
function newQuote() {
var randomQuote = Math.floor(Math.random() * (quotes.length)); //inside
document.getElementById("facebook").onclick = postFB;
function postFB() {
var url = "http://www.facebook.com/sharer/sharer.php?u=" + encodeURIComponent(quotes[randomQuote]);
window.open(url);
}
- did not include twitter function here - same idea as FB
any suggestions what I'm ( a beginner) doing wrong? Thank you in advance!