-2

I was doing a question on codesignal and when I couldn't get the answer myself for all tests, revealed the answers and saw this one; while I understand what it's trying to do (make sure positions are not divisible by n, and if it is increment n by one) I'm having trouble understanding the arrow function syntax and, rewriting it myself from their code.

function obst(inputArray) {
for (var n=1;;n+=1) if(inputArray.every(x=>x%n)) return n;}
dickjct
  • 7
  • 2
  • 1
    it basically says "for each x, return x%n", modifying the response. read more about it at the spec - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions – Ben Yitzhaki Dec 17 '18 at 14:56
  • Can you clarify your question? Do you want help on how to write this as an arrow function? – Dmitriy Dec 17 '18 at 14:56
  • The title of your question is very similar to one of the **bad** examples on the [ask] page. – Ivar Dec 17 '18 at 14:59

3 Answers3

1

In Javascript, every function written like this:

function(args) {
   // DO SOME STUFF
}

can be written like this:

(args) => {// DO SOME STUFF}

In your case, the method .every() expects a function, and

function(x) {
    return x%n;
}

is written as

x => x%n
Bertijn Pauwels
  • 575
  • 1
  • 3
  • 17
0
inputArray.every(x=>x%n) 

is the same as

inputArray.every(function (x){
  return x%n
}))

( except for the way the 'this' keyword works )

Egg
  • 39
  • 1
  • 4
0

For any doubt about Javascript language, i recommend the You Dont Know Js series written by Kyle Simpson.

It's a very enlightening book.

Tip: When you write a code, ask question to youself like:

  • what i'm doing?
  • assigment means equality?
  • what are the methods that can i use in string variables?

And something like that.

Stimulate yourself to know what in the actual fudge, you are doing.

Cheers!.

VadimB
  • 5,533
  • 2
  • 34
  • 48
Yoarthur
  • 909
  • 9
  • 20