Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 49 additions & 12 deletions StringsAndTings.js
Original file line number Diff line number Diff line change
@@ -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;
}
}

Expand Down
12 changes: 6 additions & 6 deletions StringsAndTings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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);
});
Expand All @@ -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);
});
Expand All @@ -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);
});
Expand All @@ -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);
});
Expand All @@ -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);
});
Expand Down