Quote API Challenge Project (JavaScript)

Hey! try returning your res.status(400).send()! See if that works :smiley:

Hey, thanks so much for your suggestion. I’ve tried returning that but unfortunately it’s still not returning a response :disappointed:

I think it’s because, as this is a post request and we are creating a single quote, and thus returning a single quote in our response, the singular quote reflects that.

Here’s my finished project.

Added PUT and DELETE methods, hard coded id values for every quote in database and created “generateId” function to attach unique id for every new quote in POST method.

Completed Project: :slight_smile:

Hi everyone! Here is my code. It’s about double the lines as the solution, so hopefully I can learn from this process! https://github.com/sarmstead/quote-api

Yes, it’s not a Gist, but you can still view the code! Please let me know what you think!


I have a question. I want to run npm deploy for this app, because I want to publish on github. It keeps giving me errors. I try to run npm build, but I got a bunch of errors for missing build. Does anyone know how to do. There’s no src folder with index.js inside. I am not sure what I can do. I am kinda new to react.

I believe there’s an issue with your code:

function getRandomInt(max) {
  return Math.floor(Math.random() * Math.floor(max));
const lengthQuotes = quotes.length;

// GET a random Quote.
app.get('/api/quotes/random', (req, res, next) => {
  const random = getRandomInt(lengthQuotes);
  const randomQuote = quotes[random].quote;
    quote: randomQuote

Firstly in utils.js file there is already a function (getRandomElement()) returning a random element from an array. It takes an array (‘quotes’) as an argument and returns a random ‘quote’ object. If you look at the top of your server.js file you’ll see that this function has already been imported and you can invoke it like this:

const randomQuote = getRandomElement(quotes);


    quote: getRandomElement(quotes)

Secondly, if you look at your getRandomInt(max) function, the Math.floor(max) isn’t necessary. Your array length is alway going to be an integer and doesn’t need to be rounded.

You could just have:

function getRandomInt(max) {
  return Math.floor(Math.random() * max);

or modify it slightly to return a random object (like the provided getRandomElement() function does)…

function getRandomObject(array) {
  const index = Math.floor(Math.random() * array.length);
  return array[index];

Finally, the reason you are seeing ‘undefined’ is because the client expects an object with a ‘quote’ object inside. This ‘quote’ object should have both ‘quote’ and ‘person’ properties. Instead you are sending a string with an actual quote.

You are currently sending:

    quote: "Blahblablahblah...."

While the client is expecting:

    quote: {
        quote: "Blahblablahblah....",
        person: "Blabby McBlabberface"

So, in theory in your current code removing the last word from this definition should fix your problem:

const randomQuote = quotes[random].quote;

I would expect new quotes I add through POST requests to appear in the data.js, but they don’t.
Why is that and where does the data go?