0

In this custom code we have created a range from 150 to 210 which became an array. Afterwards we wanted to check whether a specific value is included in the previously created array but even though the value is clearly in the created array the includes() returns false. I cannot figure out why this is the case, could you please help me out with this?

function range(start, end, step = 1) {
    const len = Math.floor((end - start) / step) + 1
    return Array(len).fill().map((_, idx) => start + (idx * step))
  }
let sliderInterval = [];
sliderInterval.push(range(180 - 30,180 + 30,5))
console.log(sliderInterval.includes(180));
Vanda Nagy
  • 21
  • 4
  • 1
    No, `sliderInterval` only contains one array. `180` is not an array. Did you mean `sliderInterval.push(...range(180 - 30,180 + 30,5))`? – Sebastian Simon Nov 23 '22 at 20:44
  • `range()` returns an array, as you say; why do you `.push()` the array onto *another* array? – Pointy Nov 23 '22 at 20:47
  • You can do `let sliderInterval = [...range(180 - 30,180 + 30,5)];` instead of `push()` – Asraf Nov 23 '22 at 20:48
  • When i put the ... to my code it gave the right answer, thank you for that! I'm just starting with JS and in Python that two should be the same. I mean in Python these two are the same result wise so i assumed this is a correct way in JS too. A = np.array([np.array([1,2,3]), np.array([4,5,6])]) B = np.array([[1,2,3], [4,5,6]]) But thank you again, problem got fixed! :) – Vanda Nagy Nov 23 '22 at 20:53
  • 1
    @VandaNagy Numpy arrays are _very_ different from JavaScript arrays; the way operators work in Python is also very different. – Sebastian Simon Nov 23 '22 at 20:56
  • 1
    that's not Python itself making that work the way you're used to, but the Numpy library. The Python language equivalent to your JS question is the [difference between extend and append](https://stackoverflow.com/questions/252703/what-is-the-difference-between-pythons-list-methods-append-and-extend) – Robin Zigmond Nov 23 '22 at 20:56

0 Answers0