create palindrome tool, testCases and undated index file

This commit is contained in:
Chesterkxng
2024-07-10 14:34:17 +00:00
parent 7a7bf06b28
commit b309921d89
5 changed files with 126 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
import { reverseString } from 'utils/string'
export function createPalindrome(
input: string,
lastChar: boolean // only checkbox is need here to handle it [instead of two combo boxes]
) {
if (!input) return '';
let result: string;
let reversedString: string;
// reverse the whole input if lastChar enabled
reversedString = lastChar ? reverseString(input) : reverseString(input.slice(0, -1));
result = input.concat(reversedString);
return result;
}
export function createPalindromeList(
input: string,
lastChar: boolean,
multiLine: boolean
): string {
if (!input) return '';
let array: string[];
let result: string[] = [];
if (!multiLine) return createPalindrome(input, lastChar);
else {
array = input.split('\n');
for (const word of array) {
result.push(createPalindrome(word, lastChar));
}
}
return result.join('\n');
}