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?
>Solution :
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.