Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

335
Vistas
Alternative of Python's Array.index() function in JavaScript that treats negative indices the same as Python's Array.index()?

TLDR: Is there any alternative of Python's Array.index() method in JavaScript that treats negative indices the same as Python's Array.index()?


For background, I'm writing a code for the Caesar cipher (Wikipedia link).

I have written the following code in Python:

alphabet = list('abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz') 
# list('abc') == ['a', 'b', 'c']

def caesar(text, shift, direction):
    endText = ''
    if direction == 'encode':
        shift = shift
    else:
        shift *= -1
    for i in text:
        oldIndex = alphabet.index(i)
        newIndex = oldIndex + shift
        newLetter = alphabet[newIndex]
        endText += newLetter
    print(endText)

caesar('zeus', 5, 'encode')  # 'ejzx'
caesar('ejzx', 5, 'decode')  # 'zeus'

When I enter a word that has letters that are towards the end of the English alphabet (i.e. containing letters such as v, w, x, y, z etc), it encodes the word and when I decode the result, it gives the word I encoded previously without any errors.

When I write the exact same code in JavaScript, and when I encode the same message I used in the Python code ('zeus'), it gives me ejzx; however, when I decode ejzx, it gives me undefinedeus instead of zeus i.e. it does decode the eus, but not the initial z.

JavaScript:

 let alphabet = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'.split('');

 function caesar(text, shift, direction) {
   let endText = '';
   if (direction == 'encode') {
     shift = shift;
   } else if (direction == 'decode') {
     shift *= -1;
   }
   for (let i of text) {
     let oldIndex = alphabet.indexOf(i);
     let newIndex = oldIndex + shift;
     let newLetter = alphabet[newIndex];
     endText += newLetter;
   }
   console.log(endText)
 }


 caesar('zeus', 5, 'encode') // 'ejzx'
 caesar('ejzx', 5, 'decode') // 'undefinedeus'

I tried replacing the indexOf() with Array.at() (MDN link), but it returns undefinedundefinedundefinedundefined both in encoding and decoding.

How could I fix the code so that it works the same as Python?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You don't have to replace indexOf() with Array.prototype.at(), instead of that you have to replace alphabet[newIndex] to alphabet.at(newIndex);

let alphabet = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'.split('');

 function caesar(text, shift, direction) {
   let endText = '';
   if (direction == 'encode') {
     shift = shift;
   } else if (direction == 'decode') {
     shift *= -1;
   }
   for (let i of text) {
     let oldIndex = alphabet.indexOf(i);
     let newIndex = oldIndex + shift;
     let newLetter = alphabet.at(newIndex);
     endText += newLetter;
   }
   console.log(endText)
 }

Hope this helps.

about 4 years ago · Juan Pablo Isaza Denunciar

0

I don't know pyton, but in javascript the index ever starts from 0.
In this way, in your example, to encode the letter "z" (index: 25, position: 26), you add 5 to the index and it returns the letter "e" (index: 30, position: 31), so far so good. To decode the letter "e", javascript finds it at index 4 (5th position) and by subtracting 5 it turns out that the index you are looking for for the new letter is -1 and since that index does not exist, it returns undefined. To avoid this, when you decode, add 26 to the "shift" (an alphabet) so that it uses the second set of characters from the alphabet variable, and since it will always go backwards (subtract), it will always find the corresponding character.

let alphabet = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'.split('');
 function caesar(text, shift, direction) {
   let endText = '';
   if (direction == 'encode') {
     shift = shift;
   } else if (direction == 'decode') {
     shift = (shift * -1) + 26;
   }
   for (let i of text) { 
     let oldIndex = alphabet.indexOf(i);
     let newIndex = oldIndex + shift;
     let newLetter = alphabet[newIndex];
     endText += newLetter;
   }
   console.log(endText)
 }


 caesar('zeus', 5, 'encode') // 'ejzx'
 caesar('ejzx', 5, 'decode') // 'zeus'

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda