Solución

@alkgonzalez_bf811590·1/7/2026TypeScript
solution.tsTypeScript
export function vigenereEncrypt(text: string, key: string): string {
  let result = []
  if(text.length === 0){
    return ""
  }

  let alphabet: string[] = [
  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 
  'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
  ];

  let keyIndex = 0; // Controla qué letra de "KEY" toca usar
  let extendedKey = ''

  for (const char of text) {
    // Expresión regular para detectar si es una letra (A-Z, a-z)
    if (/[a-zA-Z]/.test(char)) {
      // Si es letra, añadimos el carácter de la clave y avanzamos su índice
      extendedKey += key[keyIndex % key.length];
      keyIndex++;
    } else {
      // Si es espacio o carácter especial, lo copiamos tal cual sin avanzar la clave
      extendedKey += char;
    }
  }

  const array: string[][] = []
  for(let i = 0; i < alphabet.length; i++){
    array[i] = []
    for(let k = 0; k < alphabet.length; k++){
      array[i][k] = alphabet[k]
    }
    alphabet.push(alphabet.shift())
  }

  for(let i = 0; i < text.length; i++){
    const index = alphabet.findIndex(v => v == text[i].toLowerCase())

    if(index != -1){
      const secondIndex = alphabet.findIndex(v => v == extendedKey[i].toLowerCase())

      const isLowerCase = alphabet[index] === text[i]
      result.push(isLowerCase ?  array[index][secondIndex]: array[index][secondIndex].toUpperCase())
    } else {
      result.push(text[i])
    }
  }
  
  return result.join('');
}
0respuestas
Respuestas

Aún no hay respuestas

¡Sé el primero en responder!

Escribir un comentario

Recuerda ser amable. Estás comentando la solución de otra persona. Comparte tu perspectiva de forma constructiva y respetuosa.

Debes iniciar sesión para publicar un comentario.