0

The last else if block is not getting executed when the condition is satisfied please help. Thank you.

let rotatingArr = (n, arr, k) => {
  if (n == arr.length && k !== 0 && k <= arr.length) {
    let arr2 = []
    arr
    let delItems = Math.abs(arr.length - k)
    console.log(delItems)
    let modArr = arr.slice().splice(delItems)
    modArr
    arr.splice(-k)
    console.log(arr)
    let result = [...modArr, ...arr];
    return result
  } else if (k == 0) {
    return arr;
  } else if (11 > k > arr.length) {
    k = k - 5
    arr
    let delItems = Math.abs(arr.length - k)
    console.log(delItems)
    let modArr = arr.slice().splice(delItems)
    modArr
    arr.splice(-k)
    console.log(arr)
    let result = [...modArr, ...arr];
    return result
  }
}

console.log(rotatingArr(5, [1, 2, 3, 4, 5], 10))
acdcjunior
  • 132,397
  • 37
  • 331
  • 304
  • 1
    Does this answer your question? [How to check if a number is between two values?](https://stackoverflow.com/questions/14718561/how-to-check-if-a-number-is-between-two-values) – Konrad Oct 22 '22 at 20:13
  • What exactly is this function supposed to do? Hopefully more than just rotating the input array ;-) Now seriously, this is a pretty confusing pile of code and there must be a simpler solution. – Draško Kokić Oct 22 '22 at 20:24

2 Answers2

1

JavaScript does not support that kind of infix operators. Your code is executing (11 > k) > array.length.

OFRBG
  • 1,653
  • 14
  • 28
1

The relational operators (e.g. >) evaluate two values to a boolean.

Therefore, your condition 11 > k > arr.length will be evaluated in two steps:

  1. 11 > k: This will evaluate to either true or false.
  2. (11 > k) > arr.length: This will compare the boolean of the previous expression with arr.length. Because true == 1 and false == 0, this expression will always evaluate to false for arr.length > 1.

You should do these comparisons (11 > k and k > arr.length) in two distinct comparisons, and combine them with a logical operator.

Oskar Grosser
  • 2,804
  • 1
  • 7
  • 18