I am working on a function here and have tried lots of different ways to take the shifted word and preserve the casing . I have even tried a module so I went back to code.
My question here is I am trying to get off the ground with the proper logic to preserve the case comparing the old word to the new word and making a new text is not so good. I am getting to the goal but the text is getting duplicated. on the capitalized words with lowercase in random order.
How to do i got about this?
//import { replace, html } from "preserve-case-replace";
//const { default: replace } = require("preserve-case-replace");
//import replace from "preserve-case-replace";
function ceasarCiper(word,cipher){
let answer = "";
let word1 = word
let trackCasing = []
for (let i = 0; i < word1.length; i++){
if (word1[i].match(/^[A-Z]*$/)){
trackCasing.push(i)
}
}
console.log(trackCasing)
word = word.toLowerCase()
//let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
let alphabet = "abcdefghijklmnopqrstuvwxyz"
console.log(alphabet[23], "testing here")
console.log(alphabet[0])
let newWord = ""
for (let j = 0; j < word.length; j++){
if (!word[j].match(/[a-zA-Z]/i)){
newWord += word[j]
}
for (let i = 0; i < alphabet.length; i++){
if (alphabet[i] === word[j] && (i+cipher) > 25){
let remainder = (i+cipher) - 25
newWord += alphabet[remainder-1]
} else{
if(alphabet[i] === word[j]){
console.log(cipher)
console.log(alphabet[i + cipher], i, i+cipher)
newWord += alphabet[i+cipher] }
}
};
};
console.log(trackCasing)
newWord = newWord.split("")
//console.log(answer)
//replace(inputText, word. newWord)
return newWord
};
//console.log(ceasarCiper("hello", 2))
console.log(ceasarCiper("x?Y!Z", 3))
//failed test cases
/* for (let i = 0; i < newWord.length; i++){
for (let j = 0; j < trackCasing.length; j++){
if (i === trackCasing[j]){
answer += newWord[i].toUpperCase()
}
else(
answer += newWord[i]
)
}
}
answer = [...new Set(answer)].join("")*/
/*let counter = trackCasing.length
//string.replace(searchValue, replaceValue);
while(counter){
for (let i = 0; i < trackCasing.length; i++){
console.log(trackCasing[i])
let a = newWord.replace(newWord.charAt(trackCasing[i]), newWord.charAt(trackCasing[i]).toUpperCase())
counter--
console.log(a)
}
}*/
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="" src="test4.js"></script>
<title>Document</title>
</head>
<body>
</body>
</html>