diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..92e6c26 Binary files /dev/null and b/.DS_Store differ diff --git a/Day01/Problem01.js b/Day01/Problem01.js index 6ad3c39..fc08345 100644 --- a/Day01/Problem01.js +++ b/Day01/Problem01.js @@ -1,58 +1,47 @@ class Problem { - /** - * Have the wordCount(input) take the str string parameter being passed - * and return the number of words the string contains - * (e.g. "Never eat shredded wheat or cake" would return 6). Words will be - * separated by single spaces. - */ + wordCount(input) { - // code goes here - return null; + return input.split(" ").length; } - - /** - * Have the function LetterCapitalize(str) take the str parameter being passed and capitalize the first - letter of each word. Words will be separated by only one space. - * - */ letterCapitalize(str) { - return null; - } + const userInput = str; + const wordsOnly = userInput.split(" "); + for (let i = 0; i < wordsOnly.length; i++) { + wordsOnly[i] = wordsOnly[i][0].toUpperCase() + wordsOnly[i].substr(1); + } + let newStr = wordsOnly.join(" "); + return newStr; + } - /** - * Have the function firstReverse(String input) take the input parameter being passed and - * return the string in reversed order. For example: if the input string is "Hello World and Coders" then your - * program should return the string sredoC dna dlroW olleH. - */ - - firstReverse(input){ - return null; + firstReverse(input) { + return input.split("").reverse().join(""); } - /** - * Have the longestWord(String input) take the input parameter being passed and return the - * largest word in the string. If there are two or more words that are the same length, - * return the first word from the string with that length. Ignore punctuation and assume - * input will not be empty. - */ longestWord(input) { - // code goes here - return null; + var string = input.match(/\w[a-z]{0,}/gi); + var longestWord = string[0]; + for (var i = 1; i < string.length; i++) { + if (string[i].length > longestWord.length) { + longestWord = string[i]; + } + } + return longestWord; } - /** - * Have the swapCase(String input) take the input parameter and swap the case of each - * character. For example: if str is "Hello World" the output should be hELLO wORLD. - * Let numbers and symbols stay the way they are. - */ - swapCase(str) { - // code goes here - return null; + SwapCase(str) { + let newStr = ""; + for (let i = 0; i < str.length; i++) { + if (str[i] === str[i].toLowerCase()) { + newStr += str[i].toUpperCase(); + } else { + newStr += str[i].toLowerCase(); + } + } + return newStr; } - } module.exports = Problem; \ No newline at end of file diff --git a/Day01/Problem01.test.js b/Day01/Problem01.test.js index 3623ab2..14d6d33 100644 --- a/Day01/Problem01.test.js +++ b/Day01/Problem01.test.js @@ -55,7 +55,7 @@ test("firstReverse test01", () => { test("firstReverse test02", () => { let problem = new Problem(); - let expected = "eiD syobwoC eiD ,ylF selgaE ylF"; + let expected = "eiD syobwoC eiD ,ylF selgaE ylF"; let actual = problem.firstReverse("Fly Eagles Fly, Die Cowboys Die"); @@ -87,7 +87,7 @@ test("swapCase test01", () => { let problem = new Problem(); let expected = "hELLO-lol"; - let actual = problem.swapCase("Hello-LOL"); + let actual = problem.SwapCase("Hello-LOL"); expect(actual).toEqual(expected); }); @@ -97,7 +97,7 @@ test("swapCase test02", () => { let problem = new Problem(); let expected = "sUP dude!!?"; - let actual = problem.swapCase("Sup DUDE!!?"); + let actual = problem.SwapCase("Sup DUDE!!?"); expect(actual).toEqual(expected); }); \ No newline at end of file