Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

London | Zohreh-Kazemianpour | Structuring-and-testing-data | week3 | sprint 3 #223

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
rotate char
zohrehKazemianpour committed Dec 7, 2024
commit cf91d45f8e201927a12e944bc5867b2397a188a0
17 changes: 17 additions & 0 deletions Sprint-3/implement/rotate-char.js
Original file line number Diff line number Diff line change
@@ -41,3 +41,20 @@ console.log(rotateCharacter("7", 5)); // Output: "7" (unchanged, not a letter)
// And the function should return the rotated character as a string (e.g., 'z' rotated by 3 should become 'c', 'Z' rotated by 3 should become 'C').
console.log(rotateCharacter("z", 1)); // Output: "a" (preserves case, but wraps around)
console.log(rotateCharacter("Y", 2)); // Output: "A" (preserves case, but wraps around)

function rotateCharacter(char, shift) {


if (!/[a-zA-Z]/.test(char)) {
return char;
}

const base = char === char.toLowerCase() ? 'a'.charCodeAt(0) : 'A'.charCodeAt(0);

const code = char.charCodeAt(0);


const rotatedCode = ((code - base + shift) % 26) + base;

return String.fromCharCode(rotatedCode);
}
35 changes: 35 additions & 0 deletions Sprint-3/implement/rotate-char.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function rotateCharacter(char, shift) {


if (!/[a-zA-Z]/.test(char)) {
return char;
}

const base = char === char.toLowerCase() ? 'a'.charCodeAt(0) : 'A'.charCodeAt(0);

const code = char.charCodeAt(0);


const rotatedCode = ((code - base + shift) % 26) + base;

return String.fromCharCode(rotatedCode);
}

test('Rotate Lowercase Letters', () => {
expect(rotateCharacter("a", 3)).toBe("d");
expect(rotateCharacter("f", 1)).toBe("g");
});

test('Rotate Uppercase Letters', () => {
expect(rotateCharacter("A",3)).toBe("D");
expect(rotateCharacter("F", 1)).toBe("G");
});
test('Leave Non-Letter Characters Unchanged', () => {

expect(rotateCharacter("7", 5)).toBe("7");
});

test('Shifting a Character with Wraparound', () => {
expect(rotateCharacter("z",1)).toBe("a");
expect(rotateCharacter("Y", 2)).toBe("A");
});