Currently, I am working on a task that involves replacing specific numbers in a given string with corresponding characters. To be more precise:
- The number 5 should be replaced with the character "S"
- The number 1 should be replaced with the character "I"
- The number 0 should be replaced with the character "O"
(this is part of a Kata challenge)
I have developed a function that aims to:
- Convert the string into an array
- Iterate over the array
- Replace each occurrence of the specified numbers with the appropriate characters using the .replace() method
- Return the concatenated string.
This is how my code currently looks like:
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('');
}
However, when I execute this function, it returns the original string without any changes. I suspect that the problem lies in the way I am using the .replace() method within the loop or possibly the way index positions are defined.