diff --git a/problems/everyOtherLetter.js b/problems/everyOtherLetter.js index e6172e1..0cf5c1a 100644 --- a/problems/everyOtherLetter.js +++ b/problems/everyOtherLetter.js @@ -7,6 +7,14 @@ * */ -function everyOtherLetter() {} +const everyOtherLetter = str => { + let newStr = "" + for (let i = 0; i < str.length; i++) { + if (i % 2 === 0) { + newStr += str[i] + } + } + return newStr +} module.exports = everyOtherLetter; diff --git a/problems/howManyTargets.js b/problems/howManyTargets.js index 5843ec5..e9d0104 100644 --- a/problems/howManyTargets.js +++ b/problems/howManyTargets.js @@ -8,6 +8,15 @@ * */ -function howManyTargets() {} +const howManyTargets = (arr, target) => { + let count = 0; + for (let i = 0; i < arr.length; i++) { + const element = arr[i]; + if (element === target) { + count++ + } + } + return count +} module.exports = howManyTargets; diff --git a/problems/isDivisibleBy9.js b/problems/isDivisibleBy9.js index fd20010..a65b9cf 100644 --- a/problems/isDivisibleBy9.js +++ b/problems/isDivisibleBy9.js @@ -8,6 +8,8 @@ * */ -function isDivisibleBy9() {} +const isDivisibleBy9 = (num) => { + return num % 9 === 0 +} module.exports = isDivisibleBy9; diff --git a/problems/letterCount.js b/problems/letterCount.js index 7684140..c9b38d0 100644 --- a/problems/letterCount.js +++ b/problems/letterCount.js @@ -7,6 +7,19 @@ * */ -function letterCount() {} +const letterCount = str => { + let newArr = str.toLowerCase().split(" "); + let newStr = newArr.join("") + let obj = {} + for (let i = 0; i < newStr.length; i++) { + const element = newStr[i]; + if (obj[element]) { + obj[element]++ + } else { + obj[element] = 1 + } + } + return obj +} module.exports = letterCount; diff --git a/problems/longestWord.js b/problems/longestWord.js index ebd044b..f0122f8 100644 --- a/problems/longestWord.js +++ b/problems/longestWord.js @@ -8,6 +8,16 @@ * returns "hello" */ -function longestWord() {} +const longestWord = words => { + let str = "" + for (let i = 0; i < words.length; i++) { + const element = words[i]; + if (element.length > str.length) { + str = element + } + } + return str +} + module.exports = longestWord; diff --git a/problems/numberIncludes0.js b/problems/numberIncludes0.js index 2d7f12c..57a19e0 100644 --- a/problems/numberIncludes0.js +++ b/problems/numberIncludes0.js @@ -8,6 +8,15 @@ * */ -function numberIncludes0() {} +const numberIncludes0 = num => { + let numStr = num.toString() + for (let i = 0; i < numStr.length; i++) { + const element = numStr[i]; + if (element === "0") { + return true + } + } + return false +} module.exports = numberIncludes0; diff --git a/problems/onlyStringsGreaterThanOrEqualTo5.js b/problems/onlyStringsGreaterThanOrEqualTo5.js index 3d82b99..77d6639 100644 --- a/problems/onlyStringsGreaterThanOrEqualTo5.js +++ b/problems/onlyStringsGreaterThanOrEqualTo5.js @@ -7,6 +7,6 @@ * returns ["hello", "corey"] */ -function onlyStringsGreaterThanOrEqualTo5() {} +const onlyStringsGreaterThanOrEqualTo5 = words => words.filter(el => el.length >= 5) module.exports = onlyStringsGreaterThanOrEqualTo5; diff --git a/problems/productOfOddNumbers.js b/problems/productOfOddNumbers.js index 629f4ed..29e1160 100644 --- a/problems/productOfOddNumbers.js +++ b/problems/productOfOddNumbers.js @@ -7,6 +7,20 @@ * returns 15 */ - function productOfOddNumbers() {} + const productOfOddNumbers = (nums) => { + let num = 1 + for (let i = 0; i < nums.length; i++) { + if (nums[i] % 2 === 1) { + num *= nums[i] + } + } + if (num === 1) { + return 0 + } + return num + } - module.exports = productOfOddNumbers \ No newline at end of file + + + module.exports = productOfOddNumbers + \ No newline at end of file diff --git a/problems/sortPeopleByAge.js b/problems/sortPeopleByAge.js index 31125c7..e728ee3 100644 --- a/problems/sortPeopleByAge.js +++ b/problems/sortPeopleByAge.js @@ -17,6 +17,10 @@ ] */ -function sortPeopleByAge() {} +const sortPeopleByAge = (people) => { + return people.sort((a,b) => { + return a.age - b.age + }) +} module.exports = sortPeopleByAge; diff --git a/problems/sumOfValuesOfObject.js b/problems/sumOfValuesOfObject.js index 234b6d5..accbeaf 100644 --- a/problems/sumOfValuesOfObject.js +++ b/problems/sumOfValuesOfObject.js @@ -8,6 +8,13 @@ */ -function sumOfValuesOfObject(){} +const sumOfValuesOfObject = (obj) => { + let sum = 0 + let arr = Object.values(obj) + for (let i = 0; i < arr.length; i++) { + sum += arr[i] + } + return sum +} module.exports = sumOfValuesOfObject; \ No newline at end of file diff --git a/problems/thirdLargest.js b/problems/thirdLargest.js index 3c8ed17..9a49ee1 100644 --- a/problems/thirdLargest.js +++ b/problems/thirdLargest.js @@ -7,6 +7,34 @@ * returns 8 */ -function thirdLargest() {} +const thirdLargest = (nums) => { + let largest = -Infinity + let secondLargest = -Infinity + let thirdLargest = -Infinity + + for (let i = 0; i < nums.length; i++) { + const element = nums[i]; + if (element > largest) { + largest = element + } + } + + for (let i = 0; i < nums.length; i++) { + const element = nums[i] + if (element < largest && element > secondLargest) { + secondLargest = element + } + } + + for (let i = 0; i < nums.length; i++) { + const element = nums[i] + if (element < largest && element < secondLargest && element > thirdLargest) { + thirdLargest = element + } else if (nums.length < 3) { + return null + } + } + return thirdLargest +} module.exports = thirdLargest \ No newline at end of file