Coding Problem Help

Hello,

Link to challenge here: https://www.codewars.com/kata/577bd026df78c19bca0002c0/train/javascript

Problem I am facing: I am using REGEX to replace 0, 5, 1 with O, S, I, but I keep getting the following response when inputting the code below:

function correct(string) {
  console.log(string);
  let answer = string.replace(/0|5|1/gi, "O","S","I");
  return answer;

//expected 'DUBLON' to equal 'DUBLIN'

}

The replace method doesn’t take multiple replacement strings like that. It takes either a string or a function.

MDN: String.prototype.replace

You can still use replace here but you’ll need to figure out a new way to translate the number to its equivalent letter.

Oh dang!! MDN docs are really hard to comprehend

So, I could do ```
replace(pattern, function {
……
{)

Yes, you can specify a function and use that to determine what value you should replace your matched item with.

I’m still so lost :rofl:. What I am thinking I need to do is make a function that finds each of those numbers and replaces them with the required letters, but I can’t think of how to “get that on paper” lol

I used just if…else logic for this one.
It is a possible way to go, if you struggle with function for replace()

Another one would be to chain several replace(), with each one of them being less complex

wait I THINK I am closer to grasping it here, no?

function correct(string) {
  console.log(string.charAt(0));
  
  for (let i=0; i<string.length; i++) {
    if (string.charAt(i) === 0) {
      string.replace(i, "O");
      console.log(string[i])
    }
  }

Looping through string is good idea imo.
But keep in mind, that strings are immutable.

and this line looks like you are trying to use replace for string mutation

run this, I added couple of logs

that should give you idea what to fix/add

function correct(string) {
  console.log(string.charAt(0));
  
  for (let i=0; i<string.length; i++) {
    if (string.charAt(i) === 0) {
      console.log('we are inside an if block')
      console.log(string)
      string.replace(i, "O");
      console.log(string)
      // console.log(string[i])
    }
  }
  
}
  
console.log(correct('DUBL1N'))

what does it being immutable have to do with it? yes, I’ve already looked up immutable but the definitions are sooooooo abstract on MDN lol

Wait, I think I have to make it mutable, so turn it into an array and loop through that?

so like the below?

let O = string.replace(i, "O")

I THINK that’s what you mean by “string variable”?

Like this?

function correct(string) {
  console.log(string.charAt(0));
  
  for (let i=0; i<string.length; i++) {
    
    let zero = "O"
  let five = 'S'
  let one = 'I'
    if (string.charAt(i) === 0) {
    
      console.log(string[i])
    }
  }

I feel that I am on the right track, but SOMETHING is just not clicking ):

function correct(string) {
  let zero = 'O'
  let five = 'S'
  let one = 'I'
  for (let i = 0; i < string.length; i++) {
    if (string[i] === zero) {
      string[i].replace(i, zero);
      console.log(string)
    }
  }
  }

Tried that and got undefined lol.

function correct(string) {
  let zero = 'O'
  let five = 'S'
  let one = 'I'
  for (let i = 0; i < string.length; i++) {
    if (string[i] === zero) {
      let test= string[i].replace(i, zero);
      console.log(test)
    }
  }
  }

string[i].replace(i, zero);

I made that into a variable.

so return test?
qefwefw

I just don’t understand regex at all and am trying to just do this with a loop, but I’m obviously failing at it lol.

You can do this with a loop . You can use regex to test if a character of the string is one of the 3 letters but you would still need to create a new string. I would suggest forgetting the regex and just use a for loop to iterate through the string. You could use 3 if statements to specify what to do with each of the numbers if they are encountered.

There are definitely other ways that you could use that do not involve looping.

LOL, feel like this one got off track, haha.

First point was you can’t change the values of a string. You can look at them, and use them to create a new string, but you can’t just change the value of the string directly. So correct, you do need to assign the results of a replace to a new variable as it creates a new string.

Second… this: let test= string[i].replace(i, zero);

So replace can search through a string, find something, and then create a new string with those values replaced. BUT… you don’t have it searching through the string, you have it looking at a specific character… string[i] points to the character at positioni… so you are actually trying to do a replace on a single character. But that’s not the only problem. In the replace fuction, you say what you want to replace. You want to replace a 0 with an ‘O’, but you say you want to replace i which contains the index where the char was found.

I would suggest playing with some code in the console or a sandbox. Create a string, then use the [] to look at chars in that string. You should also make a string, and call replace on it a few times and see what the output is. See what you can come up with, and happy coding:)

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.