diff --git a/problems/countNumbers.js b/problems/countNumbers.js index 3a86643..c97abc8 100644 --- a/problems/countNumbers.js +++ b/problems/countNumbers.js @@ -12,9 +12,17 @@ * returns { 99: 2, 11: 1, 12: 1, 13: 1, 58: 1 } */ -function countNumbers() { - +function countNumbers(arr) { + newObj = {} + for (let element of arr) { + newObj[element] = 0 + } + for (element of arr) { + newObj[element]++ + } + return newObj } +console.log(countNumbers([1, 2, 3, 4, 5, 6])) module.exports = countNumbers \ No newline at end of file diff --git a/problems/getCountriesSortedByPopulation.js b/problems/getCountriesSortedByPopulation.js index 6e73ba8..c159b4d 100644 --- a/problems/getCountriesSortedByPopulation.js +++ b/problems/getCountriesSortedByPopulation.js @@ -10,8 +10,40 @@ * */ -function getCountriesSortedByPopulation() { - +// takes in an array with objects holding a country as key and it's population number as value +// need to return country names with lowest to highest population +// will sort population number from highest to lowest +// will access the property values with arr.country +// +function getCountriesSortedByPopulation(arr) { + arr.sort((a, b) => { + return b.population - a.population + }); + let arrCountries = [] + for (let element of arr) { + arrCountries.push(element["country"]) + } + return arrCountries } + + +console.log(getCountriesSortedByPopulation([ + { + country: "Algeria", + population: 41, + }, + { + country: "Belize", + population: 0.4, + }, + { + country: "China", + population: 1386, + }, + { + country: "Denmark", + population: 6, + }, + ])) module.exports = getCountriesSortedByPopulation diff --git a/problems/isOdd.js b/problems/isOdd.js index e93efee..8975a76 100644 --- a/problems/isOdd.js +++ b/problems/isOdd.js @@ -9,8 +9,17 @@ * ex: isOdd(75); // true */ -function isOdd() { - +function isOdd(n) { + if (n % 2 === 1) { + return true + } + else { + return false + } } +console.log(isOdd(3)) +console.log(isOdd(2)) + + module.exports = isOdd diff --git a/problems/numberOfDigits.js b/problems/numberOfDigits.js index a90ca53..c09c65e 100644 --- a/problems/numberOfDigits.js +++ b/problems/numberOfDigits.js @@ -7,8 +7,15 @@ */ -function numberOfDigits() { - +function numberOfDigits(n) { + let digits = 0 + let str = n.toString() + for (let element of str) { + digits++ + } + return digits } +console.log(numberOfDigits(2784)) + module.exports = numberOfDigits diff --git a/problems/removeEvenStrings.js b/problems/removeEvenStrings.js index b5e8052..7b3d0cb 100644 --- a/problems/removeEvenStrings.js +++ b/problems/removeEvenStrings.js @@ -19,8 +19,27 @@ * returns: [] */ -function removeEvenStrings() { - +function removeEvenStrings(arr) { + let newArr = [] + for (let element of arr) { + if (element.length % 2 === 1) { + newArr.push(element) + } + } + return newArr } +console.log(removeEvenStrings([ + "The", + "only", + "thing", + "we", + "have", + "to", + "fear", + "is", + "fear", + "itself", + ])) + module.exports = removeEvenStrings diff --git a/problems/removeNumbersAtOddIndices.js b/problems/removeNumbersAtOddIndices.js index 94d03a0..40e6fdd 100644 --- a/problems/removeNumbersAtOddIndices.js +++ b/problems/removeNumbersAtOddIndices.js @@ -10,8 +10,20 @@ * ex: removeNumbersAtOddIndices([5, 4, 3, 2, 1]); * returns: [5, 3, 1] */ -function removeNumbersAtOddIndices() { - +function removeNumbersAtOddIndices(arr) { + let i = 0 + let newArr = [] + while (i < arr.length) { + if (i % 2 === 0) { + newArr.push(arr[i]) + } + i++ + } + return newArr } +console.log(removeNumbersAtOddIndices([1, 2, 3, 4, 5, 6])) +console.log(removeNumbersAtOddIndices([1, 4, 7, 10])) + + module.exports = removeNumbersAtOddIndices \ No newline at end of file diff --git a/problems/removeOddNumbers.js b/problems/removeOddNumbers.js index cbfe4eb..71db2b1 100644 --- a/problems/removeOddNumbers.js +++ b/problems/removeOddNumbers.js @@ -14,8 +14,16 @@ * ex: removeOddNumbers([2, 4, 6]) * returns: [2, 4, 6] */ -function removeOddNumbers() { - +function removeOddNumbers(arr) { + let newArr = [] + for (let i of arr) { + if (i % 2 === 0) { + newArr.push(i) + } + } + return newArr } +console.log(removeOddNumbers([1, 2, 3, 4, 5, 6, 7])) + module.exports = removeOddNumbers \ No newline at end of file diff --git a/problems/removeVowels.js b/problems/removeVowels.js index be7edc6..464e8f1 100644 --- a/problems/removeVowels.js +++ b/problems/removeVowels.js @@ -14,8 +14,34 @@ * */ -function removeVowels() { +// will take in a string +// will return a new string without the vowels +// we do this by splitting the string into an array with str.split('') +// we then iterate through the newArr +// +function removeVowels(str) { + let vowel = "aeiou" + "aeiou".toUpperCase() + let newStr = str + let chars + for (let i = 0; i < vowel.length; i++) { + console.log(chars, newStr) + chars = newStr.split(vowel[i]) + newStr = chars.join('') + } + return newStr } +console.log(removeVowels("Hello kiddo")) + +// let yourName = 'Ivan' +// yourName.split === String.prototype.split +// true +// yourName.prototype === String.prototype +// false + +// String.prototype is the entire name of the object. +// It has a bunch of methods, but no properties + + module.exports = removeVowels diff --git a/problems/sevenBoom.js b/problems/sevenBoom.js index a77a8f9..3b8cbb9 100644 --- a/problems/sevenBoom.js +++ b/problems/sevenBoom.js @@ -11,8 +11,25 @@ * 17 is also replaced with 'BOOM' because it contains a 7. */ -function sevenBoom() { +function sevenBoom(n) { + let newArr = [] + + for (let i = 1; i <= n; i++) { + if (i % 7 === 0) { + newArr.push("BOOM") + } + else if (i.toString().includes(7)) { + newArr.push("BOOM") + } + else { + newArr.push(i) + } + } + + return newArr } +console.log(sevenBoom(59)) + module.exports = sevenBoom \ No newline at end of file diff --git a/problems/smallest.js b/problems/smallest.js index 931bf25..819b3b7 100644 --- a/problems/smallest.js +++ b/problems/smallest.js @@ -14,8 +14,16 @@ * returns 1 (does not matter if it is the first or second 1) */ -function smallest() { - +function smallest(arr) { + let smallestNum = Infinity + for (let element of arr) { + if (smallestNum > element) { + smallestNum = element + } + } + return smallestNum } +console.log(smallest([-99, 99, 57, 3, 6, 32])) + module.exports = smallest