From d17dff8c8ee26dd14a797a703eeff445172f0626 Mon Sep 17 00:00:00 2001 From: analogy Date: Wed, 6 Sep 2017 20:59:49 -0400 Subject: [PATCH 1/4] Changed code to ES6 --- src/project-1.js | 67 ++++++++++++++++++++++++++++++++++++++-- src/project-2.js | 79 ++++++++++++++++++++++++++++++++++++++++++++++++ src/project-3.js | 55 +++++++++++++++++++++++++++++++++ src/project-4.js | 8 +++++ 4 files changed, 207 insertions(+), 2 deletions(-) diff --git a/src/project-1.js b/src/project-1.js index dc26cfb..7d1efbc 100644 --- a/src/project-1.js +++ b/src/project-1.js @@ -3,137 +3,200 @@ const multiplyByTen = (num) => { // return num after multiplying it by ten // code here + const product = num * 10; + return product; }; const subtractFive = (num) => { // return num after subtracting five // code here + const difference = num - 5; + return difference; }; const areSameLength = (str1, str2) => { - // return true if the two strings have the same length + // return true if the two strings have the same length // otherwise return false // code here + if (str1.length === str2.length) { + return true; + } + return false; }; const areEqual = (x, y) => { // return true if x and y are the same // otherwise return false // code here + if (x === y) { + return true; + } + return false; }; const lessThanNinety = (num) => { // return true if num is less than ninety // otherwise return false // code here + if (num < 90) { + return true; + } + return false; }; const greaterThanFifty = (num) => { // return true if num is greater than fifty // otherwise return false // code here + if (num > 50) { + return true; + } + return false; }; const add = (x, y) => { // add x and y together and return the value // code here + const sum = x + y; + return sum; }; const subtract = (x, y) => { // subtract y from x and return the value // code here + const difference = x - y; + return difference; }; const divide = (x, y) => { // divide x by y and return the value // code here + const quotient = x / y; + return quotient; }; const multiply = (x, y) => { // multiply x by y and return the value // code here + const product = x * y; + return product; }; const getRemainder = (x, y) => { // return the remainder from dividing x by y // code here + const remainder = x % y; + return remainder; }; const isEven = (num) => { // return true if num is even // otherwise return false // code here + const even = num % 2; + if (even === 0) { + return true; + } + return false; }; const isOdd = (num) => { // return true if num is odd // otherwise return false // code here + const odd = num % 2; + if (odd !== 0) { + return true; + } + return false; }; const square = (num) => { // square num and return the new value // code here + const secondPower = Math.pow(num, 2); + return secondPower; }; const cube = (num) => { // cube num and return the new value // code here + const thirdPower = Math.pow(num, 3); + return thirdPower; }; const raiseToPower = (num, exponent) => { // raise num to whatever power is passed in as exponent // code here + const anyPower = Math.pow(num, exponent); + return anyPower; }; const roundNumber = (num) => { // round num and return it // code here + const round = Math.round(num); + return round; }; const roundUp = (num) => { // round num up and return it // code here + const roundHigher = Math.ceil(num); + return roundHigher; }; const addExclamationPoint = (str) => { // add an exclamation point to the end of str and return the new string // 'hello world' -> 'hello world!' // code here + const newString = `${str}!`; + return newString; }; const combineNames = (firstName, lastName) => { // return firstName and lastName combined as one string and separated by a space. // 'Lambda', 'School' -> 'Lambda School' // code here + const combine = `${firstName} ${lastName}`; + return combine; }; const getGreeting = (name) => { // Take the name string and concatenate other strings onto it so it takes the following form: // 'Sam' -> 'Hello Sam!' // code here + const greet = `Hello ${name}!`; + return greet; }; // If you can't remember these area formulas then head over to Google or look at the test code. - const getRectangleArea = (length, width) => { // return the area of the rectangle by using length and width // code here + const rArea = length * width; + return rArea; }; const getTriangleArea = (base, height) => { // return the area of the triangle by using base and height // code here + const tArea = 0.5 * base * height; + return tArea; }; const getCircleArea = (radius) => { // return the rounded area of the circle given the radius // code here + const cArea = Math.round(Math.PI * radius * radius); + return cArea; }; const getRectangularPrismVolume = (length, width, height) => { // return the volume of the 3D rectangular prism given the length, width, and height // code here + const pVolume = length * width * height; + return pVolume; }; // Do not modify code below this line. diff --git a/src/project-2.js b/src/project-2.js index 5fe0047..3ba78c7 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,35 @@ 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 === 'English') { + return 'Hello!'; + } 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 === 5) { + return true; + } else if (num === 10) { + return true; + } + return false; }; const isInRange = (num) => { // return true if num is less than 50 and greater than 20 + if (num > 20 && num < 50) { + return true; + } + return false; }; const isInteger = (num) => { @@ -29,6 +53,10 @@ const isInteger = (num) => { // -10 -> true // otherwise return false // hint: you can solve this using Math.floor + if (Number.isInteger(num)) { + return true; + } + return false; }; const fizzBuzz = (num) => { @@ -36,6 +64,14 @@ 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) => { @@ -44,35 +80,53 @@ const isPrime = (num) => { // hint: a prime number is only evenly divisible by itself and 1 // hint2: you can solve this using a for loop // note: 0 and 1 are NOT considered prime numbers + if (num < 2) return false; + 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 + for (let i = 0; i < arr.length; i++) { + arr[i] += 1; + } + return arr; }; 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.splice(0, 0, item); + return arr; }; const wordsToSentence = (words) => { @@ -80,26 +134,51 @@ 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!' + return words.join(' '); }; const contains = (arr, item) => { // check to see if item is inside of arr // return true if it is, otherwise return false + 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 + let sum = 0; + for (let i = 0; i < numbers.length; i++) { + sum += numbers[i]; + } + return sum; }; const averageTestScore = (testScores) => { // testScores is an array. Iterate over testScores and compute the average. // return the average + let sum = 0; + for (let i = 0; i < testScores.length; i++) { + sum += testScores[i]; + } + const averageScore = sum / testScores.length; + return averageScore; }; const largestNumber = (numbers) => { // numbers is an array of integers // return the largest integer + let largest = 0; + for (let i = 0; i < numbers.length; i++) { + if (numbers[i] > largest) { + largest = numbers[i]; + } + } + return largest; }; // Do not modify code below this line. diff --git a/src/project-3.js b/src/project-3.js index 7ca1430..e210cd7 100644 --- a/src/project-3.js +++ b/src/project-3.js @@ -5,61 +5,99 @@ 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 cat = { + name, + age, + meow() { + return 'Meow!'; + } + }; + return cat; }; 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 + let num = mysteryNumberObject.mysteryNumber; + num *= 5; + return num; }; 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 myObject = { + name, + email, + password + }; + return myObject; }; 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 (password === user.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 +105,10 @@ const setUsersToPremium = (users) => { // each user object has the property 'isPremium' // set each user's isPremium property to true // return the users array + users.forEach((user) => { + users.isPremium = true; + }); + return users; }; const sumUserPostLikes = (user) => { @@ -75,6 +117,12 @@ 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; + const myObject = user.posts; + myObject.forEach((post) => { + sum += post.likes; + }); + return sum; }; const addCalculateDiscountPriceMethod = (storeItem) => { @@ -87,6 +135,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 + const myobject = storeItem; + myobject.calculateDiscountPrice = () => { + const discount = storeItem.price * storeItem.discountPercentage; + const discountPrice = storeItem.price - discount; + return discountPrice; + }; + return storeItem; }; // Do not modify code below this line. diff --git a/src/project-4.js b/src/project-4.js index 3a3a186..ca0209f 100644 --- a/src/project-4.js +++ b/src/project-4.js @@ -1,9 +1,13 @@ const getFirstItem = (collection, cb) => { // invoke the callback function and pass the first item from the collection in as an argument + const length = collection.length[0]; + cb(length); }; const getLength = (collection, cb) => { // Write a function called getLength that passes the length of the array into the callback + const length = collection.length; + cb(length); }; const getLastItem = (collection, cb) => { @@ -12,10 +16,14 @@ const getLastItem = (collection, cb) => { 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 sum = x * y; + cb(sum); }; const contains = (collection, item, cb) => { From 664fdeac1e2e69f8529bf762baea66cf418cf7ba Mon Sep 17 00:00:00 2001 From: analogy Date: Wed, 6 Sep 2017 21:37:11 -0400 Subject: [PATCH 2/4] Changed code to ES6 --- src/project-4.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/project-4.js b/src/project-4.js index ca0209f..9ed5376 100644 --- a/src/project-4.js +++ b/src/project-4.js @@ -1,7 +1,7 @@ const getFirstItem = (collection, cb) => { // invoke the callback function and pass the first item from the collection in as an argument - const length = collection.length[0]; - cb(length); + const firstItem = collection[0]; + cb(firstItem); }; const getLength = (collection, cb) => { @@ -12,8 +12,10 @@ const getLength = (collection, cb) => { const getLastItem = (collection, cb) => { // Write a function called getLastItem which passes the getLastItem item of the array into the callback + const lastItem = 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; @@ -34,6 +36,9 @@ const contains = (collection, item, cb) => { 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. + // uses set data structure and from function (see notes) + const uniqueArray = Array.from(new Set(collection)); + cb(uniqueArray); }; module.exports = { From 3eae95ca37bef7cd50ed0839f7336f111203138d Mon Sep 17 00:00:00 2001 From: analogy Date: Thu, 7 Sep 2017 14:49:47 -0400 Subject: [PATCH 3/4] Changed code to ES6 Final Version --- src/project-3.js | 2 +- src/project-4.js | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/project-3.js b/src/project-3.js index e210cd7..dcec636 100644 --- a/src/project-3.js +++ b/src/project-3.js @@ -106,7 +106,7 @@ const setUsersToPremium = (users) => { // set each user's isPremium property to true // return the users array users.forEach((user) => { - users.isPremium = true; + user.isPremium = true; }); return users; }; diff --git a/src/project-4.js b/src/project-4.js index 9ed5376..e3474d8 100644 --- a/src/project-4.js +++ b/src/project-4.js @@ -13,6 +13,7 @@ const getLength = (collection, cb) => { const getLastItem = (collection, cb) => { // Write a function called getLastItem which passes the getLastItem item of the array into the callback const lastItem = collection[collection.length - 1]; + cb(lastItem); }; @@ -31,6 +32,14 @@ const multiplyNums = (x, y, cb) => { 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 + for (let i = 0; i < collection.length; i++) { + if (collection[i] !== item) { + cb(false); + } else if (collection[i] === item) { + cb(true); + // cb(true); + } + } }; const removeDuplicates = (collection, cb) => { From df76c0dcca3d80a683c9ad0ae46eb07d16d7b796 Mon Sep 17 00:00:00 2001 From: analogy Date: Thu, 7 Sep 2017 15:02:02 -0400 Subject: [PATCH 4/4] Finally, Final Version Changed code to ES6 (removed my comments) --- src/project-4.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/project-4.js b/src/project-4.js index e3474d8..34db9a9 100644 --- a/src/project-4.js +++ b/src/project-4.js @@ -37,7 +37,6 @@ const contains = (collection, item, cb) => { cb(false); } else if (collection[i] === item) { cb(true); - // cb(true); } } };