Skip to content

Commit 1d07973

Browse files
committed
rotateCharacter function done
1 parent 6de4e81 commit 1d07973

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

Sprint-3/implement/rotate-char.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,46 @@
1111
// Given a character and a shift value,
1212
// When the function rotateCharacter is called with these inputs,
1313
// Then it should:
14+
function rotateCharacter(letter, num) {
15+
// Check if the input is a letter
16+
if (!/[a-zA-Z]/.test(letter)) {
17+
return letter; // Return the original character if it's not a letter
18+
}
19+
20+
// Convert letter to uppercase
21+
const upperLetter = letter.toUpperCase();
22+
23+
// Base character code for 'A'
24+
const baseCode = 'A'.charCodeAt(0);
25+
26+
// Calculate the new character code
27+
const rotatedCode = (upperLetter.charCodeAt(0) - baseCode + num) % 26 + baseCode;
28+
29+
// Convert back to character
30+
const rotatedChar = String.fromCharCode(rotatedCode);
31+
32+
// Preserve case
33+
if (letter === letter.toLowerCase()) {
34+
return rotatedChar.toLowerCase();
35+
} else {
36+
return rotatedChar;
37+
}
38+
}
39+
40+
console.log(rotateCharacter('F', 2)); // Output: H
41+
console.log(rotateCharacter('7', 5)); // Output: 7 (unchanged, not a letter)
42+
console.log(rotateCharacter("f", 1)); // Output: g
43+
console.log(rotateCharacter("A", 3)); // Output: D
44+
console.log(rotateCharacter("z", 1)); // Output: a
45+
1446

1547
// Scenario: Rotate Lowercase Letters:
1648
// Given a lowercase letter character and a positive integer shift,
1749
// When the function is called with these inputs,
18-
// Then it should rotate the lowercase letter by shift positions within the lowercase alphabet, wrapping around if necessary, and return the rotated lowercase letter as a string.
19-
console.log(rotateCharacter("a", 3)); // Output: "d"
50+
// Then it should rotate the lowercase letter by shift positions within the
51+
// lowercase alphabet, wrapping around if necessary, and return the rotated
52+
// lowercase letter as a string.
53+
// console.log(rotateCharacter("a", 3)); // Output: "d"
2054
console.log(rotateCharacter("f", 1)); // Output: "g"
2155

2256
// Scenario: Rotate Uppercase Letters:
@@ -39,5 +73,5 @@ console.log(rotateCharacter("7", 5)); // Output: "7" (unchanged, not a letter)
3973
// When the rotateCharacter function is called with char and shift as inputs,
4074
// Then it should correctly rotate the character by shift positions within the alphabet while handling the wraparound,
4175
// 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').
42-
console.log(rotateCharacter("z", 1)); // Output: "a" (preserves case, but wraps around)
43-
console.log(rotateCharacter("Y", 2)); // Output: "A" (preserves case, but wraps around)
76+
// console.log(rotateCharacter("z", 1)); // Output: "a" (preserves case, but wraps around)
77+
// console.log(rotateCharacter("Y", 2)); // Output: "A" (preserves case, but wraps around)

0 commit comments

Comments
 (0)