8 kyu Fake Binary

Hello,

I’m trying to complete an 8th kyu challenge called ‘Fake Binary’ and I’m having trouble with the question. My results keep coming up as undefined, or the same number prints.
So at least the for loop works, lol.
Here is the description:

Given a string of digits, you should replace any digit below 5 with ‘0’ and any digit 5 and above with ‘1’. Return the resulting string.

Here’s my code:

function fakeBin(x){
  let newStr = x; 
  for ( var i = 0; i < newStr.length; i++ ){
        if ( newStr[i] < 5 ){
          newStr[i] = 0;
        }else if ( newStr[i] > 5 ){
          newStr[i] = 1;
        }
   return newStr;
}

}

I tried a few string methods that didn’t work. I would really appreciate some guidance.

Ty

Hello~!

Strings are immutable. You cannot assign a new value to newStr[i] directly.

As a hint… you CAN assign a new value to array[i] for example

1 Like

Hey there,

great work so far.

  1. As nhcarrigan already said, you want to assign 0 or 1 to your existing string. Strings are immutable. There are other data structures you can use to solve this.

  2. The text says any digit 5 and above with ‘1’. You are currently not doing this properly in your code.

Keep us posted!

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Hey Guys,

I’ve made some progress with this code.

I’m working on trying to take the commas out of the new string. I tried slice, split and join. Haven’t had luck yet.
EDIT : I FIGURED IT OUT. I won’t be posting the solution cause… ya know.
Thanks for all the responses, appreciate ya’ll <3

1 Like