The task, the function takes a numeric string, if the first number in the string is less than five, replace it with 0, if more than 1.

The result of the work should be:

fakeBin('45385593107843568') --> '01011110001100111' 

Here is my code, I can not figure out why it returns, except for 0 and 1, also the original values. How to fix it ?

 function fakeBin(text){ var result = ''; for (var i = 0; i < text.length; i++) { if (text[i] < 5) { result += 0 + " "; } else { result += 1 + " "; } result += text[i] + " "; } return result.slice(0, result.length-1); } fakeBin('16'); 

    6 answers 6

     function fakeBin(text){ var result = ''; for (var i of text) { if (i < 5) result += 0; else result += 1; } return result; } console.log(fakeBin('01234567890123456789')); 

      result += text[i] + " "; remove this line

        Jedi Path:

         const fakeBin = _ => _.split('').map(_ => +(!(_ < 5))).join(''); console.info(fakeBin('45385593107843568')); 

          In such simple tasks, the old kind recursion looks most elegant -

           const fakeBin = string => string.length ? ( parseFloat(string[0]) < 5 ? "0" : "1" ) + fakeBin( string.substring(1) ) : string; console.log(fakeBin("45385593107843568")); 

          • I agree. simple task. but what happens in the code is not entirely clear to you. very hard to read. - spectre_it
          • add a comment to your code? - spectre_it

          Original values ​​are returned, because you write them yourself

           result += text[i] + " "; 

           function fakeBin(text) { var result = ''; for (var i = 0; i < text.length; i++) { if (text[i] < 5) { result += 0 + " "; } else { result += 1 + " "; } // result += text[i] + " "; } return result.slice(0, result.length - 1); } console.log(fakeBin('16')); 

          An alternative solution might be to use the replace function.

           function fakeBin(text) { return text.replace(/\d/g, $0 => $0 < 5 ? 0 : 1); } console.log(fakeBin('45385593107843568')); 

          In addition, to remove extra space at the end, you can use the trim method

            a few more solutions:

             function fakeBin(x) { return x.split('').map(n => n < 5 ? 0 : 1).join(''); } console.info(fakeBin('45385593107843568')); 

            or

             function fakeBin(x) { return x.replace(/[1234]/g, '0').replace(/[56789]/g, '1') } console.info(fakeBin('45385593107843568')); 

            or

             const fakeBin = x => [...x].reduce((a, b) => a + (~~(+b / 5)), "") console.info(fakeBin('45385593107843568')); 

            • nothing is clear! give exhaustive comments. - user220409
            • Well, there are beginners who have just started to learn js and they don’t understand what all these methods are doing and how the steps handle all the characters. I would have thought that if they do not understand, then this is their problem and that most likely they have not yet grown, and the answer is given to those who can appreciate it. But suddenly I remembered that you have a different thinking and that it will not be difficult for you to describe step by step every step! Otherwise, you are the same Troll. I didn’t come to the last one at the time of writing the first message, but again you helped me with this. - user220409
            • sorted out. I will not persuade you. I will only ask you to explain this piece of code - fakeBin( string.substring(1) ) it is not clear to me what it does. What is its meaning. I really want to figure it out. And instead of helping you, you start to bully and tease me in the comments. - spectre_it
            • one
              in this line, the function is called as the arguments of which the current line is passed without the first letter. - user220409
            • @OlmerDale forgive me if something is wrong .., it is very hard for me to recurse. I can not trace it in your example. Magic in one word. Thank you for your understanding !! - spectre_it