diff --git a/callbacks/src/giveaway/giveaway.js b/callbacks/src/giveaway/giveaway.js index 2c92c19..6729a3a 100644 --- a/callbacks/src/giveaway/giveaway.js +++ b/callbacks/src/giveaway/giveaway.js @@ -16,7 +16,23 @@ */ // YOUR CODE HERE: +function determineCouponRecipients(users, eligibilityCallback) { + const couponRecipients = []; + let count = 0; + for (const user of users) { + if (eligibilityCallback(user)) { + couponRecipients.push(user); + count++; + + if (count === 5) { + break; + } + } + } + + return couponRecipients; +} // This is the callback function that will be passed to your function // This function takes in a single user as an argument // This function will return true only if the user is eligible to receive a coupon diff --git a/callbacks/src/sendCoupons/sendCoupons.js b/callbacks/src/sendCoupons/sendCoupons.js index cece186..9a6cc47 100644 --- a/callbacks/src/sendCoupons/sendCoupons.js +++ b/callbacks/src/sendCoupons/sendCoupons.js @@ -46,6 +46,14 @@ const updateUserCouponSent = (user) => { * - update the user in the database by calling the third callback function * If the user doesn't have a valid phone number, don't do anything for that user */ +function sendAllCoupons(users, checkPhoneNumber, printMessage, updateUser) { + for (const user of users) { + if (checkPhoneNumber(user)) { + printMessage(user); + updateUser(user); + } + } +} module.exports = { canSendCoupon, diff --git a/callbacks/src/userDatabase/userDatabase.js b/callbacks/src/userDatabase/userDatabase.js index bf6ab54..608db0f 100644 --- a/callbacks/src/userDatabase/userDatabase.js +++ b/callbacks/src/userDatabase/userDatabase.js @@ -9,16 +9,15 @@ const UserDatabase = { // SOLUTION: this keyword is not bound to the UserDatabase since this was an arrow function // SOLUTION: change the arrow function into a normal function - addUser: (name, id, phone) => { - this.users.push({ name, id , phone, hasCoupon: false, hasUsedCoupon: false}); + addUser: function(name, id, phone) { + this.users.push({ name, id, phone, hasCoupon: false, isEnrolledInRewards: false }); }, // what is wrong with this function? - function loadFromFile : () => { + loadFromFile: function() { const usersFromFile = require("./usersdb.json"); this.users = usersFromFile; - }, - + }, }; module.exports = UserDatabase;