diff --git a/problems/countNumbers.js b/problems/countNumbers.js index 3a86643..cdd1efd 100644 --- a/problems/countNumbers.js +++ b/problems/countNumbers.js @@ -11,10 +11,22 @@ * ex: countNumbers([99, 99, 11, 12, 13, 58]) * returns { 99: 2, 11: 1, 12: 1, 13: 1, 58: 1 } */ - -function countNumbers() { - +//create an object that stores the numbers in an array as keys and the amount of times it appaears in the array as values +// iterate through the array and if element has been seen before +1 on the value +// if not see before the value = 1 +function countNumbers(arr) { +let obj = {} +for(let num of arr){ + if(obj[num]){ + obj[num] += 1 + } else if(obj[num] === undefined){ + obj[num] = 1 + } +} +return obj } +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..36701a4 100644 --- a/problems/getCountriesSortedByPopulation.js +++ b/problems/getCountriesSortedByPopulation.js @@ -10,8 +10,15 @@ * */ -function getCountriesSortedByPopulation() { +function getCountriesSortedByPopulation(arr) { +let sort = arr.sort((pop1, pop2)=>{ + return pop2.population - pop1.population +}) +let sorted = sort.map((el)=> { + return el.country +}) +return sorted } - +console.log(getCountriesSortedByPopulation([ { country: "Denmark", population: 6 }, { country: "China", population: 1386 }, { country: "Egypt", population: 145 }])) module.exports = getCountriesSortedByPopulation diff --git a/problems/isOdd.js b/problems/isOdd.js index e93efee..504f951 100644 --- a/problems/isOdd.js +++ b/problems/isOdd.js @@ -9,7 +9,8 @@ * ex: isOdd(75); // true */ -function isOdd() { +function isOdd(n) { + return (n%2===1) ? true : false } diff --git a/problems/numberOfDigits.js b/problems/numberOfDigits.js index a90ca53..3cd5570 100644 --- a/problems/numberOfDigits.js +++ b/problems/numberOfDigits.js @@ -6,9 +6,17 @@ * @returns {number} - Returns how many digits are in the input. */ - -function numberOfDigits() { - +// convert n into something I can iterate over +//toString is easier for me than array +function numberOfDigits(n) { + let count = 0; + let string = n.toString(); + for (let num of string) { + if (num) { + count += 1; + } + } + return count; } -module.exports = numberOfDigits +module.exports = numberOfDigits; diff --git a/problems/removeEvenStrings.js b/problems/removeEvenStrings.js index b5e8052..18679db 100644 --- a/problems/removeEvenStrings.js +++ b/problems/removeEvenStrings.js @@ -18,8 +18,10 @@ * ex: removeEvenStrings([]) * returns: [] */ +//returns new arr either create variable or use .map or .filter -function removeEvenStrings() { +function removeEvenStrings(arr) { + return arr.filter((el)=> el.length%2===1) } diff --git a/problems/removeNumbersAtOddIndices.js b/problems/removeNumbersAtOddIndices.js index 94d03a0..bd84990 100644 --- a/problems/removeNumbersAtOddIndices.js +++ b/problems/removeNumbersAtOddIndices.js @@ -10,7 +10,10 @@ * ex: removeNumbersAtOddIndices([5, 4, 3, 2, 1]); * returns: [5, 3, 1] */ -function removeNumbersAtOddIndices() { +// new array +// +function removeNumbersAtOddIndices(arr) { +return arr.map((el, i, array)=> i % 2 === 1) } diff --git a/problems/removeOddNumbers.js b/problems/removeOddNumbers.js index cbfe4eb..ad47f37 100644 --- a/problems/removeOddNumbers.js +++ b/problems/removeOddNumbers.js @@ -14,7 +14,8 @@ * ex: removeOddNumbers([2, 4, 6]) * returns: [2, 4, 6] */ -function removeOddNumbers() { +function removeOddNumbers(arr) { + return arr.filter((el)=> el % 2 === 0) } diff --git a/problems/removeVowels.js b/problems/removeVowels.js index be7edc6..68cb778 100644 --- a/problems/removeVowels.js +++ b/problems/removeVowels.js @@ -14,8 +14,19 @@ * */ -function removeVowels() { +function removeVowels(str) { +let newStr = "" +let vowels = ['a','e','i','o','u','A', 'E', 'I', 'O', 'U'] +for(let i = 0; i < str.length; i++){ + if(!vowels.includes(str[i])){ + newStr += str[i] +} +} +return newStr } +console.log(removeVowels("hello")) +console.log(removeVowels("HELLO")) + module.exports = removeVowels diff --git a/problems/sevenBoom.js b/problems/sevenBoom.js index a77a8f9..1673818 100644 --- a/problems/sevenBoom.js +++ b/problems/sevenBoom.js @@ -11,8 +11,19 @@ * 17 is also replaced with 'BOOM' because it contains a 7. */ -function sevenBoom() { +function sevenBoom(n) { +let arr = [] +for(let i = 1; i <= n; i++){ +if(i % 7 === 0){ + arr.push('BOOM') +}else if(i %10 === 7){ + arr.push('BOOM') +}else if(i){ + arr.push(i) +} } - +return arr +} +console.log(sevenBoom(271)) module.exports = sevenBoom \ No newline at end of file diff --git a/problems/smallest.js b/problems/smallest.js index 931bf25..f0d14a4 100644 --- a/problems/smallest.js +++ b/problems/smallest.js @@ -14,8 +14,10 @@ * returns 1 (does not matter if it is the first or second 1) */ -function smallest() { +function smallest(arr) { +let sorted = arr.sort((el1, el2) => el1 - el2) +return sorted[0] } - +console.log(smallest([-3,3, 0])) module.exports = smallest