2

I'm making a deciphering function and I'm stuck on a part where I need to swap the positions of the second letter and the last letter of the string. I have also tried using the replace method but I think substring should be used.

Hello should equal Holle, etc

function decipher(str) {
  let s = ""
  for (let word of str.split(" ")){
    let dig = word.match(/\d+/)[0]
    word = word.replace(dig, String.fromCharCode(dig))
    let secondLetter = word[1]
    let lastLetter = word[word.length - 1]
    let swapped = word.substring(0,1) + lastLetter + word.substring(2,3) + secondLetter
    s += swapped + " "
  }
  return s
}; 
tarik
  • 33
  • 7
  • 2
    Can't you just swap directly? Put the second letter in a temp variable and then assign the last letter to the second letter and then assign the temp variable to the last letter – greedsin Jul 30 '21 at 12:15

5 Answers5

1

Please change this line:

let swapped = word.substring(0,1) + lastLetter + word.substring(2,word.length - 1) + secondLetter;
Rinkal Rohara
  • 232
  • 1
  • 7
  • I know that line is the problem, it only works on 4 letter strings, I'm looking for an alternative approach which would work on strings of all lengths – tarik Jul 30 '21 at 12:19
1

You can destructure the string:

const swap = ([a, b, ...xs]) => [a, xs.pop(), ...xs, b].join('');
//                                  ^^^^^^^^         ^
//                                  |____swapping____|

swap("Hello");
//=> "Holle"

With destructuring you will also support things like emojis (but maybe not graphemes):

swap("Hll");
//=> "Hll"

Swapping words in a string:

const decipher = str => str.split(' ').map(swap).join(' ');

decipher("Hello World");
//=> "Holle Wdrlo"

decipher(decipher("Hello World"));
//=> "Hello World"

Why destructure?

Reading characters in a string via index or (simple) regex probably won't work with multi-codepoint characters such as (but not limited to) emojis:

"".length;
//=> 2! Not 1.

"".charAt(0);
//=> "\ud83c"! Not "".

Consider this swap function:

function swap(str) {
  var arr = str.split('');
  var [a, b] = [arr[1], arr[arr.length-1]];
  arr[1] = b;
  arr[arr.length-1] = a;
  return arr.join('');
}

Works fine with plain old ASCII:

swap("Hello");
//=> "Holle"

Doesn't work as you would expect with emojis:

swap("Hll");
//=> "H\udf63\udf2fll\ud83c\ud83c"
customcommander
  • 17,580
  • 5
  • 58
  • 84
0

Consider extracting it into a function to keep a cleaner codebase:

function swapSecondAndLastLetter(str) {
   // Split the string into a mutable array
   let original = str.split('');

   original[1] = str[str.length-1];
   original[original.length-1] = str[1];

   // Join the mutable array back into a string
   return original.join('');
}
Kamen Kotsev
  • 303
  • 1
  • 7
0

If it is only for a specific use case (i.e. swap second and last), you can do it with simple regex -

Regex -(.)(.)(.*)(.)

const str = "Hello";
console.log(swap(str));

function swap() {
  return str.replace(/(.)(.)(.*)(.)/, "$1$4$3$2")
}
Nikhil Patil
  • 2,480
  • 1
  • 7
  • 20
0

We can create a basic function like this

const swapCharacters = (str, char1, char2)=>{
  let a = str.replaceAll(char1, '~')
  let b = a.replaceAll(char2, char1)
  let c = b.replaceAll('~', char2)
  return c
}

console.log(swapCharacters('42,23.5453,6', '.', ',')) //42.23,5453.6
Mikias
  • 131
  • 2
  • 3