1

I am attempting to take a given string and replace a given number in that string with a character. Specifically:

  • 5 is to be replaced with "S"
  • 1 is to be replaced with "I"
  • 0 is to be replaced with "O"

(this is a Kata exercise)

I have written a function which should:

  1. Take the string and convert it into an array
  2. Loop over the array
  3. Replace each instance of the aforementioned numbers with the appropriate replacements using .replace()
  4. Return the joined string.

My code is as appears below

Function correct(string) {
let array = string.split('')
  for (let i = 0; i < array.length; i++) {
    array[i].replace('5', 'S')
    array[i].replace('0', 'O')
    array[i].replace('1', 'I')
  }
  return array.join('');
}

The function returns the string exactly as initially provided, unchanged. I suspect my issue is with the way I've used the .replace() method within the loop, or the index positions declared.

6
  • 1
    .replace "returns a new string"... Commented Jun 8, 2022 at 8:10
  • 1
    String.replace doesn't work in place. You have to assign the result somewhere ... Commented Jun 8, 2022 at 8:11
  • Just use String.prototype.replaceAll, like string.replaceAll('5', 'S').replaceAll('0', 'O') Commented Jun 8, 2022 at 8:12
  • 1
    If you want your code to work, just assign a result of a replacement like so: array[i] = array[i].replace('5', 'S');. But I recomment using the solution from my previous comment. Commented Jun 8, 2022 at 8:15
  • You can also use a regualar expression for replacement return string.replace(/5/g, 'S').replace(/0/g, 'O').replace(/1/g, 'I') Commented Jun 8, 2022 at 8:17

3 Answers 3

1

String.prototype.replace returns a new string and does not mutate the variable it was executed on. That's why you need to assign the result of replacement somewhere.

To fix your code just do the following with each replacement:

array[i] = array[i].replace('5', 'S');

Note 1: since array[i] is always a single character, there is no need in replace, because you can just compare like so:

if (array[i] === '5') {
  array[i] = 'S';
}

or oneliner:

array[i] = array[i] === '5' ? 'S' : array[i];

Note 2: for loop for a replacement is not a clean code. JavaScript is very flexible and provides much better solutions to the problem. Eg. String.prototype.replaceAll.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this :

const inputStr = 'AB5DE0G1H';

function correct(string) {
  return  string.replaceAll('5', 'S').replaceAll('0', 'O').replaceAll('1', 'l')
}

console.log(correct(inputStr));

Comments

0

You can do with simple regex replace() based on your key=>value like this,

const key_to_replace = { 
    '5': 'S',
    '1': 'I',
    '0': 'O'
}

const correct = (str) => 
   str.replace(/./g, (m) => key_to_replace[m] ?? m);

console.log(correct("this 5is a0 test1 with 55abc"))

5 Comments

What is a purpose of string template literal?
This code does not work for more than one occurrence. For example for this string 55abc.
I have a few correct solutions for this exercise. However, I was wondering whether one can loop over the array using the .replace() method specifically. If not, how come?
@ChristosBinos you can, see my second comment under you question.
@ChristosBinos it should work now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.