My code is outputting the correct numbers but wont pass in FCC

Can someone help me out with the Smallest Common Multiple challenge in Intermediate Algorithm Scripting? My code is outputting the right numbers in repl -, but failing when i submit it to fcc. Is it failing because the for loop is too long?

Nvm i figured it out. Adding //no protect on the first line solved it

I’m surprised that your declaring low and high inside your if/else block didn’t cause a test failure.

1 Like

var low = 0;
var high = 0;
if (arr[0] > arr[1]) {
low = arr[1];
high = arr[0];
} else {
low = arr[0];
high = arr[1];

Would this be better?

Much! Otherwise you can run into issues due both to duplicate declarations and scoping.

1 Like

vars are not block scoped. They recognize only function scope.

Declaring the same variable multiple times in the same scope is graciously forgiven by JavaScript engine, but makes reasoning about code difficult, because when a programmer sees var keyword, they think this is the first time when the variable appears in a given scope, which might not be true. For that reason, liters do not follow JavaScript policy an yell about any duplicate declaration.

1 Like

I thought I remembered that FCC tests would fail on warnings like that. I know that my build environment at work would, but we enforce stricter patterns, so I can’t always remember what are standard.

1 Like

It certainly is a good idea to have linting step included in the build process that rejects any commit with linting errors.

The screenshots you posted show that jshint is definitely not the best linter out there although quite fast. I’d suggest using eslint, or even better, switching to typescript or flow for cases where accuracy is far more important than speed.

1 Like