diff --git a/data/jokes.json b/data/jokes.json index 731e707..b876384 100644 --- a/data/jokes.json +++ b/data/jokes.json @@ -6125,7 +6125,7 @@ { "body": "Who is Santa's favourite singer?\nElf-is Presley!", "category": "Wordplay" - } + }, { "body": "A wife asks her husband, \"Honey, if I died, would you remarry?\"\"After a considerable period of grieving, I guess I would.. We all need companionship.\"\"If I died and you remarried,\" the wife asks, \"would she live in this house?\"\"We've spent a lot of money getting this house just the way we want it. I'm not going to get rid of my house. I guess she would.\"\"If I died and you remarried, and she lived in this house,\" the wife asks, \"would she sleep in our bed?\"\"Well, the bed is brand new, and it cost us $2,000. It's going to last a long time, so I guess she would.\"\"If I died and you remarried, and she lived in this house and slept in our bed, would she use my golf clubs?\"\"Oh, no,\" the husband replies. \"She's left-handed.\"", "category": "Men" diff --git a/data/quotes.json b/data/quotes.json index f2faf09..69ed4f1 100644 --- a/data/quotes.json +++ b/data/quotes.json @@ -5221,7 +5221,7 @@ }, { "quote": "Pain shared, my brother, is pain not doubled but halved. No man is an island", - "author": "Neil Gaiman, Anansi Boys" + "author": "Neil Gaiman, Anansi Boys"},{ "quote": "Your work is going to fill a large part of your life,and the only way to be truly satisfied is to do what you believe is great work.And the only way to do great work is to love what you do. ", "author": "Steve Jobs" }, @@ -5280,9 +5280,9 @@ { "quote": "If your hate could be turned into electricity, it would light up the whole world.", "author": "Nikola Tesla" - } + }, { "quote": "Have the courage to follow your heart and intuition. They somehow already know what you truly want to become.", "author": "Steve Jobs" - }, + } ] diff --git a/index.js b/index.js index b43664d..c8b4191 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,47 @@ -const quotes = require("./data/quotes.json"); -const jokes = require("./data/jokes.json"); -const riddles = require("./data/riddles.json"); +const quotes = removeDuplicateQuotes(require("./data/quotes.json")); +const jokes = removeDuplicateJokes(require("./data/jokes.json")); +const riddles = removeDuplicateRiddles(require("./data/riddles.json")); + +// This function removes all duplicate riddles +function removeDuplicateRiddles(inputArray) { + outputArray = inputArray.filter( + (arrayElement, index, self) => + index === + self.findIndex( + (element) => + element.riddle === arrayElement.riddle && + element.answer === arrayElement.answer + ) + ); + return outputArray; +} + +// This function removes all duplicate Jokes +function removeDuplicateJokes(inputArray) { + outputArray = inputArray.filter( + (arrayElement, index, self) => + index === + self.findIndex( + (element) => + element.body === arrayElement.body && + element.category === arrayElement.category + ) + ); + return outputArray; +} +// This function removes all duplicate Quotes +function removeDuplicateQuotes(inputArray) { + outputArray = inputArray.filter( + (arrayElement, index, self) => + index === + self.findIndex( + (element) => + element.quote === arrayElement.quote && + element.author === arrayElement.author + ) + ); + return outputArray; +} function getRandomItem(items) { return items[Math.floor(Math.random() * items.length)]; @@ -8,7 +49,7 @@ function getRandomItem(items) { function getRandomQuote() { return getRandomItem(quotes); -}; +} function getRandomJoke() { return getRandomItem(jokes); @@ -21,5 +62,5 @@ function getRandomRiddle() { module.exports = { getRandomQuote, getRandomJoke, - getRandomRiddle + getRandomRiddle, };