Convert some digits to letters

hi, i’m wondering why this doesn’t work and how can i fix it? I already know one method with the split and then join. However, the below example seems to result in buggy situations where sometimes it works and sometimes it doesn’t. Could someone explain the logic please and how to fix it?


function correct(string)
  var newword = ""
	for (var i=0; i < string.length; i++) {
    if (string.charAt(i) == "5") {
        var result = string.replace(/5/g,'S');
         if (string.charAt(i) == "0") {
      result = string.replace(/0/g,'O');
     if (string.charAt(i) == "1") {
      result = string.replace(/1/g,'I');
   } console.log(result)

Maybe this Functional Programming lesson can help you:

What specific test cases cause it to produce unexpected results?

There isn’t much logic there to explain. I would point out that each regex has the global flag enabled, so the loop is entirely redundant…and so are the if statements.