Random quote machine display double quotes and authors

Random quote machine display double quotes and authors
0.0 0

#1

I read my code almost 1000 times but still this question with no answer running in my head : :unamused:
Why my quotes and authors are displayed twice ? (You new to clic on ‘new quote’ to see the problem)

Many thanks for your help or advices on this.


#2

I tried it and, for me at least, it won’t load a new quote at all, so I can’t see the problem you’re asking about. It’s failing with “jquery.js:9600 GET https://andruxnet-random-famous-quotes.p.mashape.com/ 401 (Unauthorized)”


#3

If you look at your console, you will see the 401 code @r1chard5mith mentioned. 401 means you do not have the proper authorization to use the https://andruxnet-random-famous-quotes.p.mashape.com end point . You need to sign up for an account and they will give you some an API application key to use when calling the end point.

When I go directly to the url, I see the following in the browser:

{“message”:“Missing Mashape application key. Go to http://docs.mashape.com/api-keys to learn how to get your API application key.”}

Also, why are you using Babel JavaScript preprocessor and loading the React library when not using React in the code?


#4

Good point for the Babel one. My mistake for the wrong source, at tried something else late in the night and let it like that. I put the right source again, and you will be able to see the quote displayed in double.


#5

Thank you for your answer. You right, I put the right source again, and you will be able to see the quote displayed in double. Sorry for that.


#6

The reason you are seeing two of each is because of the following:

       var keys = Object.keys(val);
       keys.forEach(function(key) {
        quote += val.quote;
        author += val.author + "<br>";
       });

val contains two properties (quote and autor). For some reason, you created an array of the properties and then iterated over them. During each iteration of the forEach callback function, you concatenated quote with val.quote and autor with val.autor. Since there are two iterations (because of the two properties), you create two of each.

See below for your code with the unnecessary parts commented out.

       // var keys = Object.keys(val);
       // keys.forEach(function(key) {
        quote += val.quote;
        author += val.author + "<br>";
       //});

#7

Ok right I broke something. :roll_eyes:


#8

Thank you very much for your help. I understood my error thanks to you. Have a nice day