0

I was given the challenge of converting a string of digits into 'fake binary' on Codewars.com, and I am to convert each individual digit into a 0 or a 1, if the number is less than 5 it should become a 0, and if it's 5 or over it should become a 1. I know how to analyze the whole string's value like so:

function fakeBin(x){
if (x < 5)
return 0;
else return 1;
}

This however, analyzes the value of the whole string, how would I go about analyzing each individual digit within the string rather than the whole thing?

Note: I have already looked at the solutions on the website and don't understand them, I'm not cheating.

Eric Hasegawa
  • 169
  • 1
  • 14

6 Answers6

3

You can do it in one line with two simple global string replacement operations:

function fakeBin(x){
  return ("" + x).replace(/[0-4]/g,'0').replace(/[5-9]/g,'1');
}

console.log(fakeBin(1259))
console.log(fakeBin(7815))
console.log(fakeBin("1234567890"))

The ("" + x) part is just to ensure you have a string to work with, so the function can take numbers or strings as input (as in my example calls above).

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
1

Simple javascript solution to achieve expected solution

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

console.log(fakeBin(357))
divya reddy
  • 139
  • 1
  • 11
  • super ugly code, +1. I truly didn't know it could be done this ugly. –  Mar 04 '17 at 06:30
0

Split the string and apply the current function you have to each element of the string. You can accomplish this with map or with reduce:

function fakeBin(x) {
  
  x = x.split('');
  
  let toBin = x => {
    if (x < 5)
      return 0;
    else return 1
  }
  
  return x.map(toBin).join('');
}

console.log(fakeBin("2351"));

refactored

function fakeBin(x) {
  x = [...x];

  let toBin = x => x < 5 ? 0 : 1;

  return x.map(toBin).join('');
}

console.log(fakeBin("2351"));

reduce

function fakeBin(x) {

  let toBin = x => x < 5 ? 0 : 1;

  return [...x].reduce((acc,val) => acc + toBin(val), "");

}

console.log(fakeBin("23519"));
Jonathan Portorreal
  • 2,730
  • 4
  • 21
  • 38
0

if you are in java you can use

charAt()

and you make a for with the word length and you can check one by one

for(int i = 0; i < text.length(); i++){
yourfunction(texto.charAt(i));
}
0

The snippet below will take a string and return a new string comprised of zeros and/or ones based on what you described.

We use a for ...of loop to traverse the input string and will add a 0 or 1 to our return array based on whether the parsed int if greater or less than 5.

Also note that we are checking and throwing an error if the character is not a number.

const word = "1639";

const stringToBinary = function(str) {
  let ret = [];
  for (const char of word) {
     if (Number.isNaN(parseInt(char, 10))) {
       throw new Error(`${char} is not a digit!`);
     } else {
       const intVal = parseInt(char, 10);
       ret.push(intVal > 5 ? 1 : 0);
     }
  }
  return ret.join('');
};

console.log(stringToBinary(word));
Jordan Bonitatis
  • 1,527
  • 14
  • 12
0

You can use String.prototype.replace() with RegExp /([0-4])|([5-9])/g to match 0-4, 5-9, replace with 0, 1 respectively

let str = "8539734222673566";

let res = str.replace(/([0-4])|([5-9])/g, (_, p1, p2) =>  p1 ? 0 : 1);
 
console.log(res);
guest271314
  • 1
  • 15
  • 104
  • 177