diff --git a/StringsAndTings.js b/StringsAndTings.js index 4fdca47..47e45e6 100644 --- a/StringsAndTings.js +++ b/StringsAndTings.js @@ -1,28 +1,65 @@ class StringsAndTings { - + // @return string with identical content, and the first character capitalized - camelCase(str){ - return null; + camelCase(str) { + let result = ""; + for (let i = 0; i < str.length; i++) { + if (i == 0 || str[i - 1] == " ") { + result += str[i].toUpperCase(); + } else { + result += str[i]; + } + } + return result; + } + + reverseString(str) { + // Strings in js are immutable. Need to split it into a new array to modify it. + let result = str.split(""); + let length = result.length; + // You would think you'd need to subtract 1 from here, but that does not work. This method seems to work for even and odd lengthed inputs. + const halfLength = Math.floor(length / 2); + for (let i = 0; i < halfLength; i++) { + let rightLetter = result[length - i - 1]; + result[length - i - 1] = result[i]; + result[i] = rightLetter; + } + return result.join("").toString(); } - //@return string with identical contents, with each word individually in reverse order - reverseString(str){ - return null; + reverseWords(str) { + let result = ""; + let words = str.split(" "); + for (let word of words) { + word = this.reverseString(word); + result += `${word} `; + } + return result.trim(); } // @return string with identical contents, in reverse order, with first character capitalized - reverseThenCamelCase(str){ - return null; + reverseThenCamelCase(str) { + const reversedStr = this.reverseString(str); + return this.camelCase(reversedStr); } // @return string with identical contents excluding first and last character - removeFirstAndLastCharacter(str){ - return null; + removeFirstAndLastCharacter(str) { + // Use str.length - 1 because it returns the value *up to* that index, not *at* that index. So return value up to the very last index. + return str.substring(1, str.length - 1) } // @return string with identical characters, each with opposite casing - invertCasing(str){ - return null; + invertCasing(str) { + let result = ""; + for (let c of str) { + if (c == c.toUpperCase()) { + result += c.toLowerCase(); + } else { + result += c.toUpperCase(); + } + } + return result; } } diff --git a/StringsAndTings.test.js b/StringsAndTings.test.js index 689b63d..31948d2 100644 --- a/StringsAndTings.test.js +++ b/StringsAndTings.test.js @@ -8,7 +8,7 @@ test("camelCaseTest", () => { let input = "she sells sea shells"; let expected = "She Sells Sea Shells"; - let actual = stringsAndTings.camelCase(15); + let actual = stringsAndTings.camelCase(input); expect(actual).toEqual(expected); }); @@ -19,7 +19,7 @@ test("reverseTest", () => { let input = "she sells sea shells"; let expected = "sllehs aes slles ehs"; - let actual = stringsAndTings.camelCase(15); + let actual = stringsAndTings.reverseString(input); expect(actual).toEqual(expected); }); @@ -30,7 +30,7 @@ test("reverseWordsTest", () => { let input = "she sells sea shells"; let expected = "ehs slles aes sllehs"; - let actual = stringsAndTings.camelCase(15); + let actual = stringsAndTings.reverseWords(input); expect(actual).toEqual(expected); }); @@ -41,7 +41,7 @@ test("reverseThenCamelCaseTest", () => { let input = "she sells sea shells"; let expected = "Sllehs Aes Slles Ehs"; - let actual = stringsAndTings.camelCase(15); + let actual = stringsAndTings.reverseThenCamelCase(input); expect(actual).toEqual(expected); }); @@ -52,7 +52,7 @@ test("removeFirstAndLastCharacterTest", () => { let input = "she sells sea shells"; let expected = "he sells sea shell"; - let actual = stringsAndTings.camelCase(15); + let actual = stringsAndTings.removeFirstAndLastCharacter(input); expect(actual).toEqual(expected); }); @@ -64,7 +64,7 @@ test("invertCasingTest1", () => { let input = "shE sells SEA sHeLlS"; let expected = "SHe SELLS sea ShElLs"; - let actual = stringsAndTings.camelCase(15); + let actual = stringsAndTings.invertCasing(input); expect(actual).toEqual(expected); });