Random Quote sent to Twitter problem

Random Quote sent to Twitter problem
0

#1

Hi,

I’m stuck at the stage where I can’t get my quote into the Twitter box. I’ve looked around and nothing really shows me how to do it. I’ve cut and paste code but it never works in my particular situation.

I feel unprepared for this step. There were a lot of lessons on algorithmic coding which I knew about already and was fun because it was puzzle solving. This part of the programming that I’m having problems with I feel isn’t properly covered, skimmed over. I feel dumb and like an outsider. “What am I missing?” I go on W3 and stack overflow but it goes over my head because my lack of knowledge, like I missed a term. I become discouraged. You could show me what lines I’m missing and that would be great (Thanks!) but I wouldn’t know WHY I should be writing those lines.

Thanks & Regards


#2

Why not give the following div an id like “tweetDiv”, then in your code you can change the innerHTML of the div inside your getRandomQuote function:

  var twitterQuote = theQuoteAndAuthor[0].replace(/"/g,'').replace(/;/g,'%3B') + ' - ' + theQuoteAndAuthor[1];
  document.getElementById('tweetDiv').innerHTML =  
    '<a href="https://twitter.com/intent/tweet?text=' + 
    twitterQuote + '" class="fa fa-twitter" target="_blank"> Tweet This</a>';

#3

Thanks Randell,

Do you mean all of what you wrote gets encased in a

in my HTML window?
or only a part of it?

or does it go in the function? I don’t understand, sorry


#4

It goes inside your getRandomQuote function.


#5

Thanks Randall

I got that working now. Will look at why it works.
Out of the 3 parts of the problem, I only knew what to move.
I couldn’t describe in code how or where to move it.

Regards,
DJW


#6

The line above is needed, because of two separate issues (explained below to why I used the replace function twice).

#1) For some reason, because the quote had double quotes in it, it was causing some syntax errors when trying to concatenate the the quote portion with the author portion. So, the first replace uses a regular expression to replace all instances of double quotes with a blank string.

After thinking about it a bit more, since you still want the double quote to appear in the Twitter tweet box, you could use replace(/"/g,’%22’) instead of what I originally suggested ( the replace(/"/g,’’) part ). This will escape the double quote allow the Twitter API to display it as a double quote.

#2) The second replace was needed, because when a quote had a semi-colon in it, the Twitter API was cutting off the rest of the quote. I used a regular expression (/;/g) to replace all semi-colons with %3B (the escape character for a semi-colon) so that the twitter API would still display it as a semi-colon, but would not cut off the rest of the string.