From 972fad3c47067981257e1059e2ae114ab4c8adef Mon Sep 17 00:00:00 2001 From: Ivan Castillo Date: Tue, 19 Jan 2021 12:52:06 -0500 Subject: [PATCH 1/3] First commit. Wanted to commit the passing minimum of 7/10 before taking a break --- problems/countNumbers.js | 12 ++++++++++-- problems/isOdd.js | 13 +++++++++++-- problems/numberOfDigits.js | 11 +++++++++-- problems/removeEvenStrings.js | 23 +++++++++++++++++++++-- problems/removeNumbersAtOddIndices.js | 16 ++++++++++++++-- problems/removeOddNumbers.js | 12 ++++++++++-- problems/removeVowels.js | 18 +++++++++++++++++- problems/sevenBoom.js | 19 ++++++++++++++++++- 8 files changed, 110 insertions(+), 14 deletions(-) diff --git a/problems/countNumbers.js b/problems/countNumbers.js index 3a86643..c97abc8 100644 --- a/problems/countNumbers.js +++ b/problems/countNumbers.js @@ -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 \ No newline at end of file diff --git a/problems/isOdd.js b/problems/isOdd.js index e93efee..8975a76 100644 --- a/problems/isOdd.js +++ b/problems/isOdd.js @@ -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 diff --git a/problems/numberOfDigits.js b/problems/numberOfDigits.js index a90ca53..c09c65e 100644 --- a/problems/numberOfDigits.js +++ b/problems/numberOfDigits.js @@ -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 diff --git a/problems/removeEvenStrings.js b/problems/removeEvenStrings.js index b5e8052..7b3d0cb 100644 --- a/problems/removeEvenStrings.js +++ b/problems/removeEvenStrings.js @@ -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 diff --git a/problems/removeNumbersAtOddIndices.js b/problems/removeNumbersAtOddIndices.js index 94d03a0..40e6fdd 100644 --- a/problems/removeNumbersAtOddIndices.js +++ b/problems/removeNumbersAtOddIndices.js @@ -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 \ No newline at end of file diff --git a/problems/removeOddNumbers.js b/problems/removeOddNumbers.js index cbfe4eb..71db2b1 100644 --- a/problems/removeOddNumbers.js +++ b/problems/removeOddNumbers.js @@ -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 \ No newline at end of file diff --git a/problems/removeVowels.js b/problems/removeVowels.js index be7edc6..b9b7368 100644 --- a/problems/removeVowels.js +++ b/problems/removeVowels.js @@ -14,8 +14,24 @@ * */ -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 vowels = "aeiou" + "aeiou".toUpperCase() + let newArr = [] + for (let i = 0; i < vowels.length; i++) { + if (str.includes(vowels[i])) { + + } + } + + return newStr } +console.log(removeVowels("Hello")) + module.exports = removeVowels diff --git a/problems/sevenBoom.js b/problems/sevenBoom.js index a77a8f9..3b8cbb9 100644 --- a/problems/sevenBoom.js +++ b/problems/sevenBoom.js @@ -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 \ No newline at end of file From a8c44d50aaf4ce86c87eff80ff440a11e66137b3 Mon Sep 17 00:00:00 2001 From: Ivan Castillo Date: Tue, 19 Jan 2021 16:39:21 -0500 Subject: [PATCH 2/3] Second commit. Still have one problem unsolved --- problems/getCountriesSortedByPopulation.js | 32 ++++++++++++++++++++-- problems/removeVowels.js | 15 +++++----- problems/smallest.js | 12 ++++++-- 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/problems/getCountriesSortedByPopulation.js b/problems/getCountriesSortedByPopulation.js index 6e73ba8..8be41b5 100644 --- a/problems/getCountriesSortedByPopulation.js +++ b/problems/getCountriesSortedByPopulation.js @@ -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 = {} + for (let element in arr) { + newObj[element] = arr[element] + } + 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 diff --git a/problems/removeVowels.js b/problems/removeVowels.js index b9b7368..26d9477 100644 --- a/problems/removeVowels.js +++ b/problems/removeVowels.js @@ -20,18 +20,17 @@ // we then iterate through the newArr // function removeVowels(str) { - let vowels = "aeiou" + "aeiou".toUpperCase() - let newArr = [] + let vowel = "aeiou" + "AEIOU" + let newStr = str + let chars - for (let i = 0; i < vowels.length; i++) { - if (str.includes(vowels[i])) { - - } + for (let i = 0; i < vowel.length; i++) { + chars = newStr.split(vowel[i]) + newStr = chars.join('') } - return newStr } -console.log(removeVowels("Hello")) +console.log(removeVowels("Hello kiddo")) module.exports = removeVowels diff --git a/problems/smallest.js b/problems/smallest.js index 931bf25..819b3b7 100644 --- a/problems/smallest.js +++ b/problems/smallest.js @@ -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 From 8cd5514997ab83bd7a9b54308397526b2aead823 Mon Sep 17 00:00:00 2001 From: Ivan Castillo Date: Fri, 22 Jan 2021 21:49:16 -0500 Subject: [PATCH 3/3] Final commit, passed every test. Learned my way through array method sort, as well as accessing object elements --- problems/getCountriesSortedByPopulation.js | 16 ++++++++++------ problems/removeVowels.js | 13 ++++++++++++- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/problems/getCountriesSortedByPopulation.js b/problems/getCountriesSortedByPopulation.js index 8be41b5..c159b4d 100644 --- a/problems/getCountriesSortedByPopulation.js +++ b/problems/getCountriesSortedByPopulation.js @@ -12,16 +12,20 @@ // 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 sort population number from highest to lowest // will access the property values with arr.country // function getCountriesSortedByPopulation(arr) { - let newObj = {} - for (let element in arr) { - newObj[element] = arr[element] - } - return newObj + arr.sort((a, b) => { + return b.population - a.population + }); + let arrCountries = [] + for (let element of arr) { + arrCountries.push(element["country"]) + } + return arrCountries } + console.log(getCountriesSortedByPopulation([ { diff --git a/problems/removeVowels.js b/problems/removeVowels.js index 26d9477..464e8f1 100644 --- a/problems/removeVowels.js +++ b/problems/removeVowels.js @@ -20,11 +20,12 @@ // we then iterate through the newArr // function removeVowels(str) { - let vowel = "aeiou" + "AEIOU" + let vowel = "aeiou" + "aeiou".toUpperCase() let newStr = str let chars for (let i = 0; i < vowel.length; i++) { + console.log(chars, newStr) chars = newStr.split(vowel[i]) newStr = chars.join('') } @@ -33,4 +34,14 @@ function removeVowels(str) { console.log(removeVowels("Hello kiddo")) +// let yourName = 'Ivan' +// yourName.split === String.prototype.split +// true +// yourName.prototype === String.prototype +// false + +// String.prototype is the entire name of the object. +// It has a bunch of methods, but no properties + + module.exports = removeVowels