diff --git a/StringsAndTings.js b/StringsAndTings.js index 4fdca47..6c77acb 100644 --- a/StringsAndTings.js +++ b/StringsAndTings.js @@ -2,28 +2,69 @@ class StringsAndTings { // @return string with identical content, and the first character capitalized camelCase(str){ - return null; + // Split string into an array + let stringSplit = str.split(" "); + + // char at to select the first letter and also to uppercase to capitalize of each indice in the array + for (let i = 0; i < stringSplit.length; i++) { + let word = stringSplit[i]; + stringSplit[i] = word[0].toUpperCase() + word.substring(1); + } + return stringSplit.join(" "); } //@return string with identical contents, with each word individually in reverse order reverseString(str){ - return null; + let reverse = ""; + for (let i = str.length - 1; i >= 0; i--) { + reverse += str[i]; + } + return reverse; } + + // @return string with each word reversed but still in the same order + reverseWords(str){ + let stringSplit = str.split(" "); + let newArr = []; - // @return string with identical contents, in reverse order, with first character capitalized - reverseThenCamelCase(str){ - return null; + for (let i = 0; i < stringSplit.length; i++){ + stringSplit[i] = stringSplit[i].split("").reverse(); + let rejoin = stringSplit[i].join("") + " "; + newArr += rejoin; + } + return newArr.trim() + } + + // @return string with identical contents, in reverse order, with first character capitalized + reverseThenCamelCase(str){ + let reverseCap = str.split("").reverse().join(""); + let string = reverseCap.split(" "); + for (let i = 0; i < string.length; i++) { + let word = string[i]; + string[i] = word[0].toUpperCase() + word.substring(1); + } + return string.join(" "); } // @return string with identical contents excluding first and last character removeFirstAndLastCharacter(str){ - return null; + let firstLastChar = str.substring(1, str.length - 1); + return firstLastChar; } // @return string with identical characters, each with opposite casing invertCasing(str){ - return null; + var output = ""; + for(var i = 0; i < str.length; i++){ + if(str[i] == str[i].toUpperCase()){ + output += str[i].toLowerCase(); + }else{ + output += str[i].toUpperCase(); + } + } + return output; } } + module.exports = StringsAndTings; \ No newline at end of file diff --git a/StringsAndTings.test.js b/StringsAndTings.test.js index 689b63d..98942ab 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("she sells sea shells"); 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("she sells sea shells"); 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("she sells sea shells"); 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("she sells sea shells"); 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("she sells sea shells"); 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("shE sells SEA sHeLlS"); expect(actual).toEqual(expected); });