diff --git a/practiceQuestions.js b/practiceQuestions.js new file mode 100644 index 0000000..421b86d --- /dev/null +++ b/practiceQuestions.js @@ -0,0 +1,150 @@ +/** + * Returns a new string with every other letter. + * @param {string} str - a string + * @returns {str} - + * + * ex: everyOddCharacter("mississippi") //=> "issip" + * + */ + +function everyOddCharacter(str) {} + + + +/** + * Returns the characters from str that are found in targets array + * @param {string} str - string input + * @param {string[]} target - targets to find in string + * @returns {string} - amount of times the target was included + * + * ex: retieveTargets("hello world", ["h", "l", "o"] ) //=> "hllool" + * + */ + +function retieveTargets(str, targets) {} + + + +/** + * Returns true if every number is an odd number . + * @param {arr} nums - an array of numbers + * @returns {boolean} - is every number is an odd number + * + * ex: isOddNumbers([27, 17, 21, 51]) //=> true + * ex: isOddNumbers([1, 37, 5, 2]) //=> false + * + */ + +function isOddNumbers(nums) {} + + +/** + * Return an array of letters that match the frequency value of that letter + * @param {Object} obj- the characters and their frequency + * @returns {Object} - + * + * ex: frequencyPrint({h: 1, e: 1, l: 2, o: 1}) //=> "hello" + * + */ + +function frequencyPrint(obj) {} + + +/** + * Returns the longest word in the object. + * Ties should go to the earlier word + * @param {object} petNames - an object containing words + * @returns {string} - longest word + * + * ex: longestPetName({cat:"Tommy", bird:"Sunny", dog:"Sunshine", rat:"tiki"}) + * returns "Sunshine" + */ + +function longestPetName(petNames) {} + + +/** + * Return a number without zeros. + * @param {number} num - a number + * @returns {number || null} - a number without zeros + * + * ex: remove0(1023) //=> 123 + * ex: remove0(0000000) //=> null + * + */ + +function remove0(num) {} + + +/** + * Returns an array of words that includes a target character. + * @param {string[]} words - an array containing words + * @param {string} letter - a letter target + * @returns {string[]} - words that include target letter + * + * ex: onlyStringsThatContainTheTargetLetter(["cat", "hello", "corey", "dog", "hiccups"], "c") + * returns ["cat", "corey", "hiccups"] + */ + +function onlyStringsThatContainTheTargetLetter(words, target) {} + + +/** + * Return the product of all even numbers in an array. + * @param {number[]} nums - an array containing numbers + * @returns {number} - product of all the odd numbers + * + * ex: productOfOddNumbers([1, 2, 3, 4, 5, 6]) + * returns 48 + */ + +function productOfEvenNumbers(nums) {} + +/** + * Takes in an object and returns the sum of all values + * @param {Object} obj - an object with key value pairs + * @returns {number} - sum of all values + * + * ex: sumOfValuesOfObject({corey: 5, sam: 10, peter: 3, sparky: 9, key: 10}) + * returns 27 + */ + +function sumOfValuesOfObject(obj) {} + + +/** + * Return the third smallest number. Cannot use sort. + * @param {number[]} nums - an array containing numbers + * @returns {number} - third smallest number + * + * ex: thirdSmallest([12, 3, 8, 2, 1, 14]) + * returns 3 + */ + +function thirdLargest(nums) {} + + +/** + * Takes in an array of cars and sorts them by year from newest to oldest + * @param {Object[]} people - an array containing car objects + * @returns {Object[]} - a sorted array + * + * ex: sortCarByYear([ + { car: "BMW", year: 2001 }, + { car: "Tesla", year: 2021 }, + { car: "Toyota", year: 1995 }, + { car: "Ford", year: 2020 }, +]) + + /** returns [ + { car: "Tesla", year: 2021 }, + { car: "Ford", year: 2020 }, + { car: "BMW", year: 2005 }, + { car: "Toyota", year: 1995 }, +] + */ + +function sortCarByYear(peoples) {} + + + diff --git a/problems/everyOtherLetter.js b/problems/everyOtherLetter.js index e6172e1..b8bfc93 100644 --- a/problems/everyOtherLetter.js +++ b/problems/everyOtherLetter.js @@ -7,6 +7,13 @@ * */ -function everyOtherLetter() {} +function everyOtherLetter(str) { + let newStr = "" + for(i = 0; i < str.length; i+=2) { + newStr += str[i] + } + return newStr + } + module.exports = everyOtherLetter; diff --git a/problems/howManyTargets.js b/problems/howManyTargets.js index 5843ec5..972f166 100644 --- a/problems/howManyTargets.js +++ b/problems/howManyTargets.js @@ -8,6 +8,14 @@ * */ -function howManyTargets() {} +function howManyTargets(arr, target) { + let count = 0 + arr.forEach((el) => { + if(el === target) { + count++ + } + }) + return count +} module.exports = howManyTargets; diff --git a/problems/isDivisibleBy9.js b/problems/isDivisibleBy9.js index fd20010..4130a40 100644 --- a/problems/isDivisibleBy9.js +++ b/problems/isDivisibleBy9.js @@ -8,6 +8,11 @@ * */ -function isDivisibleBy9() {} +function isDivisibleBy9(num) { + if(num % 9 === 0) { + return true + } + return false +} module.exports = isDivisibleBy9; diff --git a/problems/letterCount.js b/problems/letterCount.js index 7684140..1d0ddde 100644 --- a/problems/letterCount.js +++ b/problems/letterCount.js @@ -7,6 +7,21 @@ * */ -function letterCount() {} +function letterCount(str) { + let arr = str.split(" ") + let str1 = arr.join("") + let obj = {} + for(i = 0; i < str1.length; i++) { + let el = str1[i].toLowerCase() + if(obj[el]) { + + obj[el] +=1 + + } else { + obj[el] = 1 + } + } + return obj +} module.exports = letterCount; diff --git a/problems/longestWord.js b/problems/longestWord.js index ebd044b..3253a6c 100644 --- a/problems/longestWord.js +++ b/problems/longestWord.js @@ -8,6 +8,17 @@ * returns "hello" */ -function longestWord() {} +function longestWord(words) { + let longest = words[0] + for(i = 0; i < words.length; i++) { + let el = words[i] + if(el.length > longest.length) { + longest = el + } else if(el.length === longest.length) { + longest + } + } + return longest +} module.exports = longestWord; diff --git a/problems/numberIncludes0.js b/problems/numberIncludes0.js index 2d7f12c..3e92012 100644 --- a/problems/numberIncludes0.js +++ b/problems/numberIncludes0.js @@ -8,6 +8,16 @@ * */ -function numberIncludes0() {} +function numberIncludes0(num) { + let newStr = '' + num + let newArr = newStr.split("") + for(i = 0; i < newArr.length; i++) { + if(newArr[i] === "0") { + return true + } else { + } +} +return false +} module.exports = numberIncludes0; diff --git a/problems/onlyStringsGreaterThanOrEqualTo5.js b/problems/onlyStringsGreaterThanOrEqualTo5.js index 3d82b99..c615e08 100644 --- a/problems/onlyStringsGreaterThanOrEqualTo5.js +++ b/problems/onlyStringsGreaterThanOrEqualTo5.js @@ -7,6 +7,10 @@ * returns ["hello", "corey"] */ -function onlyStringsGreaterThanOrEqualTo5() {} +function onlyStringsGreaterThanOrEqualTo5(words) { + return words.filter((word) => { + return word.length >= 5 + }) +} module.exports = onlyStringsGreaterThanOrEqualTo5; diff --git a/problems/productOfOddNumbers.js b/problems/productOfOddNumbers.js index 629f4ed..e649209 100644 --- a/problems/productOfOddNumbers.js +++ b/problems/productOfOddNumbers.js @@ -1,12 +1,24 @@ /** - * Return the product of all odd numbers in an array. + * Return the product of all odd numbers in an array. * @param {number[]} nums - an array containing numbers * @returns {number} - product of all the odd numbers - * + * * ex: productOfOddNumbers([1, 2, 3, 4, 5]) * returns 15 */ - function productOfOddNumbers() {} +function productOfOddNumbers(nums) { + let product = 1; + + let numOfOdds = nums.filter((num) => num % 2===1 ) + if(numOfOdds.length === 0) { + return 0 + } else { + numOfOdds.forEach((num) =>{ + product *= num + }) + } + return product +} - module.exports = productOfOddNumbers \ No newline at end of file +module.exports = productOfOddNumbers; diff --git a/problems/sortPeopleByAge.js b/problems/sortPeopleByAge.js index 31125c7..d4fb357 100644 --- a/problems/sortPeopleByAge.js +++ b/problems/sortPeopleByAge.js @@ -17,6 +17,10 @@ ] */ -function sortPeopleByAge() {} +function 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..8422a73 100644 --- a/problems/sumOfValuesOfObject.js +++ b/problems/sumOfValuesOfObject.js @@ -8,6 +8,13 @@ */ -function sumOfValuesOfObject(){} +function sumOfValuesOfObject(obj){ + let sum = 0 + let objValues = Object.values(obj) + objValues.forEach((el) => { + sum += el + }) + return sum +} module.exports = sumOfValuesOfObject; \ No newline at end of file diff --git a/problems/thirdLargest.js b/problems/thirdLargest.js index 3c8ed17..8f49411 100644 --- a/problems/thirdLargest.js +++ b/problems/thirdLargest.js @@ -7,6 +7,26 @@ * returns 8 */ -function thirdLargest() {} +function thirdLargest(nums) { + let thirdLarge = -Infinity + let largest = -Infinity + let secondLargest = -Infinity + for(let i = 0; i < nums.length; i++) { + let el = nums[i] + if(nums.length < 3) { + return null + } else if(el > largest) { + thirdLarge = secondLargest + secondLargest = largest + largest = el + } else if(el > secondLargest) { + thirdLarge = secondLargest + secondLargest = el + } else if(el > thirdLarge) { + thirdLarge = el + } + } + return thirdLarge +} module.exports = thirdLargest \ No newline at end of file