trim() only removes whitespaces from the start and end of a string:
https://www.w3schools.com/Jsref/jsref_trim_string.asp
have a look here to remove whitespaces between strings:
Replace all whitespace characters
the relevant part is to use it like:
str = str.replace(/\s/g, "X");