Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
12 changes: 10 additions & 2 deletions problems/countNumbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
32 changes: 30 additions & 2 deletions problems/getCountriesSortedByPopulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,36 @@
*
*/

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 lowest to highest
// will access the property values with arr.country
//
function getCountriesSortedByPopulation(arr) {
let newObj = {}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This function should return an array, not an object.

for (let element in arr) {
newObj[element] = arr[element]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider what 'element' is, which information you need from the element, and which array method allows you to add data into the array you are going to return.

}
return newObj
}

console.log(getCountriesSortedByPopulation([
{
country: "Algeria",
population: 41,
},
{
country: "Belize",
population: 0.4,
},
{
country: "China",
population: 1386,
},
{
country: "Denmark",
population: 6,
},
]))

module.exports = getCountriesSortedByPopulation
13 changes: 11 additions & 2 deletions problems/isOdd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 9 additions & 2 deletions problems/numberOfDigits.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 21 additions & 2 deletions problems/removeEvenStrings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 14 additions & 2 deletions problems/removeNumbersAtOddIndices.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 10 additions & 2 deletions problems/removeOddNumbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 16 additions & 1 deletion problems/removeVowels.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,23 @@
*
*/

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"
let newStr = str
let chars

for (let i = 0; i < vowel.length; i++) {
chars = newStr.split(vowel[i])
newStr = chars.join('')
}
return newStr
}

console.log(removeVowels("Hello kiddo"))

module.exports = removeVowels
19 changes: 18 additions & 1 deletion problems/sevenBoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 10 additions & 2 deletions problems/smallest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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