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 5fcc4b5..a95d643 100644 --- a/problems/countNumbers.js +++ b/problems/countNumbers.js @@ -9,9 +9,38 @@ * returns { 1:3, 2:2, 3:1, 4:1 } */ -function countNumbers() { - +function countNumbers(arr) { + const output = {}; + arr.forEach(el => { + if (output[el]) { + output[el] += 1; + } else { + output[el] = 1; + } + console.log(output) + }) + return output } + + + +// let count = {} +// for (let i = 0; i < arr.length; i++) { +// const el = arr[i]; +// if (count[el]) { +// count[el] += 1 +// } else { +// count[el] = 1; + +// } +// } + +// return count; + +// } + + + module.exports = countNumbers \ No newline at end of file diff --git a/problems/getCountriesSortedByPopulation.js b/problems/getCountriesSortedByPopulation.js index a5a5e58..5fad197 100644 --- a/problems/getCountriesSortedByPopulation.js +++ b/problems/getCountriesSortedByPopulation.js @@ -7,8 +7,23 @@ * */ -function getCountriesSortedByPopulation() { +function getCountriesSortedByPopulation(countries) { + countries.sort((country1, country2) => { + return country2.population - country1.population; + + }) + return countries.map((country) => { + + return country.country; + + + + + }) } + + + module.exports = getCountriesSortedByPopulation diff --git a/problems/isOdd.js b/problems/isOdd.js index 3f7d20d..e4c1da9 100644 --- a/problems/isOdd.js +++ b/problems/isOdd.js @@ -7,8 +7,8 @@ */ -function isOdd() { - +function isOdd(n) { + return n % 2 === 1; } module.exports = isOdd diff --git a/problems/numberOfDigits.js b/problems/numberOfDigits.js index a90ca53..1bef99c 100644 --- a/problems/numberOfDigits.js +++ b/problems/numberOfDigits.js @@ -7,8 +7,11 @@ */ -function numberOfDigits() { - +function numberOfDigits(n) { + let num = n + return num.toString().length } + + module.exports = numberOfDigits diff --git a/problems/removeEvenStrings.js b/problems/removeEvenStrings.js index fc351ae..e1ffb94 100644 --- a/problems/removeEvenStrings.js +++ b/problems/removeEvenStrings.js @@ -6,8 +6,26 @@ * @returns {string[]} - Returns the strings in arr that have an odd number of characters */ -function removeEvenStrings() { - +function removeEvenStrings(arr) { + const output = arr.filter(el => el.length % 2 === 1) + return output; } + + +// let ouptut = []; +// for (let i = 0; i < arr.length; i++) { +// if (arr[i].length % 2 === 1) { +// ouptut.push(arr[i]); +// } +// } + +// return ouptut; + + module.exports = removeEvenStrings + + +//Write down the steps to solve the question +//Use a sample input +//Sample output diff --git a/problems/removeNumbersAtOddIndices.js b/problems/removeNumbersAtOddIndices.js index a87db28..e59cd22 100644 --- a/problems/removeNumbersAtOddIndices.js +++ b/problems/removeNumbersAtOddIndices.js @@ -2,8 +2,21 @@ * @param {number[]} arr - The input array * @returns {number[]} - An array removing all elements initially appearing at an odd index */ -function removeNumbersAtOddIndices() { - +function removeNumbersAtOddIndices(arr) { + output = arr.filter((el, index) => !(index % 2)); + return output; } + + + + + +// for (i = 0; i < arr.length; i += 2) { +// output.push(arr[i]); + +// } + + + module.exports = removeNumbersAtOddIndices \ No newline at end of file diff --git a/problems/removeOddNumbers.js b/problems/removeOddNumbers.js index d7f8ae4..05a13bc 100644 --- a/problems/removeOddNumbers.js +++ b/problems/removeOddNumbers.js @@ -2,8 +2,19 @@ * @param {number[]} arr - The input array * @returns {number[]} - The input array with all odd number removed */ -function removeOddNumbers() { +function removeOddNumbers(arr) { + let output = arr.filter((el, index) => !(el % 2)); + + // let output = []; + // for (let i = 0; i < arr.length; i++) { + // if (arr[i] % 2 === 0) { + // output.push(arr[i]); + // } + // } + return output; } + + module.exports = removeOddNumbers \ No newline at end of file diff --git a/problems/removeVowels.js b/problems/removeVowels.js index eabfd52..dd731f5 100644 --- a/problems/removeVowels.js +++ b/problems/removeVowels.js @@ -6,8 +6,16 @@ * @returns {string} - Returns a new string without any vowels. */ -function removeVowels() { +function removeVowels(str) { + let arr = str.split("") + let out = []; + for (let i = 0; i < arr.length; i++) { + if (arr[i] !== "o" && arr[i] !== "e" && arr[i] !== "u" && arr[i] !== "i" && arr[i] !== "A" && arr[i] !== "E" && arr[i] !== "I" && arr[i] !== "U" && arr[i] !== "O" && arr[i] !== "a") { + out.push(arr[i]) + } + } + return out.join("") } module.exports = removeVowels diff --git a/problems/secondSmallest.js b/problems/secondSmallest.js index 9d1ece6..b40cdff 100644 --- a/problems/secondSmallest.js +++ b/problems/secondSmallest.js @@ -6,8 +6,31 @@ * @returns {number} - Returns the second smallest number. */ -function secondSmallest() { - +function secondSmallest(arr) { + + + // if (arr.length < 2) { + // return null; + // } + // arr.sort((a, b) => a - b) + // return arr[1]; + + + let smallest = Infinity; + let secondSmallest = Infinity; + + for (let i = 0; i < arr.length; i++) { + const num = arr[i] + if (num < smallest) { + secondSmallest = smallest + smallest = num + } else if (num < secondSmallest) { + secondSmallest = num + + } + } + return secondSmallest; + } module.exports = secondSmallest diff --git a/problems/sevenBoom.js b/problems/sevenBoom.js index 14b2a07..20be693 100644 --- a/problems/sevenBoom.js +++ b/problems/sevenBoom.js @@ -4,8 +4,18 @@ * @param {number} n - The number to count up to * @returns {number[]} - An array matching the pattern described above */ -function sevenBoom() { - +function sevenBoom(n) { + let output = []; + for (let i = 1; i <= n; i++) { + if (i % 7 === 0 || i.toString().includes("7")) { + output.push("BOOM") + } else { + output.push(i) + } + } + return output; } + + module.exports = sevenBoom \ No newline at end of file