Why isn't this returning the filtered result?

I’m trying to split a string into letters and return it without spaces, which works with variables outside of a function but not inside one. What is the issue here?

function rot13(str) {
    str = str.split('').filter(a => Boolean(a))  
  return str
}

console.log(rot13("SERR CVMMN!"));

I don’t think there is space in your string to split by.

Please take a look here Edit fiddle - JSFiddle - Code Playground
The function returns the array with ' ' while when it’s done using global variables it works

The code you provided has '', not ' '.

The code in your fiddle is unrelated to the code you posted here.

This has nothing to do with global vs local variables.

So ' ' evaluates to true?

A string with a single space in it is truthy, yes.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.