JSFiddle - React, Tailwind, and code Playground
by moob
JavaScript
function encodeString(str) {
const alphabet = 'abcdefghijklmnopqrstuvwxyz';
const shift = 5;
let encodedStr = '';
for (let i = 0; i < str.length; i++) {
const char = str[i].toLowerCase();
const index = alphabet.indexOf(char);
if (index !== -1) {
const encodedIndex = (index + shift) % alphabet.length; // Loop around the alphabet
encodedStr += alphabet[encodedIndex];
} else {
encodedStr += str[i]; // If the character is not in the alphabet, keep it unchanged
}
}
return encodedStr;
}
// Example usage:
const originalStr = "encode your answer. what day is it?";
const encodedStr = encodeString(originalStr);
console.log(encodedStr); // Output: "hfyx fsi itlx"