From 62f294218f33c2306a126e67a4af54ae40cc611a Mon Sep 17 00:00:00 2001 From: Iqra Javed Date: Sat, 17 Feb 2018 19:02:51 -0600 Subject: [PATCH 1/2] completed project-1 --- .DS_Store | Bin 0 -> 6148 bytes package-lock.json | 4189 +++++++++++++++++++++++++++++++++++++++++++++ src/project-1.js | 25 + 3 files changed, 4214 insertions(+) create mode 100644 .DS_Store create mode 100644 package-lock.json diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..94f6cc278e9f47d30b6eb92e4d4cf02d03cb5827 GIT binary patch literal 6148 zcmeHK&1xGl5FW)&vUYOV&=dl_=vwf>Ax@m*)=iHEgNsdRg0q`lu)ypZuN)JMVb6Vx z{v>^jLTTTjkJ4LbBqby`IR!#6Gt1038fhf-fix=s5UokP3*ZBQhe}wkqPa(?pL9W1 zT!c{29Qi03W)ZgjMWcn$9AE~Rf#=2mo!t`TFov^edi(qiRj%vxzhb3SUR_)FD%I*H ze*V_xNoSBvJ37>pycLc=nU;!^$ocJ153cFs0Y2V*|o25EbX)?Z*dfZcK zoK0KVB#raqj-9WkVm;{lFWckf98=q2nsimJA~jU{qEhkj>-#&iS)*}KlY8~Uc}>om z&3a8X_L}p#|I_ncz21NG{^Mx=`ODXD-!HFz{Q7PA7kFJ6dExLH{-Co88WN;LL(Y@{ z5G5?j$bdTp-o`R+j`ze2FawWbfbIv0N*FjSESjqW4gEe)f0>X3eR@j}N{4~N!Xoyd z2%Cy%Q-ynC2%C;^>Ba>P3yU@#gjpHqaVrb=LJ?+lj7u{dL|~CEGr$boWuRo6KGpws z55NEKCUJ`yU { // return num after multiplying it by ten // code here + return (num * 10); }; const subtractFive = (num) => { // return num after subtracting five // code here + return (num - 5); }; const areSameLength = (str1, str2) => { // return true if the two strings have the same length // otherwise return false // code here + return (str1.length === str2.length); }; const areEqual = (x, y) => { // return true if x and y are the same // otherwise return false // code here + return (x === y); }; const lessThanNinety = (num) => { // return true if num is less than ninety // otherwise return false // code here + return (num < 90); }; const greaterThanFifty = (num) => { // return true if num is greater than fifty // otherwise return false // code here + return (num > 50); }; const add = (x, y) => { // add x and y together and return the value // code here + return (x + y); }; const subtract = (x, y) => { // subtract y from x and return the value // code here + return (x - y); }; const divide = (x, y) => { // divide x by y and return the value // code here + return (x / y); }; const multiply = (x, y) => { // multiply x by y and return the value // code here + return (x * y); }; const getRemainder = (x, y) => { // return the remainder from dividing x by y // code here + return (x % y); }; const isEven = (num) => { // return true if num is even // otherwise return false // code here + return (num % 2 === 0); }; const isOdd = (num) => { // return true if num is odd // otherwise return false // code here + return (num % 2 !== 0); }; const square = (num) => { // square num and return the new value // code here + return (num * num); }; const cube = (num) => { // cube num and return the new value // code here + return (num * num * num); }; const raiseToPower = (num, exponent) => { // raise num to whatever power is passed in as exponent // code here + return (Math.pow(num, exponent)); }; const roundNumber = (num) => { // round num and return it // code here + return Math.round(num); }; const roundUp = (num) => { // round num up and return it // code here + return Math.ceil(num); }; const addExclamationPoint = (str) => { // add an exclamation point to the end of str and return the new string // 'hello world' -> 'hello world!' // code here + return (`${str}!`); }; const combineNames = (firstName, lastName) => { // return firstName and lastName combined as one string and separated by a space. // 'Lambda', 'School' -> 'Lambda School' // code here + return (`${firstName} ${lastName}`); }; const getGreeting = (name) => { // Take the name string and concatenate other strings onto it so it takes the following form: // 'Sam' -> 'Hello Sam!' // code here + return (`Hello ${name}!`); }; // If you can't remember these area formulas then head over to Google or look at the test code. @@ -119,21 +140,25 @@ const getGreeting = (name) => { const getRectangleArea = (length, width) => { // return the area of the rectangle by using length and width // code here + return length * width; }; const getTriangleArea = (base, height) => { // return the area of the triangle by using base and height // code here + return (1 / 2) * (base * height); }; const getCircleArea = (radius) => { // return the rounded area of the circle given the radius // code here + return Math.round(Math.PI * radius * radius); }; const getRectangularPrismVolume = (length, width, height) => { // return the volume of the 3D rectangular prism given the length, width, and height // code here + return (length * width * height); }; // Do not modify code below this line. From cd32fd8561ef4806735146575044041db8d7770d Mon Sep 17 00:00:00 2001 From: Iqra Javed Date: Mon, 19 Feb 2018 22:45:37 -0600 Subject: [PATCH 2/2] completed project 2 and 3 --- .DS_Store | Bin 6148 -> 6148 bytes src/project-2.js | 90 ++++++++++++++++++++++++++++++++++++++++++++++- src/project-3.js | 36 +++++++++++++++++++ src/project-4.js | 16 +++++++++ 4 files changed, 141 insertions(+), 1 deletion(-) diff --git a/.DS_Store b/.DS_Store index 94f6cc278e9f47d30b6eb92e4d4cf02d03cb5827..a37083471dca526cc7fa0ce2bfe0b6ab24450e0f 100644 GIT binary patch delta 23 ecmZoMXffDuk%i4zN5RnCWb!Lk_sw#w&x8P6?+2y; delta 16 XcmZoMXffDuk!A7|R^QEXtj~l1I!gwH diff --git a/src/project-2.js b/src/project-2.js index 5fe0047..c5cfd6f 100644 --- a/src/project-2.js +++ b/src/project-2.js @@ -3,6 +3,10 @@ const getBiggest = (x, y) => { // x and y are integers. Return the larger integer // if they are the same return either one + if (x >= y) { + return x; + } + return y; }; const greeting = (language) => { @@ -11,15 +15,28 @@ const greeting = (language) => { // language: 'Spanish' -> 'Hola!' // language: 'Chinese' -> 'Ni Hao!' // if language is undefined return 'Hello!' + if (language === 'German') { + return 'Guten Tag!'; + } else if (language === 'Spanish') { + return 'Hola!'; + } else if (language === 'Chinese') { + return 'Ni Hao!'; + } return 'Hello!'; }; const isTenOrFive = (num) => { // return true if num is 10 or 5 // otherwise return false + if (num === 10 || num === 5) { + return true; + } return false; }; const isInRange = (num) => { // return true if num is less than 50 and greater than 20 + if (num < 50 && num > 20) { + return true; + } return false; }; const isInteger = (num) => { @@ -29,6 +46,9 @@ const isInteger = (num) => { // -10 -> true // otherwise return false // hint: you can solve this using Math.floor + if (Math.floor(num) === num) { + return true; + } return false; }; const fizzBuzz = (num) => { @@ -36,43 +56,76 @@ const fizzBuzz = (num) => { // if num is divisible by 5 return 'buzz' // if num is divisible by 3 & 5 return 'fizzbuzz' // otherwise return num + if (num % 3 === 0 && num % 5 === 0) { + return 'fizzbuzz'; + } else if (num % 3 === 0) { + return 'fizz'; + } else if (num % 5 === 0) { + return 'buzz'; + } return num; }; const isPrime = (num) => { // return true if num is prime. // otherwise return false - // hint: a prime number is only evenly divisible by itself and 1 + // hint: a prime number is only evenly divisible by itself and 1 num % num === 0 && num % 1 === 0 0,1, === prime // hint2: you can solve this using a for loop // note: 0 and 1 are NOT considered prime numbers + if (num === 0 || num === 1) { + return false; + } + if (num === 2) { + return true; + } + for (let i = 2; i < num; i++) { + if (num % i === 0) { + return false; + } + } + return true; }; const returnFirst = (arr) => { // return the first item from the array + return arr[0]; }; const returnLast = (arr) => { // return the last item of the array + return arr[arr.length - 1]; }; const getArrayLength = (arr) => { // return the length of the array + return arr.length; }; const incrementByOne = (arr) => { // arr is an array of integers // increase each integer by one // return the array + const newArr = []; + let newVal = 0; + for (let i = 0; i < arr.length; i++) { + newVal = arr[i] + 1; + newArr.push(newVal); + } + return newArr; }; const addItemToArray = (arr, item) => { // add the item to the end of the array // return the array + arr.push(item); + return arr; }; const addItemToFront = (arr, item) => { // add the item to the front of the array // return the array // hint: use the array method .unshift + arr.unshift(item); + return arr; }; const wordsToSentence = (words) => { @@ -80,26 +133,61 @@ const wordsToSentence = (words) => { // return a string that is all of the words concatenated together // spaces need to be between each word // example: ['Hello', 'world!'] -> 'Hello world!' + // Chris's solution return words.join(' '); + let newWords = ''; + for (let i = 0; i < words.length; i++) { + newWords += (`${words[i]} `); + } return newWords.trim(); }; const contains = (arr, item) => { // check to see if item is inside of arr // return true if it is, otherwise return false + // Chris's solution return arr.includes(item); + for (let i = 0; i < arr.length; i++) { + if (arr[i] === item) { + return true; + } + } + return false; }; const addNumbers = (numbers) => { // numbers is an array of integers. // add all of the integers and return the value + /* Chris's solution: return numbers.reduce ((acc, cur) => { + return acc + cur; + }); +*/ + let finalNumber = 0; + for (let i = 0; i < numbers.length; i++) { + finalNumber += numbers[i]; + } + return finalNumber; }; const averageTestScore = (testScores) => { // testScores is an array. Iterate over testScores and compute the average. // return the average + let sumOfScores = 0; + let averageOfScores = 0; + for (let i = 0; i < testScores.length; i++) { + sumOfScores += testScores[i]; + averageOfScores = sumOfScores / (testScores.length); + } + return averageOfScores; }; const largestNumber = (numbers) => { // numbers is an array of integers // return the largest integer + let largeNumber = 0; + for (let i = 0; i < numbers.length; i++) { + if (numbers[i] > largeNumber) { + largeNumber = numbers[i]; + } + } + return largeNumber; }; // Do not modify code below this line. diff --git a/src/project-3.js b/src/project-3.js index 7ca1430..c191287 100644 --- a/src/project-3.js +++ b/src/project-3.js @@ -5,61 +5,83 @@ const makeCat = (name, age) => { // add an age property to the object with the value set to the age argument // add a method called meow that returns the string 'Meow!' // return the object + const newObj = { name, age, meow() { return 'Meow!'; } }; + return newObj; }; const addProperty = (object, property) => { // add the property to the object with a value of null // return the object // note: the property name is NOT 'property'. The name is the value of the argument called property (a string) + object[property] = null; + return object; }; const invokeMethod = (object, method) => { // method is a string that contains the name of a method on the object // invoke this method // nothing needs to be returned + object[method](); }; const multiplyMysteryNumberByFive = (mysteryNumberObject) => { // mysteryNumberObject has a property called mysteryNumber // multiply the mysteryNumber property by 5 and return the product + return mysteryNumberObject.mysteryNumber * 5; }; const deleteProperty = (object, property) => { // remove the property from the object // return the object + delete object[property]; + return object; }; const newUser = (name, email, password) => { // create a new object with properties matching the arguments passed in. // return the new object + const newObj = { name, email, password }; + return newObj; }; const hasEmail = (user) => { // return true if the user has a value for the property 'email' // otherwise return false + if (user.email) return true; + return false; }; const hasProperty = (object, property) => { // return true if the object has the value of the property argument // property is a string // otherwise return false + if (object[property]) return true; + return false; }; const verifyPassword = (user, password) => { // check to see if the provided password matches the password property on the user object // return true if they match // otherwise return false + if (user.password === password) { + return true; + } + return false; }; const updatePassword = (user, newPassword) => { // replace the existing password on the user object with the value of newPassword // return the object + user.password = newPassword; + return user; }; const addFriend = (user, newFriend) => { // user has a property called friends that is an array // add newFriend to the end of the friends array // return the user object + user.friends.push(newFriend); + return user; }; const setUsersToPremium = (users) => { @@ -67,6 +89,10 @@ const setUsersToPremium = (users) => { // each user object has the property 'isPremium' // set each user's isPremium property to true // return the users array + for (let i = 0; i < users.length; i++) { + users[i].isPremium = true; + } + return users; }; const sumUserPostLikes = (user) => { @@ -75,6 +101,11 @@ const sumUserPostLikes = (user) => { // each post object has an integer property called 'likes' // sum together the likes from all the post objects // return the sum + let sum = 0; + for (let i = 0; i < user.posts.length; i++) { + sum += user.posts[i].likes; + } + return sum; }; const addCalculateDiscountPriceMethod = (storeItem) => { @@ -87,8 +118,13 @@ const addCalculateDiscountPriceMethod = (storeItem) => { // discountPrice = 20 - (20 * .2) // Make sure you return storeItem after adding the method to it // hint: arrow functions don't bind a this + storeItem.calculateDiscountPrice = function () { + return storeItem.price - (storeItem.price * storeItem.discountPercentage); + }; + return storeItem; }; + // Do not modify code below this line. // -------------------------------- diff --git a/src/project-4.js b/src/project-4.js index 3a3a186..362bd59 100644 --- a/src/project-4.js +++ b/src/project-4.js @@ -1,31 +1,47 @@ const getFirstItem = (collection, cb) => { // invoke the callback function and pass the first item from the collection in as an argument + cb(collection[0]); }; const getLength = (collection, cb) => { // Write a function called getLength that passes the length of the array into the callback + cb(collection.length); }; const getLastItem = (collection, cb) => { // Write a function called getLastItem which passes the getLastItem item of the array into the callback + cb(collection[collection.length - 1]); }; const sumNums = (x, y, cb) => { // Write a function called sumNums that adds two numbers and passes the result to the callback + const sum = x + y; + cb(sum); }; const multiplyNums = (x, y, cb) => { // Write a function called multiplyNums that multiplies two numbers and passes the result to the callback + const product = x * y; + cb(x * y); }; const contains = (collection, item, cb) => { // Write a function called contains that checks if an item is present inside of the given array. // Pass true to the callback if it is, otherwise pass false + if (collection.includes(item)) { + cb(true); + return; + } + cb(false); }; const removeDuplicates = (collection, cb) => { // Write a function called removeDuplicates that removes all duplicate values from the given array. // Pass the array to the callback function. Do not mutate the original array. + const hasDuplicate = (item, index) => { + return !(collection.slice(index + 1).includes(item)); + }; + cb(collection.filter(hasDuplicate)); }; module.exports = {