From b2176a3a74f568c5a24d98f8a4e452f8d377ed70 Mon Sep 17 00:00:00 2001 From: Tammam Date: Tue, 14 Feb 2023 11:56:20 -0800 Subject: [PATCH] Pass HOFpractice tests --- data.js | 152 ++++++++++++++++++++++++++-------------------------- practice.js | 105 +++++++++++++++++++++++++++++------- spec.js | 50 ++++++++--------- 3 files changed, 186 insertions(+), 121 deletions(-) diff --git a/data.js b/data.js index 390b8c5..649e44c 100644 --- a/data.js +++ b/data.js @@ -216,117 +216,117 @@ var movies = [ var tweets = [ { - "user": "douglascalhoun", - "message": "that wizard overhauled this worm #techlife", - "createdAt": "2022-09-05T21:39:19.281Z", - "profilePhotoURL": "./assets/img/user-3.png" + 'user': 'douglascalhoun', + 'message': 'that wizard overhauled this worm #techlife', + 'createdAt': '2022-09-05T21:39:19.281Z', + 'profilePhotoURL': './assets/img/user-3.png' }, { - "user": "douglascalhoun", - "message": "completely overhauled this security system #sxsw", - "createdAt": "2022-09-06T02:39:19.281Z", - "profilePhotoURL": "./assets/img/user-3.png" + 'user': 'douglascalhoun', + 'message': 'completely overhauled this security system #sxsw', + 'createdAt': '2022-09-06T02:39:19.281Z', + 'profilePhotoURL': './assets/img/user-3.png' }, { - "user": "mracus", - "message": " formulated that cat ", - "createdAt": "2022-09-06T07:39:19.281Z", - "profilePhotoURL": "./assets/img/user-2.png" + 'user': 'mracus', + 'message': ' formulated that cat ', + 'createdAt': '2022-09-06T07:39:19.281Z', + 'profilePhotoURL': './assets/img/user-2.png' }, { - "user": "douglascalhoun", - "message": "productively deployed my koolaid for real", - "createdAt": "2022-09-06T12:39:19.281Z", - "profilePhotoURL": "./assets/img/user-3.png" + 'user': 'douglascalhoun', + 'message': 'productively deployed my koolaid for real', + 'createdAt': '2022-09-06T12:39:19.281Z', + 'profilePhotoURL': './assets/img/user-3.png' }, { - "user": "mracus", - "message": " delegated your belief system #burningman", - "createdAt": "2022-09-06T17:39:19.281Z", - "profilePhotoURL": "./assets/img/user-2.png" + 'user': 'mracus', + 'message': ' delegated your belief system #burningman', + 'createdAt': '2022-09-06T17:39:19.281Z', + 'profilePhotoURL': './assets/img/user-2.png' }, { - "user": "shawndrost", - "message": "completely enjoyed the way of life ", - "createdAt": "2022-09-06T22:39:19.281Z", - "profilePhotoURL": "./assets/img/user-0.png" + 'user': 'shawndrost', + 'message': 'completely enjoyed the way of life ', + 'createdAt': '2022-09-06T22:39:19.281Z', + 'profilePhotoURL': './assets/img/user-0.png' }, { - "user": "shawndrost", - "message": " aided my worm ", - "createdAt": "2022-09-07T03:39:19.281Z", - "profilePhotoURL": "./assets/img/user-0.png" + 'user': 'shawndrost', + 'message': ' aided my worm ', + 'createdAt': '2022-09-07T03:39:19.281Z', + 'profilePhotoURL': './assets/img/user-0.png' }, { - "user": "sharksforcheap", - "message": " experienced the bad decision but only i know how", - "createdAt": "2022-09-07T08:39:19.281Z", - "profilePhotoURL": "./assets/img/user-1.png" + 'user': 'sharksforcheap', + 'message': ' experienced the bad decision but only i know how', + 'createdAt': '2022-09-07T08:39:19.281Z', + 'profilePhotoURL': './assets/img/user-1.png' }, { - "user": "douglascalhoun", - "message": "efficiently installed an entire koolaid #sf", - "createdAt": "2022-09-07T13:39:19.281Z", - "profilePhotoURL": "./assets/img/user-3.png" + 'user': 'douglascalhoun', + 'message': 'efficiently installed an entire koolaid #sf', + 'createdAt': '2022-09-07T13:39:19.281Z', + 'profilePhotoURL': './assets/img/user-3.png' }, { - "user": "shawndrost", - "message": "the president automated the big system but only i know how", - "createdAt": "2022-09-07T18:39:19.281Z", - "profilePhotoURL": "./assets/img/user-0.png" + 'user': 'shawndrost', + 'message': 'the president automated the big system but only i know how', + 'createdAt': '2022-09-07T18:39:19.281Z', + 'profilePhotoURL': './assets/img/user-0.png' }, { - "user": "douglascalhoun", - "message": "a seedy old man invented my city #yolo", - "createdAt": "2022-09-07T23:39:19.281Z", - "profilePhotoURL": "./assets/img/user-3.png" + 'user': 'douglascalhoun', + 'message': 'a seedy old man invented my city #yolo', + 'createdAt': '2022-09-07T23:39:19.281Z', + 'profilePhotoURL': './assets/img/user-3.png' }, { - "user": "sharksforcheap", - "message": " interfaced the big potato ", - "createdAt": "2022-09-07T23:39:20.431Z", - "profilePhotoURL": "./assets/img/user-1.png" + 'user': 'sharksforcheap', + 'message': ' interfaced the big potato ', + 'createdAt': '2022-09-07T23:39:20.431Z', + 'profilePhotoURL': './assets/img/user-1.png' }, { - "user": "mracus", - "message": "ask me how i systematized the big mind #techlife", - "createdAt": "2022-09-07T23:39:20.977Z", - "profilePhotoURL": "./assets/img/user-2.png" + 'user': 'mracus', + 'message': 'ask me how i systematized the big mind #techlife', + 'createdAt': '2022-09-07T23:39:20.977Z', + 'profilePhotoURL': './assets/img/user-2.png' }, { - "user": "sharksforcheap", - "message": " debugged the big life #sf", - "createdAt": "2022-09-07T23:39:21.416Z", - "profilePhotoURL": "./assets/img/user-1.png" + 'user': 'sharksforcheap', + 'message': ' debugged the big life #sf', + 'createdAt': '2022-09-07T23:39:21.416Z', + 'profilePhotoURL': './assets/img/user-1.png' }, { - "user": "shawndrost", - "message": " downloaded the money #burningman", - "createdAt": "2022-09-07T23:39:22.259Z", - "profilePhotoURL": "./assets/img/user-0.png" + 'user': 'shawndrost', + 'message': ' downloaded the money #burningman', + 'createdAt': '2022-09-07T23:39:22.259Z', + 'profilePhotoURL': './assets/img/user-0.png' }, { - "user": "mracus", - "message": "just navigated your koolaid ", - "createdAt": "2022-09-07T23:39:23.233Z", - "profilePhotoURL": "./assets/img/user-2.png" + 'user': 'mracus', + 'message': 'just navigated your koolaid ', + 'createdAt': '2022-09-07T23:39:23.233Z', + 'profilePhotoURL': './assets/img/user-2.png' }, { - "user": "mracus", - "message": "just enjoyed the big cloud for real", - "createdAt": "2022-09-07T23:39:24.329Z", - "profilePhotoURL": "./assets/img/user-2.png" + 'user': 'mracus', + 'message': 'just enjoyed the big cloud for real', + 'createdAt': '2022-09-07T23:39:24.329Z', + 'profilePhotoURL': './assets/img/user-2.png' }, { - "user": "mracus", - "message": "last night i delegated the worm ", - "createdAt": "2022-09-07T23:39:24.645Z", - "profilePhotoURL": "./assets/img/user-2.png" + 'user': 'mracus', + 'message': 'last night i delegated the worm ', + 'createdAt': '2022-09-07T23:39:24.645Z', + 'profilePhotoURL': './assets/img/user-2.png' }, { - "user": "shawndrost", - "message": " developed the cloud ", - "createdAt": "2022-09-07T23:39:25.694Z", - "profilePhotoURL": "./assets/img/user-0.png" + 'user': 'shawndrost', + 'message': ' developed the cloud ', + 'createdAt': '2022-09-07T23:39:25.694Z', + 'profilePhotoURL': './assets/img/user-0.png' }, ]; diff --git a/practice.js b/practice.js index c493e15..dfbd39e 100644 --- a/practice.js +++ b/practice.js @@ -15,7 +15,7 @@ var moreFruits = function (fruits) { var results = []; - _.each(fruits, function(fruit, index, collection) { + _.each(fruits, function (fruit, index, collection) { results.push(fruit); }); @@ -25,12 +25,24 @@ var moreFruits = function (fruits) { // use _.each to traverse the number array and determine // which are multiples of five. var multiplesOfFive = function (numbers) { - + var counter = 0; + _.each(numbers, function (number) { + if (number % 5 === 0) { + counter++; + } + }); + return counter; }; // use _.each to build an array containing only tweets belonging to a specified user. -var getUserTweets = function(tweets, user) { - +var getUserTweets = function (tweets, user) { + var userTweets = []; + _.each(tweets, function (tweet) { + if (tweet.user === user) { + userTweets.push(tweet); + } + }); + return userTweets; }; /* @@ -41,23 +53,31 @@ var getUserTweets = function(tweets, user) { // use _.filter to return the fruits array with only the desired fruit. var onlyOneFruit = function (fruits, targetFruit) { - + return _.filter(fruits, function(fruit) { + return fruit === targetFruit; + }); }; // use _.filter to return the fruits array with only fruits // starting with the letter 'P'. var startsWith = function (fruits, letter) { - + return _.filter(fruits, function(fruit) { + return fruit[0] === letter; + }); }; // return a filtered array containing only cookie-type desserts. var cookiesOnly = function (desserts) { - + return _.filter(desserts, function(dessert) { + return dessert.type === 'cookie'; + }); }; // rebuild the getUserTweets function from above with _.filter instead -var filterUserTweets = function(tweets, user) { - +var filterUserTweets = function (tweets, user) { + return _.filter(tweets, function(tweet) { + return tweet.user === user; + }); }; /* @@ -69,20 +89,33 @@ var filterUserTweets = function(tweets, user) { // given an array of strings, use _.map to return a new array containing all // strings converted to uppercase letters. var upperCaseFruits = function (fruits) { - + return _.map(fruits, function(fruit) { + return fruit.toUpperCase(); + }); }; // given an array of dessert objects, return a new array of objects // that have a new "glutenFree" property, with a boolean value. // TIP: Items that contain flour are not gluten-free. var glutenFree = function (desserts) { - + return _.map(desserts, function(dessert) { + if (!dessert['ingredients'].includes('flour')) { + dessert['glutenFree'] = true; + return dessert; + + } else { + dessert['glutenFree'] = false; + return dessert; + } + }); }; // given an array of tweet objects, return a new array of strings // containing only the message properties. -var allUserMessages = function(tweets) { - +var allUserMessages = function (tweets) { + return _.map(tweets, function(tweet) { + return tweet.message; + }); }; // use _.map to return an array of items with their sale prices, with a new property @@ -106,7 +139,13 @@ var allUserMessages = function(tweets) { */ var applyCoupon = function (groceries, coupon) { - + return _.map(groceries, function(item) { + var price = Number(item.price.slice(1)); + var salePrice = price - (price * coupon * 100) / 100; + var index = salePrice.toString().indexOf('.'); + item['salePrice'] = '$' + salePrice.toString().slice(0, index + 3); + return item; + }); }; /* @@ -117,13 +156,22 @@ var applyCoupon = function (groceries, coupon) { // return the total price of all products. var sumTotal = function (products) { - + return _.reduce(products, function(memo, product) { + return memo += Number(product.price.slice(1)); + }, 0); }; // return an object consisting of dessert types and how many of each. // exampleOutput: { dessertType: 3, dessertType2: 1 } var dessertCategories = function (desserts) { - + return _.reduce(desserts, function(memo, dessert) { + if (memo[dessert.type] === undefined) { + memo[dessert.type] = 1; + } else { + memo[dessert.type] ++; + } + return memo; + }, {}); }; // return an object with the proper count of all user messages @@ -137,20 +185,37 @@ var dessertCategories = function (desserts) { "sharksforcheap": 3 } */ -var countMessagesPerUser = function(tweets) { - +var countMessagesPerUser = function (tweets) { + return _.reduce(tweets, function(memo, tweet) { + if (memo[tweet.user] === undefined) { + memo[tweet.user] = 1; + } else { + memo[tweet.user]++; + } + return memo; + }, {}); }; // given an array of movie data objects,return an array containing // movies that came out between 1990 and 2000. // TIP: use an array as your accumulator - don't push to an external array! var ninetiesKid = function (movies) { - + return _.reduce(movies, function(memo, movie) { + if (movie.releaseYear >= 1990 && movie.releaseYear <= 2000) { + memo.push(movie.title); + } + return memo; + }, []); }; // return an boolean stating if there exists a movie with a shorter // runtime than your time limit. // timeLimit is an integer representing a number of minutes. var movieNight = function (movies, timeLimit) { - + return _.reduce(movies, function(memo, movie) { + if (movie.runtime < timeLimit) { + memo = true; + } + return memo; + }, false); }; diff --git a/spec.js b/spec.js index c24a5e4..8d4198e 100644 --- a/spec.js +++ b/spec.js @@ -413,25 +413,25 @@ }); it('should return array of only user messages', function () { var onlyMessages = [ - "that wizard overhauled this worm #techlife", - "completely overhauled this security system #sxsw", - " formulated that cat ", - "productively deployed my koolaid for real", - " delegated your belief system #burningman", - "completely enjoyed the way of life ", - " aided my worm ", - " experienced the bad decision but only i know how", - "efficiently installed an entire koolaid #sf", - "the president automated the big system but only i know how", - "a seedy old man invented my city #yolo", - " interfaced the big potato ", - "ask me how i systematized the big mind #techlife", - " debugged the big life #sf", - " downloaded the money #burningman", - "just navigated your koolaid ", - "just enjoyed the big cloud for real", - "last night i delegated the worm ", - " developed the cloud " + 'that wizard overhauled this worm #techlife', + 'completely overhauled this security system #sxsw', + ' formulated that cat ', + 'productively deployed my koolaid for real', + ' delegated your belief system #burningman', + 'completely enjoyed the way of life ', + ' aided my worm ', + ' experienced the bad decision but only i know how', + 'efficiently installed an entire koolaid #sf', + 'the president automated the big system but only i know how', + 'a seedy old man invented my city #yolo', + ' interfaced the big potato ', + 'ask me how i systematized the big mind #techlife', + ' debugged the big life #sf', + ' downloaded the money #burningman', + 'just navigated your koolaid ', + 'just enjoyed the big cloud for real', + 'last night i delegated the worm ', + ' developed the cloud ' ]; expect(allUserMessages(testTweets)).to.eql(onlyMessages); }); @@ -489,7 +489,7 @@ }); it('should return total sum of all prices', function () { var total = sumTotal(testGrocery); - console.log(total); + // console.log(total); expect(total).to.equal(173.98); }); }); @@ -536,16 +536,16 @@ nativeMethods('reduce', false, function () { countMessagesPerUser(testTweets); }); - noForLoops(countMessagesPerUser) + noForLoops(countMessagesPerUser); it('should return an object', function () { expect(countMessagesPerUser(testTweets)).to.be.an('object'); }); it('should return object with correct values', function () { var counts = { - "douglascalhoun": 5, - "mracus": 6, - "shawndrost": 5, - "sharksforcheap": 3 + 'douglascalhoun': 5, + 'mracus': 6, + 'shawndrost': 5, + 'sharksforcheap': 3 }; expect(countMessagesPerUser(testTweets)).to.eql(counts); });