1

I'm studying this JS function:

function solution (array, commands) {
     return commands.map (v => {
         return array.slice(v[0] -1, v[1]).sort((a, b) => a - b).slice(v[2] -1, v[2])[0];
     });
}

I do not know what v[0] -1 does.

If commands is a two-dimensional array like this:

[[2,5,4] [4,4,1] [1,7,3]]

What does v[0] mean and what does it mean to do -1 to it like v[0] -1?

1
  • 1
    map iterates through each item in commands. The current iteration/item is v. Therefore, v[0] would refer to the first item in one of child arrays. Commented Nov 6, 2018 at 23:36

4 Answers 4

3

Your code is simply accessing the indexes of each array within commands

[[2,5,4], [4,4,1], [1,7,3]].forEach(arrayV => console.log(arrayV));
.as-console-wrapper { max-height: 100% !important; top: 0; }

v[0] is simply getting the first index of each array

[[2,5,4], [4,4,1], [1,7,3]].forEach(arrayV => console.log(arrayV[0]));
.as-console-wrapper { max-height: 100% !important; top: 0; }

And, v[0] - 1 is only subtracting 1 to first value on array v

[[2,5,4], [4,4,1], [1,7,3]].forEach(arrayV => console.log(arrayV[0] - 1));
.as-console-wrapper { max-height: 100% !important; top: 0; }

The function Array.prototype.map() loops over the array and pass each element to the handler. In your case, is passing arrays from each index.

Sign up to request clarification or add additional context in comments.

Comments

2

If commands is [[2,5,4],[4,4,1],[1,7,3]]

then in the context of commands.map( v => {}), v[0] is not an array; it's one of the numbers. map() will take each sub array and assign it to v, so when you access v[0] you are getting the first element of the subarray:

let commands=[[2,5,4],[4,4,1],[1,7,3]]
let m  = commands.map(v => {
    console.log("v[0]:", v[0])
    // we don't know what array is so not sure what to return
    //return array.slice(v[0] -1, v[1]).sort((a, b) => a - b).slice(v[2] -1, v[2])[0];
    return "some value"
});

Comments

2

What it means is this:

v[0]

This gets the first element from the array called v.

-1

This takes 1 away from the first element in the array v.

Comments

1

Elaborating on Jack's answer, I believe that the OP's confusion arises from overlooking the influence of the map function, viz:

  • The map() method creates a new array with the results of calling a provided function on every element in the calling array.

So, the function in question will be called three times: first with [2,5,4], then [4,4,1] and so on. And so, realizing this, the code snippet as-written now makes sense: v[0]-1 is equal to 1 the first time, and so on.

  • Now, having said all that – "I hate to encounter code like this," although of course I encounter it a lot as we all do. Not only because it is fragile, but also because it makes me feel like I've stumbled upon an episode of Name That Tune where the original-programmer is trying to minimize keystrokes for no particular reason. I dislike having to "puzzle out" how a piece of source-code works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.