Hey everyone, I’m having a problem with my RQM and would love it if someone could help me. I’ve learned how to do an AJAX request and it is successfully retrieving my JSON; however, when I click on the New Quote button it randomly retrieves the quote, but sometimes it doesn’t change the author
Here is my code- https://codepen.io/mattwr18/pen/WOZaaQ
I still need to clean up some things and work better on the styling, so please don’t take it as a finished project.
Any help would be greatly appreciated. Also, if anyone is keen for some pair programming, I’m down.
The response object occasionally misspells the property
As far as the author not changing, there are only 17 different quotes in the response, so it’s very possible that clicking new quote will generate the same quote.
Also, the response is an array of objects, so to generate a random index use
var random = Math.floor(Math.random() * json.length);
I’m really embarrased. I misspelled quote and then copied and pasted it. I hadn’t even considered it was due to a spelling mistake. I need to be more careful if I wanna be a programmer
So, the problem wasn’t that it was generating the same quote, but that it wasn’ finding the quote property of the random index because it was misspelled, but it could find the author so it was leaving the quote and just changing the author. However, I have already thought about adding a currentQuote and an if statement to avoid generating the same quote.
I originally wrote my var random like you have above, but when I started having problems, I started reading stuff on stackoverflow and tried changing some things. I agree that json.length is better though.
Anyways, thank you so much for you help and next time I will make sure I check basic spelling before agonizing so much and wasting your time.
And the problem with using
.ceil() for creating a random number would allow random to occasionally be equal to the length of the array (along with never being able to access the 0th element).
array[array.length] // undefined
Glad to hear it’s all sorted now
You aren’t wasting anyone’s time, you had a legitimate question and it was answered. However silly the mistake may of been, it was legitimate, and so help was provided.
That’s one of the most common mistakes we make as programmers - typos that break stuff. The important thing is being able to find the source of problems and be able to fix it. Remember - you can do it and as always feel free to ask when you need help
Yeah, we all make silly little mistakes. It happens. It will keep happening. Maybe you’ll get a little better at finding them, but there will always be one here and there that drives you crazy.
That moment when you realize it’s all because line 1769 is missing a semi-colon