diff --git a/package-lock.json b/package-lock.json index f383978..d73b101 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "pursuit-core-unit-one-final-assessment", + "name": "module-one-final-assessment", "version": "1.0.0", "lockfileVersion": 1, "requires": true, diff --git a/problems/countNumbers.js b/problems/countNumbers.js index 3a86643..13346bc 100644 --- a/problems/countNumbers.js +++ b/problems/countNumbers.js @@ -1,20 +1,26 @@ -/** +/** * Count all the numbers in an array - * + * * @param {number[]} arr - An array containing numbers * @returns {object} - an object where the key is the number, and the value is the count of that number - * - * ex: countNumbers([1,1,1,2,2,3,4]) + * + * ex: countNumbers([1,1,1,2,2,3,4]) * returns { 1:3, 2:2, 3:1, 4:1 } - * + * * ex: countNumbers([99, 99, 11, 12, 13, 58]) * returns { 99: 2, 11: 1, 12: 1, 13: 1, 58: 1 } */ -function countNumbers() { - +function countNumbers(arr) { + let numObj = {} + + for (let i = 0; i < arr.length; i++) { + let num = arr[i]; + numObj[num] = numObj[num] ? numObj[num] + 1 : 1 + } + return numObj; } -module.exports = countNumbers \ No newline at end of file +module.exports = countNumbers diff --git a/problems/getCountriesSortedByPopulation.js b/problems/getCountriesSortedByPopulation.js index 6e73ba8..dbf0302 100644 --- a/problems/getCountriesSortedByPopulation.js +++ b/problems/getCountriesSortedByPopulation.js @@ -1,17 +1,19 @@ /** * Returns an array of country names sorted in descending order by population (biggest to smallest) -* +* * @param {object[]} arr - The input array. Objects will be in the form: { country: "foo", population: 10 } * @returns {string[]} - Returns an array of country names, sorted by their population in descending order (biggest to smallest) -* +* * ex: getCountriesSortedByPopulation([ { country: "Denmark", population: 6 }, { country: "China", population: 1386 }, { country: "Egypt", population: 145 }]) * returns: ["China", "Egypt", "Denmark"] -* +* */ -function getCountriesSortedByPopulation() { +function getCountriesSortedByPopulation(arr) { + return arr.sort((a, b) => a.population < b.population ? 1 : -1).map(el => el.country) } + module.exports = getCountriesSortedByPopulation diff --git a/problems/isOdd.js b/problems/isOdd.js index e93efee..9224eff 100644 --- a/problems/isOdd.js +++ b/problems/isOdd.js @@ -9,8 +9,12 @@ * ex: isOdd(75); // true */ -function isOdd() { - +function isOdd(n) { + if (n % 2 !== 0 ) { + return true + } else { + return false + } } module.exports = isOdd diff --git a/problems/numberOfDigits.js b/problems/numberOfDigits.js index a90ca53..1661004 100644 --- a/problems/numberOfDigits.js +++ b/problems/numberOfDigits.js @@ -7,8 +7,9 @@ */ -function numberOfDigits() { - +function numberOfDigits(n) { + let digits = n.toString().length + return digits } module.exports = numberOfDigits diff --git a/problems/removeEvenStrings.js b/problems/removeEvenStrings.js index b5e8052..5d06cc7 100644 --- a/problems/removeEvenStrings.js +++ b/problems/removeEvenStrings.js @@ -1,26 +1,33 @@ /** -* Takes in an array of strings and returns a new array that contains only the strings +* Takes in an array of strings and returns a new array that contains only the strings * that have an odd number of characters. -* +* * @param {string[]} arr - The input array of strings * @returns {string[]} - A new array with the strings in arr that have an odd number of characters * * ex: removeEvenStrings(["a", "bb", "ccc", "dddd", "eeeee"]) * returns: ["a", "ccc", "eeeee"] -* +* * ex: removeEvenStrings(["the", "cat", "is", "gray"]) * returns: ["the", "cat"] * * ex: removeEvenStrings(["four"]) * returns: [] -* +* * ex: removeEvenStrings([]) * returns: [] */ -function removeEvenStrings() { +function removeEvenStrings(arr) { + let oddNumChar = []; + for (let i = 0; i < arr.length; i++) { + if (arr[i].length % 2 !== 0 ){ + oddNumChar.push(arr[i]) + } + } + return oddNumChar; } module.exports = removeEvenStrings diff --git a/problems/removeNumbersAtOddIndices.js b/problems/removeNumbersAtOddIndices.js index 94d03a0..e387623 100644 --- a/problems/removeNumbersAtOddIndices.js +++ b/problems/removeNumbersAtOddIndices.js @@ -1,6 +1,6 @@ -/** Takes an array of numbers and returns a new array -* with only the numbers at odd indices in the input array. -* +/** Takes an array of numbers and returns a new array +* with only the numbers at odd indices in the input array. +* * @param {number[]} arr - The input array * @returns {number[]} - An array removing all elements initially appearing at an odd index * @@ -10,8 +10,15 @@ * ex: removeNumbersAtOddIndices([5, 4, 3, 2, 1]); * returns: [5, 3, 1] */ -function removeNumbersAtOddIndices() { +function removeNumbersAtOddIndices(arr) { + let odd = []; + + for (let i = 0; i < arr.length; i+=2) { + odd.push(arr[i]); + + } + return odd; + } -} -module.exports = removeNumbersAtOddIndices \ No newline at end of file +module.exports = removeNumbersAtOddIndices diff --git a/problems/removeOddNumbers.js b/problems/removeOddNumbers.js index cbfe4eb..542acab 100644 --- a/problems/removeOddNumbers.js +++ b/problems/removeOddNumbers.js @@ -1,7 +1,7 @@ /** Takes an array of numbers and returns a new array. * The returned array contains only the even numbers from the input array, * with no odd numbers. -* +* * @param {number[]} arr - The input array * @returns {number[]} - The input array with all odd number removed * @@ -14,8 +14,17 @@ * ex: removeOddNumbers([2, 4, 6]) * returns: [2, 4, 6] */ -function removeOddNumbers() { +function removeOddNumbers(arr) { + let removeOdd = [] + + for(let i = 0; i < arr.length; i++){ + if(arr[i] % 2 ===0){ + removeOdd.push(arr[i]) + } + } + return removeOdd; } -module.exports = removeOddNumbers \ No newline at end of file + +module.exports = removeOddNumbers diff --git a/problems/removeVowels.js b/problems/removeVowels.js index be7edc6..2216f18 100644 --- a/problems/removeVowels.js +++ b/problems/removeVowels.js @@ -1,5 +1,5 @@ /** -* Removes all vowels from an input string. +* Removes all vowels from an input string. * For this problem, treat y as a consonant, not a vowel. * Vowels are "a", "e", "i", "o", and "u" (upper and lowercase) * @@ -14,8 +14,18 @@ * */ -function removeVowels() { +function removeVowels(str) { + let vowelsMoved = ''; + for (let i = 0; i < str.length; i++) { + if (str[i] !== 'a' && str[i] !== 'e' && str[i] !== 'i' && str[i] !== 'o' && str[i] !== 'u' && str[i] !== 'A' && str[i] !== 'E' && str[i] !== 'I' && str[i] !== 'O' && str[i] !== 'U') { + + vowelsMoved += str[i] + + } + } + return vowelsMoved; } + module.exports = removeVowels diff --git a/problems/sevenBoom.js b/problems/sevenBoom.js index a77a8f9..81d2762 100644 --- a/problems/sevenBoom.js +++ b/problems/sevenBoom.js @@ -11,8 +11,32 @@ * 17 is also replaced with 'BOOM' because it contains a 7. */ -function sevenBoom() { +function sevenBoom(n) { + let boomArr = [] + for(let i = 1; i <= n; i+= 1) { + if(i === 7 || i.toString().includes(7)||i % 7 === 0 ){ + boomArr.push("BOOM") + } else { + boomArr.push(i) + } + } + return boomArr + + +// let boomArr = [] + +// for (let i = 0; i < 21; i++) { +// if (i % 7 === 0) { +// return 'BOOM' +// } else if (i % 14 === 0) { +// return 'BOOM' +// } else if (i % 17 === 0) { +// return 'BOOM' +// } else +// return i; +// } } -module.exports = sevenBoom \ No newline at end of file + +module.exports = sevenBoom diff --git a/problems/smallest.js b/problems/smallest.js index 931bf25..7732b57 100644 --- a/problems/smallest.js +++ b/problems/smallest.js @@ -14,8 +14,9 @@ * returns 1 (does not matter if it is the first or second 1) */ -function smallest() { - +function smallest(arr) { + let smallNum = Math.min.apply(Math, arr); + return smallNum; } module.exports = smallest