From d49fa0e558520430800a88a6b189e83e3b8a4128 Mon Sep 17 00:00:00 2001 From: George Roth Date: Fri, 16 Sep 2022 16:32:05 +0100 Subject: [PATCH 1/8] Adding easy tasks --- src/easy/digitCount/plain.md | 2 ++ src/easy/digitCount/pseudo.md | 2 ++ src/easy/digitCount/solution.js | 9 +++++++++ src/easy/makeSentence/plain.md | 4 ++++ src/easy/makeSentence/pseudo.md | 11 +++++++++++ src/easy/makeSentence/solution.js | 31 +++++++++++++++++++++++++++++++ src/easy/random/plain.md | 8 ++++++++ src/easy/random/pseudo.md | 8 ++++++++ src/easy/random/solution.js | 22 ++++++++++++++++++++++ src/easy/reverse/pseudo.md | 8 ++++++++ src/easy/reverse/solution.js | 18 ++++++++++++++++++ 11 files changed, 123 insertions(+) diff --git a/src/easy/digitCount/plain.md b/src/easy/digitCount/plain.md index 73370f1..f683ffc 100644 --- a/src/easy/digitCount/plain.md +++ b/src/easy/digitCount/plain.md @@ -1 +1,3 @@ # Plain English Solution + +I'd just use the .length method and hope it works on numbers. If it doesn't, I would split it into a list and then check the length. \ No newline at end of file diff --git a/src/easy/digitCount/pseudo.md b/src/easy/digitCount/pseudo.md index 520841f..96444c8 100644 --- a/src/easy/digitCount/pseudo.md +++ b/src/easy/digitCount/pseudo.md @@ -1 +1,3 @@ # Pseudo Code Solution + +log the length of numbers into the console \ No newline at end of file diff --git a/src/easy/digitCount/solution.js b/src/easy/digitCount/solution.js index e69de29..7b880a5 100644 --- a/src/easy/digitCount/solution.js +++ b/src/easy/digitCount/solution.js @@ -0,0 +1,9 @@ +// log the length of numbers into the console + +numbers = 12334 + +// console.log(numbers.length) – doesn't work! Time to convert it to a list + +// console.log(numbers.split('').length) - numbers can't be converted into a list this way either. String then? + +console.log(String(numbers).length) \ No newline at end of file diff --git a/src/easy/makeSentence/plain.md b/src/easy/makeSentence/plain.md index 73370f1..47821d0 100644 --- a/src/easy/makeSentence/plain.md +++ b/src/easy/makeSentence/plain.md @@ -1 +1,5 @@ # Plain English Solution + +Convert the string to a list. Get the first character of the sentence with the index [0]. Add the first character into a new variable, convert it into uppercase (if it's already uppercase, still convert it), and then popping it back with concatenation or unshift. Then check if the last character is punctuation, and if it is, leave the string alone, but if not, add a dot. + +If shifting a string doesn't work, I'll convert it to a list with .split and then reassemble it afterwards with a for loop. \ No newline at end of file diff --git a/src/easy/makeSentence/pseudo.md b/src/easy/makeSentence/pseudo.md index 520841f..6c024bc 100644 --- a/src/easy/makeSentence/pseudo.md +++ b/src/easy/makeSentence/pseudo.md @@ -1 +1,12 @@ # Pseudo Code Solution + +Target first character with [0] + +Put the shifted character into the upperCaseStart variable + +Make the upperCaseStart variable upper case + +Pop back upperCaseStart to the string with unshift or concatenation + +If the last character is not punctuation + – then add a dot \ No newline at end of file diff --git a/src/easy/makeSentence/solution.js b/src/easy/makeSentence/solution.js index e69de29..b0b27b2 100644 --- a/src/easy/makeSentence/solution.js +++ b/src/easy/makeSentence/solution.js @@ -0,0 +1,31 @@ +// Console log shifting a string, hopefully it works +let sentence = "this sentence is to be capitalized and a dot should be added to the end" + +let sentenceToList = sentence.split('') + +// Put the character into the upperCaseStart variable – update: don't even need to do that, can just uppercase directly + +let upperCaseStart = sentenceToList[0].toUpperCase() + +// Make the upperCaseStart variable upper case – done above + +// Pop back upperCaseStart to the string with unshift or concatenation + +sentenceToList.shift() + +sentenceToList.unshift(upperCaseStart) + +// If the last character is not punctuation +// – then add a dot + +if (sentenceToList[sentenceToList.length-1] !== '.' && sentenceToList[sentenceToList.length-1] !== '!' && sentenceToList[sentenceToList.length-1] !== '?' ) { + sentenceToList.push('.') +} + +let capStartFullStopSentence = '' + +for (let i of sentenceToList) { + capStartFullStopSentence += i +} + +console.log(capStartFullStopSentence) \ No newline at end of file diff --git a/src/easy/random/plain.md b/src/easy/random/plain.md index 73370f1..6b433d5 100644 --- a/src/easy/random/plain.md +++ b/src/easy/random/plain.md @@ -1 +1,9 @@ # Plain English Solution + +Start a min and max variable that our generator will generate between + +Use date.now() to generate a random number + +Loop through date.now() from the back (important since the back will be different all the time, the front will be the same) and if a number is found that is between min and max, then return that and break from the loop. + +This won't be able to generate negative numbers and numbers that are smaller than one. \ No newline at end of file diff --git a/src/easy/random/pseudo.md b/src/easy/random/pseudo.md index 520841f..f2b3d79 100644 --- a/src/easy/random/pseudo.md +++ b/src/easy/random/pseudo.md @@ -1 +1,9 @@ # Pseudo Code Solution + +Start a min and max variable that our generator will generate between + +Use date.now() to generate a random number + +Loop through date.now() from the back (important since the back will be different all the time, the front will be the same) and if a number is found that is between min and max, then return that and break from the loop. + +This won't be able to generate negative numbers and numbers that are smaller than one. \ No newline at end of file diff --git a/src/easy/random/solution.js b/src/easy/random/solution.js index e69de29..13c9dd1 100644 --- a/src/easy/random/solution.js +++ b/src/easy/random/solution.js @@ -0,0 +1,22 @@ +// Start a min and max variable that our generator will generate between + +function randomGenerator(min, max) { + + // Use date.now() to generate a random number + + const randomList = String(Date.now()).split('') + + // Loop through date.now() from the back (important since the back will be different all the time, the front will be the same) and if a number is found that is between min and max, then return that and break from the loop. + + + for (let i = randomList.length; i >= 0; i--) { + if (randomList[i] > min && randomList[i] < max) { + return randomList[i] + } + } +} + +console.log(randomGenerator(0, 9)) + +// This won't be able to generate negative numbers and numbers that are smaller than one. + diff --git a/src/easy/reverse/pseudo.md b/src/easy/reverse/pseudo.md index 520841f..85a9c5c 100644 --- a/src/easy/reverse/pseudo.md +++ b/src/easy/reverse/pseudo.md @@ -1 +1,9 @@ # Pseudo Code Solution + +Define empty newString + +Loop through the elements of a string backwards + +Add the characters to the empty newString + +Return newString \ No newline at end of file diff --git a/src/easy/reverse/solution.js b/src/easy/reverse/solution.js index e69de29..71b62cf 100644 --- a/src/easy/reverse/solution.js +++ b/src/easy/reverse/solution.js @@ -0,0 +1,18 @@ + + +function reverse(stringToReverse) { + + // Define empty newString + let reversedString = '' + + // Loop through the elements of a string backwards + for (let i = stringToReverse.length - 1; i >= 0; i--) { + // Add the characters to the empty newString + reversedString += stringToReverse[i] + } + + // Return newString + return reversedString +} + +console.log(reverse('123')) \ No newline at end of file From 32bcbcf1e992280c843d0e178896764116339f89 Mon Sep 17 00:00:00 2001 From: George Roth Date: Fri, 16 Sep 2022 16:33:10 +0100 Subject: [PATCH 2/8] Adding plain English solution to reverse --- src/easy/reverse/plain.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/easy/reverse/plain.md b/src/easy/reverse/plain.md index 73370f1..47c9c13 100644 --- a/src/easy/reverse/plain.md +++ b/src/easy/reverse/plain.md @@ -1 +1,3 @@ # Plain English Solution + +Loop through the elements of the string backwards, and add the current character to a new string. \ No newline at end of file From ca5e5d1267e8ec62d275ef0654fa0839e229b1a9 Mon Sep 17 00:00:00 2001 From: George Roth Date: Fri, 16 Sep 2022 16:55:13 +0100 Subject: [PATCH 3/8] Adding morseCode exercise --- src/medium/morseCode/pseudo.md | 7 +++ src/medium/morseCode/solution.js | 77 ++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/src/medium/morseCode/pseudo.md b/src/medium/morseCode/pseudo.md index 520841f..159fd34 100644 --- a/src/medium/morseCode/pseudo.md +++ b/src/medium/morseCode/pseudo.md @@ -1 +1,8 @@ # Pseudo Code Solution + +Create a function that converts letters to morse code with a million if statements + +Define a new empty string + +Loop through the elements of the input string + – use the function to add the correct morse code to a new string \ No newline at end of file diff --git a/src/medium/morseCode/solution.js b/src/medium/morseCode/solution.js index e69de29..11cd72a 100644 --- a/src/medium/morseCode/solution.js +++ b/src/medium/morseCode/solution.js @@ -0,0 +1,77 @@ +function morseConverter(englishSentence) { + let morseSentence ='' // Define a new empty string + for (i of englishSentence.toLowerCase()) { // Loop through the elements of the input string + morseSentence += morseCharacterConverter(i) // – use the function to add the correct morse code to a new string + } + return morseSentence +} + +// Create a function that converts letters to morse code with a millio else if statements + +function morseCharacterConverter(englishLetter){ + let morseLetter = '' + if (englishLetter === 'a') { + morseLetter = ' .– ' + } else if (englishLetter === 'b') { + morseLetter = ' -... ' + } else if (englishLetter === 'c') { + morseLetter = ' -.-. ' + } else if (englishLetter === 'd') { + morseLetter = ' -.. ' + } else if (englishLetter === 'e') { + morseLetter = ' . ' + } else if (englishLetter === 'f') { + morseLetter = ' ..-. ' + } else if (englishLetter === 'g') { + morseLetter = ' --. ' + } else if (englishLetter === 'h') { + morseLetter = ' .... ' + } else if (englishLetter === 'i') { + morseLetter = ' .. ' + } else if (englishLetter === 'j') { + morseLetter = ' .--- ' + } else if (englishLetter === 'k') { + morseLetter = ' -.- ' + } else if (englishLetter === 'l') { + morseLetter = ' .-.. ' + } else if (englishLetter === 'm') { + morseLetter = ' -- ' + } else if (englishLetter === 'n') { + morseLetter = ' -. ' + } else if (englishLetter === 'o') { + morseLetter = ' --- ' + } else if (englishLetter === 'p') { + morseLetter = ' .--. ' + } else if (englishLetter === 'q') { + morseLetter = ' --.- ' + } else if (englishLetter === 'r') { + morseLetter = ' .-. ' + } else if (englishLetter === 's') { + morseLetter = ' ... ' + } else if (englishLetter === 't') { + morseLetter = ' - ' + } else if (englishLetter === 'u') { + morseLetter = ' ..- ' + } else if (englishLetter === 'v') { + morseLetter = ' ...- ' + } else if (englishLetter === 'w') { + morseLetter = ' .-- ' + } else if (englishLetter === 'x') { + morseLetter = ' -..- ' + } else if (englishLetter === 'y') { + morseLetter = ' -.-- ' + } else if (englishLetter === 'z') { + morseLetter = ' --.. ' + } else if (englishLetter === ' ') { + morseLetter = ' ' + } else if (englishLetter === '.') { + morseLetter = '' + } else if (englishLetter === '?') { + morseLetter = '' + } else if (englishLetter === '!') { + morseLetter = '' + } + return morseLetter +} + +console.log(morseConverter("Hello, let's see if this works")) \ No newline at end of file From ea190c06aeb1d2c578c0f98d7fb70de712f46aa2 Mon Sep 17 00:00:00 2001 From: George Roth Date: Fri, 16 Sep 2022 16:57:24 +0100 Subject: [PATCH 4/8] Adding bigger spaces and skipping punctuation --- src/medium/morseCode/solution.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/medium/morseCode/solution.js b/src/medium/morseCode/solution.js index 11cd72a..00d8c31 100644 --- a/src/medium/morseCode/solution.js +++ b/src/medium/morseCode/solution.js @@ -63,12 +63,8 @@ function morseCharacterConverter(englishLetter){ } else if (englishLetter === 'z') { morseLetter = ' --.. ' } else if (englishLetter === ' ') { - morseLetter = ' ' - } else if (englishLetter === '.') { - morseLetter = '' - } else if (englishLetter === '?') { - morseLetter = '' - } else if (englishLetter === '!') { + morseLetter = ' ' + } else { morseLetter = '' } return morseLetter From 88a1acf5cdad1fba5974ab1da0f9e87f20455500 Mon Sep 17 00:00:00 2001 From: George Roth Date: Fri, 16 Sep 2022 16:59:09 +0100 Subject: [PATCH 5/8] Adding plain English morse code (forgot to save...) --- src/medium/morseCode/plain.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/medium/morseCode/plain.md b/src/medium/morseCode/plain.md index 73370f1..172966a 100644 --- a/src/medium/morseCode/plain.md +++ b/src/medium/morseCode/plain.md @@ -1 +1,8 @@ # Plain English Solution + +Create a function that converts letters to morse code with a million if statements + +Define a new empty string + +Loop through the elements of the input string + – use the function to add the correct morse code to a new string \ No newline at end of file From 8d1733109e9b06797472da5fdf92a3909d6bc838 Mon Sep 17 00:00:00 2001 From: George Roth Date: Thu, 22 Sep 2022 15:42:00 +0200 Subject: [PATCH 6/8] Adding roman numerals problem code --- src/medium/romanNumerals/solution.js | 50 ++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/medium/romanNumerals/solution.js b/src/medium/romanNumerals/solution.js index e69de29..6f04d17 100644 --- a/src/medium/romanNumerals/solution.js +++ b/src/medium/romanNumerals/solution.js @@ -0,0 +1,50 @@ +number = 100 + +const oneDigitMap = { + 1: 'I', + 2: "II", + 3: "III", + 4: "IV", + 5: "V", + 6: "VI", + 7: "VII", + 8: "VIII", + 9: "IX" +} + +const twoDigitMap = { + 1: "X", + 2: "XX", + 3: "XXX", + 4: "XL", + 5: "L", + 6: "LX", + 7: "LXX", + 8: "LXXX", + 9: "XC" +} + +const threeDigitMap = { + 1: "C", + 2: "CC", + 3: "CCC", + 4: "CD", + 5: "D", + 6: "DC", + 7: "DCC", + 8: "DCCC", + 9: "CM" +} + +function arabicToRomanNumberals (number) { + numAsString = number.toString() + if (numAsString.length === 1) { + return oneDigitMap[numAsString] || "" + } else if (numAsString.length === 2) { + return twoDigitMap[numAsString[0]] || "" + oneDigitMap[numAsString[1]] || "" + } else if (numAsString.length === 3) { + return threeDigitMap[numAsString[0]] || "" + twoDigitMap[numAsString[1]] || "" + oneDigitMap[numAsString[2]] || "" + } +} + +console.log(arabicToRomanNumberals(number)) From ea1fe82eec27fe19069d4c1bc2d0018e9b7f9967 Mon Sep 17 00:00:00 2001 From: George Roth Date: Fri, 23 Sep 2022 12:31:00 +0200 Subject: [PATCH 7/8] Adding Roman Numerals the right way --- src/medium/romanNumerals/solution.js | 78 +++++++++----------- src/medium/romanNumerals/solutionWrongWay.js | 50 +++++++++++++ 2 files changed, 83 insertions(+), 45 deletions(-) create mode 100644 src/medium/romanNumerals/solutionWrongWay.js diff --git a/src/medium/romanNumerals/solution.js b/src/medium/romanNumerals/solution.js index 6f04d17..892c31a 100644 --- a/src/medium/romanNumerals/solution.js +++ b/src/medium/romanNumerals/solution.js @@ -1,50 +1,38 @@ -number = 100 +const romanNumeral = 'DCCCXXVIII ' -const oneDigitMap = { - 1: 'I', - 2: "II", - 3: "III", - 4: "IV", - 5: "V", - 6: "VI", - 7: "VII", - 8: "VIII", - 9: "IX" +const valueMap = { + I: 1, + V: 5, + X: 10, + L: 50, + C: 100, + D: 500, + M: 1000 } -const twoDigitMap = { - 1: "X", - 2: "XX", - 3: "XXX", - 4: "XL", - 5: "L", - 6: "LX", - 7: "LXX", - 8: "LXXX", - 9: "XC" -} - -const threeDigitMap = { - 1: "C", - 2: "CC", - 3: "CCC", - 4: "CD", - 5: "D", - 6: "DC", - 7: "DCC", - 8: "DCCC", - 9: "CM" -} +console.log(romanToArabicNumerals(romanNumeral)) -function arabicToRomanNumberals (number) { - numAsString = number.toString() - if (numAsString.length === 1) { - return oneDigitMap[numAsString] || "" - } else if (numAsString.length === 2) { - return twoDigitMap[numAsString[0]] || "" + oneDigitMap[numAsString[1]] || "" - } else if (numAsString.length === 3) { - return threeDigitMap[numAsString[0]] || "" + twoDigitMap[numAsString[1]] || "" + oneDigitMap[numAsString[2]] || "" +function romanToArabicNumerals (roman) { + romanAsArray = roman.split('') + previousLetter = '' + arabicValue = 0 + for (let i = 0; i < romanAsArray.length; i++) { + currentLetter = romanAsArray[i] + if (currentLetter === "V" && previousLetter === "I") { + arabicValue -= 2 + } else if (currentLetter === "X" && previousLetter === "I") { + arabicValue -= 2 + } else if (currentLetter === "L" && previousLetter === "X") { + arabicValue -= 20 + } else if (currentLetter === "C" && previousLetter === "X") { + arabicValue -= 20 + } else if (currentLetter === "D" && previousLetter === "C") { + arabicValue -= 200 + } else if (currentLetter === "M" && previousLetter === "C") { + arabicValue -= 200 + } + arabicValue += valueMap[currentLetter] || 0 + previousLetter = currentLetter } -} - -console.log(arabicToRomanNumberals(number)) + return arabicValue +} \ No newline at end of file diff --git a/src/medium/romanNumerals/solutionWrongWay.js b/src/medium/romanNumerals/solutionWrongWay.js new file mode 100644 index 0000000..6f04d17 --- /dev/null +++ b/src/medium/romanNumerals/solutionWrongWay.js @@ -0,0 +1,50 @@ +number = 100 + +const oneDigitMap = { + 1: 'I', + 2: "II", + 3: "III", + 4: "IV", + 5: "V", + 6: "VI", + 7: "VII", + 8: "VIII", + 9: "IX" +} + +const twoDigitMap = { + 1: "X", + 2: "XX", + 3: "XXX", + 4: "XL", + 5: "L", + 6: "LX", + 7: "LXX", + 8: "LXXX", + 9: "XC" +} + +const threeDigitMap = { + 1: "C", + 2: "CC", + 3: "CCC", + 4: "CD", + 5: "D", + 6: "DC", + 7: "DCC", + 8: "DCCC", + 9: "CM" +} + +function arabicToRomanNumberals (number) { + numAsString = number.toString() + if (numAsString.length === 1) { + return oneDigitMap[numAsString] || "" + } else if (numAsString.length === 2) { + return twoDigitMap[numAsString[0]] || "" + oneDigitMap[numAsString[1]] || "" + } else if (numAsString.length === 3) { + return threeDigitMap[numAsString[0]] || "" + twoDigitMap[numAsString[1]] || "" + oneDigitMap[numAsString[2]] || "" + } +} + +console.log(arabicToRomanNumberals(number)) From 7451bb356f38869b34714c255d2becef6f613a2c Mon Sep 17 00:00:00 2001 From: George Roth Date: Fri, 23 Sep 2022 12:35:16 +0200 Subject: [PATCH 8/8] Adding comments to the roman numerals code --- src/medium/romanNumerals/solution.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/medium/romanNumerals/solution.js b/src/medium/romanNumerals/solution.js index 892c31a..6654c4a 100644 --- a/src/medium/romanNumerals/solution.js +++ b/src/medium/romanNumerals/solution.js @@ -1,4 +1,4 @@ -const romanNumeral = 'DCCCXXVIII ' +const romanNumeral = 'DCCCXXVIII' const valueMap = { I: 1, @@ -13,12 +13,12 @@ const valueMap = { console.log(romanToArabicNumerals(romanNumeral)) function romanToArabicNumerals (roman) { - romanAsArray = roman.split('') - previousLetter = '' - arabicValue = 0 + romanAsArray = roman.split('') // Splitting the input into a list + previousLetter = '' // Empty variable so we can see if we need to deduct in the future + arabicValue = 0 // Empty variable for the value total at the end for (let i = 0; i < romanAsArray.length; i++) { - currentLetter = romanAsArray[i] - if (currentLetter === "V" && previousLetter === "I") { + let currentLetter = romanAsArray[i] // new var in current iteration for easier readability + if (currentLetter === "V" && previousLetter === "I") { // huge if statemet for deducting value (there has to be a neater way) arabicValue -= 2 } else if (currentLetter === "X" && previousLetter === "I") { arabicValue -= 2 @@ -31,8 +31,8 @@ function romanToArabicNumerals (roman) { } else if (currentLetter === "M" && previousLetter === "C") { arabicValue -= 200 } - arabicValue += valueMap[currentLetter] || 0 - previousLetter = currentLetter + arabicValue += valueMap[currentLetter] || 0 // iterating through the map and adding the value + previousLetter = currentLetter // setting up the previous letter so we can check if we need to deduct stuff next iteration } return arabicValue } \ No newline at end of file