Random Quote Machine snag

Tell us what’s happening:

I have created a functional random quote machine, but in order to pass the final user story I need to link my “tweet” button directly to a twitter url (
twitter.com/intent/tweet”) inside of the html. This Url is currently provided in my javascript, which freecodecamp deems incorrect.

is it possible to get the freecodecamp script to pass my build without breaking my jquery target?

Your code so far


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36.

Link to the challenge:

Your quote generator “works” in the sense that it accomplishes what it is supposed to, but not in a way that it can be tested - or at least not in the way that the FCC test works. We could debate the fairness of, but this is sometimes how web dev is - sometimes you have do not just what the specs say very specifically to do, but also how they say to do it.

The instructions say:

  • User Story #10: I can tweet the current quote by clicking on the #tweet-quote a element. This a element should include the "twitter.com/intent/tweet" path in it’s href attribute to tweet the current quote.

It is saying that the complete url for the tweeting must be in the anchor tag. You didn’t solve the problem that way, you did it with a click handler. This is a perfectly reasonable way to handle the problem, but is not what you were told to do.

Your anchor is:

<a id = "tweet-quote" href = "#">Tweet Quote</a>

The test looks at that id and sees that it is not what it is expecting.

  1. I can tweet the current quote by clicking on the #tweet-quote element. This element should include the “twitter.com/intent/tweet” path in it’s href attribute to tweet the current quote.
    The #tweet-quote element does not utilize the correct twitter intent : expected ‘https://s.codepen.io/boomerang/iframekey-29c51022-e3f7-8b01-1a71-7fd7835234b7/index.html#’ to include ‘twitter.com/intent/tweet
    AssertionError: The #tweet-quote element does not utilize the correct twitter intent : expected ‘https://s.codepen.io/boomerang/iframekey-29c51022-e3f7-8b01-1a71-7fd7835234b7/index.html#’ to include ‘twitter.com/intent/tweet’…

The simplest way to do this is to not use a click handler for the tweet, but when you generate the quote your function getQuote, to also use jQuery to change the href in that element.

For example, if I wanted to make that button open up google, I would do:

  $("#tweet-quote").attr("href", "http://google.com");

That goes out into the DOM and overwrites the href of that element so it points to where I want it to go. jQuery explains it here.

The other thing you’ll need to do is give that element and attribute of target = "_blank". This is peculiar to codepen and is necessary if you want it to open up a new window. Without it it will pass the test but not work physically as expected.

The last thing I might suggest is to call getQuote as the last thing in your $(document).ready callback. This is not necessary to pass the test, but that way the tweet button has something to tweet if they press it before getting a new quote.

When I make these changes, your app passes the tests.

I know it’s frustrating, but a big part of web dev is paying attention to small details. And sometimes the specs expect you to do things that you don’t think are the “best” way.

2 Likes

I understand that I must develop in a way that will pass the tests, I just couldn’t figure out how to do it. Thanks!

I tried your suggestion and it passes the tests, but now I can no longer add the quotes or authors to my tweet button.

it opens a new window with a twitter page, but said page does not include a quote as it did before

You didn’t quite do what I suggested. Please reread my answer. Let us know if you need anything clarified.