Fake Binary

Lee·2022년 7월 28일

Algorithm

목록 보기
60/92
post-thumbnail

❓ Fake Binary

Q. 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.

Note: input will never be an empty string

✔ Solution

//#my solution
function fakeBin(x) {
  return x
    .split("")
    .map((num) => (num < 5 ? 0 : 1))
    .join("");
}

//other solution
function fakeBin(x) {
  return x.replace(/\d/g, d => d < 5 ? 0 : 1);
}
profile
Lee

0개의 댓글