Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

331
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda