Untitled

mail@pastecode.io avatar
unknown
typescript
a year ago
827 B
11
Indexable
function theTest(text: string): string {

	const charaterToReplace = 'a';
	let result = text

	for (let i = 0; i < text.length; i++) {
		const j = text.length - i - 1

		if (i >= j) {
			break
		}

		if (text[i] == '?' && text[j] == '?') {
			result = replaceAtIndex(result, i, charaterToReplace)
			result = replaceAtIndex(result, j, charaterToReplace)
			continue
		}

		if (text[i] == '?') {
			result = replaceAtIndex(result, i, text[j])
			continue;
		}

		if (text[j] == '?') {
			result = replaceAtIndex(result, j, text[i])
			continue;
		}

		if (text[i] != text[j]) {
			return "NO"
		}

	}

	return result
}

function replaceAtIndex(text: string, index: number, replacement: string): string {
	return text.substring(0, index) + replacement + text.substring(index + replacement.length);
}

console.log(theTest("?a?"));