S
S
Sergey Goryachev2019-09-27 20:21:38
JavaScript
Sergey Goryachev, 2019-09-27 20:21:38

Is it possible to implement conditions in JS in an array?

There is an array of user actions:

karta: [
  ['stop', {}, 1],
  ['step', "up", { x: 1, y: 2 }, 2],
  ['step', "up", { x: 2, y: 1 }, 3],
  ['stop', {}, 4]
],

There is also a variable, let's say lvl, it can be any number.
Depending on the lvl, you need to do one or another step.
Well, this is how I saw it:
karta: [
  ['stop', {}, 1],
  if (lvl == "12") {
    ['step', "up", { x: 1, y: 2 }, 2],
  } else {
    ['step', "up", { x: 1, y: 5 }, 2],
},
  ['step', "up", { x: 2, y: 1 }, 3],
  ['stop', {}, 4]
],

But it doesn't work, of course.
How can such conditions be implemented inside this map?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Tokarchuk, 2018-05-28
@Kewa2008

5b0c16d41064c436269132.png
But remember:

As experienced programmers like to joke, if you have a problem and want to solve it with regular expressions, now you have two problems.

A
Alexey Yarkov, 2019-09-27
@webirus

karta: [
  ['stop', {}, 1],
  ['step', lvl === 12 ? "up" : "down", { x: 1, y: 2 }, 2],
  ['step', "up", { x: 2, y: 1 }, 3],
  ['stop', {}, 4]
],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question