Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
  Tomas Rinblad
Full Stack JavaScript Techdegree Student 12,427 PointsDont initiate the else if command.
function getRandomQuote () { Number = Math.floor(Math.random() * quotes.length); var newQuote = quotes[Number].quote; if (newQuote !== usedQuotes) { usedQuotes.push(newQuote) return prop = quotes[Number]; } else if (newQuote === usedQuotes) { console.log("NOPE!"); }
}
I cant understand why it doesnt work for me. I have tried adding .indexOf but then it will only return a number, and it still does never prompt "NOPE" in the console. I know for a fact that it adds the quote towars the "usedQuotes" array but it seems it never does the check. Anyone knows what´s wrong here?
3 Answers
Steven Parker
243,199 PointsIt looks like a type mismatch in the comparisons.
It's a bit hard to tell because only part of the code is here, and it's not formatted, but it looks like when you do your comparisons that newQuote is a single item and usedQuotes is an array of the same kind of item. Those comparisons will always fail because the items are of different types.
You'll probably need a loop if you want to check if an item is present in an array. But it could be easier just to remove the items from the original array as you put them into the used array. That way, every random pick would return a unique item. Then when the original array is exhausted, you can transfer everything back and start over.
In future questions, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. 
Tomas Rinblad
Full Stack JavaScript Techdegree Student 12,427 Points//Get the random quote.
function getRandomQuote () {
    Number = Math.floor(Math.random() * quotes.length);
    var newQuote = quotes[Number].quote;
    if (newQuote !== usedQuotes) {
        usedQuotes.push(newQuote)
       return prop = quotes[Number];
    } else if (newQuote === usedQuotes) {
        console.log("NOPE!");
    }
}
var usedQuotes = [];
var Number;
var quotes = [
    {
        quote : "Try not to become a man of success, but rather try to become a man of value",
        author : "Albert Einstein",
        citation : "",
        year : ""
    },
    {
        quote : "If you´re absent during my struggle, don´t expect to be present during my success",
        author : "William Smith",
        citation : "",
        year : ""
    },
    {
        quote : "Sucess is a journey, not a destination. The doing is often more important than the outcome.",
        author : "Arthur Ashe",
        citation : "",
        year : ""
    },
    {
        quote : "The ones who are crazy enough to think that they can change the world, are the ones who do.",
        author : "Steve Jobs",
        citation : "",
        year : ""
    },
    {
        quote : "I have not FAILED, I´ve just found 10,000 ways that WON´T work",
        author : "Thomas Edison",
        citation : "",
        year : ""
    }
    ];
This is the code i think you need to know :) Ah you thinking of using a slice code? Was thinking about that one too but was not sure how to use it the best way.
Steven Parker
243,199 PointsThat confirms my assumptions, so it's definitely a type mismatch as I was suggesting before.
Tomas Rinblad
Full Stack JavaScript Techdegree Student 12,427 PointsWhere do you see the mismatch? :D
Steven Parker
243,199 PointsThe mismatch is in both if statements, where you compare newQuote (which is a string) with usedQuotes (which is an array).