0
Hello, experience coders, help me with this,
function addOne(str){ return str.split("").filter(function(str){ if (str/1 == str || str/0 == 1) { return 55; } }).join(""); } console.log(addOne("Helloworld0123456")); I am returning 55 to the filter function why is the code giving me the right answer.
6 Respostas
+ 6
You mixed up filter and map
55 has a boolean value of true, so the digits are all kept.
+ 2
there is no return when the character is string
+ 2
filter method does not store the returned value, it stores the array item when the returned value has a boolean value of true.
0
Gordon No, I wanted to use filter only.
If the Boolean is true, it should return all the value, i.e string and numbers, why it is returning only the numbers??
0
Gordon because the if condition inside the callback only check for the number value,
My doubt is inside the callback function I am returning 55, why isn't it is giving me 55 back?
0
Gordon okay thank you.