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

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

#1

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


#2

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


#3

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


#4

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?


#5

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


#6

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.


#7


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.


#8

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.