Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions problems/countNumbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 9 additions & 2 deletions problems/getCountriesSortedByPopulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion problems/isOdd.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
* ex: isOdd(75); // true
*/

function isOdd() {
function isOdd(n) {
return (n%2===1) ? true : false

}

Expand Down
16 changes: 12 additions & 4 deletions problems/numberOfDigits.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
4 changes: 3 additions & 1 deletion problems/removeEvenStrings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}

Expand Down
5 changes: 4 additions & 1 deletion problems/removeNumbersAtOddIndices.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}

Expand Down
3 changes: 2 additions & 1 deletion problems/removeOddNumbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}

Expand Down
13 changes: 12 additions & 1 deletion problems/removeVowels.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 13 additions & 2 deletions problems/sevenBoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 4 additions & 2 deletions problems/smallest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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