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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 14 additions & 8 deletions problems/countNumbers.js
Original file line number Diff line number Diff line change
@@ -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
module.exports = countNumbers
10 changes: 6 additions & 4 deletions problems/getCountriesSortedByPopulation.js
Original file line number Diff line number Diff line change
@@ -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
8 changes: 6 additions & 2 deletions problems/isOdd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 3 additions & 2 deletions problems/numberOfDigits.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

*/

function numberOfDigits() {

function numberOfDigits(n) {
let digits = n.toString().length
return digits
}

module.exports = numberOfDigits
17 changes: 12 additions & 5 deletions problems/removeEvenStrings.js
Original file line number Diff line number Diff line change
@@ -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
19 changes: 13 additions & 6 deletions problems/removeNumbersAtOddIndices.js
Original file line number Diff line number Diff line change
@@ -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
*
Expand All @@ -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
module.exports = removeNumbersAtOddIndices
15 changes: 12 additions & 3 deletions problems/removeOddNumbers.js
Original file line number Diff line number Diff line change
@@ -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
*
Expand All @@ -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

module.exports = removeOddNumbers
14 changes: 12 additions & 2 deletions problems/removeVowels.js
Original file line number Diff line number Diff line change
@@ -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)
*
Expand All @@ -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
28 changes: 26 additions & 2 deletions problems/sevenBoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

module.exports = sevenBoom
5 changes: 3 additions & 2 deletions problems/smallest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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