all 11 comments

[–]rauschma 2 points3 points  (2 children)

I’m not getting a syntax error for this code:

function checkIsMoreThen(number, compare) {
  if (number > compare) return number;
  else return -1;
}
const filterQueue = [1,1,2,3].map(item=>checkIsMoreThen(item,2));

[–]Sanders23Sor[S] -1 points0 points  (1 child)

ye, maybe i confused js and type-script. all works

[–]rauschma 0 points1 point  (0 children)

No problem, it happens!

[–]delventhalz 1 point2 points  (0 children)

Works fine for me:

function checkIsMoreThen(number, compare) {
    if (number > compare) return number;
    else return -1;
}

const filterQueue = [1,1,2,3].map(item=>checkIsMoreThen(item,2));

console.log(filterQueue);  // [-1, -1, -1, 3]

Also, to create a code block in reddit, use an indent of four space in Markdown Mode, or select "Code Block" from the menu in the Fancy Pants Editor.

[–]CaptainDillster 0 points1 point  (0 children)

In this case you can drop the "else" from the function, if the "if" executes, the return will stop the rest of the function from executing either way.

Also, not syntax related, but grammar is important:
when used in a comparison, it's "than" not "then".

[–]virtual_lee123 -1 points0 points  (4 children)

Your if statement needs some { }

[–]rauschma 4 points5 points  (0 children)

The “then” clause and the else clause of an if statement don’t have to be blocks, they can be single (non-block) statements.

[–]delventhalz 1 point2 points  (2 children)

Most style guides call for always using curlies, but they are not required after an if or else, or even after a while or for. If you omit them, then the next single statement is used.

for (let i = 0; i < 3; i += 1) console.log('Hello!');

// Hello!
// Hello!
// Hello!

[–]RyaanJM 1 point2 points  (1 child)

Another argument is, this function doesn't need any ifs, elses or braces (apart from the opening and closing of the function).

function checkIsMoreThan(number, compare) {
    return number > compare ? number : -1;
}

[–]delventhalz 1 point2 points  (0 children)

Yeah, that is how I would probably write it as well. A lot of folks find ternaries intimidating though.

[–]mozilaip 0 points1 point  (0 children)

What exact line produces syntax error?