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.

18th Oct 2020, 1:54 PM
Lucifer
Lucifer - avatar
6 ответов
+ 6
You mixed up filter and map 55 has a boolean value of true, so the digits are all kept.
18th Oct 2020, 2:21 PM
Gordon
Gordon - avatar
+ 2
there is no return when the character is string
20th Oct 2020, 6:38 AM
Gordon
Gordon - avatar
+ 2
filter method does not store the returned value, it stores the array item when the returned value has a boolean value of true.
20th Oct 2020, 7:13 AM
Gordon
Gordon - avatar
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??
18th Oct 2020, 3:53 PM
Lucifer
Lucifer - avatar
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?
20th Oct 2020, 6:55 AM
Lucifer
Lucifer - avatar
0
Gordon okay thank you.
20th Oct 2020, 7:15 AM
Lucifer
Lucifer - avatar