I have an alternate solution for the problem Remove Whitespace from Start and End that seems like it works—my solution returns “Hello, World!” with no spaces according to console.log()
—but it fails the test. And if I test the “Hello, World!” that I get for equality against the “Hello, World!” returned by the official solution, the test returns false. So they look equal, but somehow they are not equal.
Here is what I used to test this, with my solution at the top, the official solution at the bottom, and some console.log()
statements and equality tests built in (you can try a live version at this Repl.it page:
// My solution - fails test
let hello = " Hello, World! ";
let wsRegex = /^(\s*)(.+)(\s*)$/;
let result = hello.replace(wsRegex, "$2");
console.log("my result = " + result) // prints Hello, World!
// FreeCodeCamp solution - passes test
let hello2 = " Hello, World! ";
let wsRegex2 = /^\s+|\s+$/g;
let result2 = hello2.replace(wsRegex2, "");
console.log("FCC result = " + result2) // also prints Hello, World!
console.log("testing loose equality: " + (result == result2)); // returns FALSE - why?
console.log("testing strict equality: " + (result === result2)); // returns FALSE - why?
Can anyone explain what is going on here?