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.

33 changes: 31 additions & 2 deletions problems/countNumbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 16 additions & 1 deletion problems/getCountriesSortedByPopulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions problems/isOdd.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

*/

function isOdd() {

function isOdd(n) {
return n % 2 === 1;
}

module.exports = isOdd
7 changes: 5 additions & 2 deletions problems/numberOfDigits.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

*/

function numberOfDigits() {

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



module.exports = numberOfDigits
22 changes: 20 additions & 2 deletions problems/removeEvenStrings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 15 additions & 2 deletions problems/removeNumbersAtOddIndices.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 12 additions & 1 deletion problems/removeOddNumbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 9 additions & 1 deletion problems/removeVowels.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try using a for of loop when going through strings and not needing the index.

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") {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check out the includes method "aeiouAEIOU".includes(arr[i])

out.push(arr[i])
}

}
return out.join("")
}

module.exports = removeVowels
27 changes: 25 additions & 2 deletions problems/secondSmallest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 12 additions & 2 deletions problems/sevenBoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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