| Viewed 149 times

Convert "Zero" and "One" to 0 and 1 in javaScript?


Create a function that takes a string as an argument. The function must return a string containing 1s and 0s based on the string argument's words

1 Answer
Mohammad Qandeel
Answered
22

ok, it's very easy to Convert "Zero" and "One" to 0 and 1 in javaScript

look to this function 

const textToNumberBinary = str => {
  const cleanStr = str
   .replace(/zero/gi, '0')
   .replace(/one/gi, '1')
    .replace(/[^01]/g, '');
  
  const strLength = Math.floor(cleanStr.length / 8) * 8;
  
  return cleanStr.slice(0, strLength);
}


Related Questions