-4

I want to exchange the first value for the last, the second with the last but one, and so on.

Like this:

Array = [0,1,2,3,4,5,6]

Array[0] --> Array[7] --> Array = [6,1,2,3,4,5,0]

I tried to make this, but it didn't work, it only makes the array blank:

var vetor = [],temp=0,b=vetor.length-1;

for (var i = 0;i<20;i++) {
    vetor[i] = Number(prompt("Digite um número: " + i));
    temp = vetor[i];
    vetor[i] = vetor[b];
    b--;
    console.log(vetor[b]);
}
alert(vetor);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • I'm Brazilian btw :) – jojonso_ Aug 21 '23 at 21:35
  • You mean [`Array#reverse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse)/[`#toReversed()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toReversed)? – InSync Aug 21 '23 at 21:40
  • No like: 1: Array[0,1,2,3,4,5,6] = [6,1,2,3,4,5,0] 2: Array[6,1,2,3,4,5,0] = [6,5,2,3,4,1,0]... – jojonso_ Aug 21 '23 at 21:46
  • 2
    Wouldn't that eventually lead to `[6, 5, 4, 3, 2, 1, 0]` which is the original reversed? – InSync Aug 21 '23 at 21:48
  • The description suggests *reversing* the elements of the array. The example suggests *rotation* of the array. Which one? – Peter Mortensen Aug 22 '23 at 05:54
  • For the former, some duplicate targets are *[Reverse an array in JavaScript without mutating the original array](https://stackoverflow.com/questions/30610523/reverse-an-array-in-javascript-without-mutating-the-original-array)* and *[What is the most efficient way to reverse an array in JavaScript?](https://stackoverflow.com/questions/5276953/what-is-the-most-efficient-way-to-reverse-an-array-in-javascript)*. – Peter Mortensen Aug 22 '23 at 07:00
  • For the latter, there is *[Rotate the elements in an array in JavaScript](https://stackoverflow.com/questions/1985260/rotate-the-elements-in-an-array-in-javascript)*. – Peter Mortensen Aug 22 '23 at 07:03
  • Though if it is homework, there may be further restrictions, like using built-in functions in forbidden. – Peter Mortensen Aug 22 '23 at 07:05
  • For rotation, left rotation as in this question: *[Rotate the elements of an array to the left](https://stackoverflow.com/questions/62392340)* – Peter Mortensen Aug 22 '23 at 07:18
  • OK, it is probably reversing the array. – Peter Mortensen Aug 22 '23 at 07:27
  • So the real answer is probably related to why `temp` is 'assigned' (OK, may be a faux pas), but never used. – Peter Mortensen Aug 22 '23 at 07:31
  • Allegedly, [Visual Studio Code](https://en.wikipedia.org/wiki/Visual_Studio_Code) will [give a warning](https://stackoverflow.com/questions/62490233/javascript-variable-declared-but-never-used-even-though-im-using-it). – Peter Mortensen Aug 22 '23 at 16:15
  • A linter, like [ESLint, as well](https://stackoverflow.com/questions/45399923/eslint-disable-warning-defined-but-never-used-for-specific-function/57932893#57932893). – Peter Mortensen Aug 22 '23 at 16:18

2 Answers2

0

The function works by creating a temp var to store the first element of the array - array[0]. Then it assigns the value of the last element to the first element and assigns the value of the temporary variable to the last element.

function swap(array) {
  var temp = array[0];
  array[0] = array[array.length - 1];
  array[array.length - 1] = temp;
  return array;
}

const array = [0, 1, 2, 3, 4, 5, 6];
console.log(swap(array));

Reference: length

Serenity
  • 51
  • 3
0

I just created a function with parameters (array and an iteration). For example, if you want to change the second and sixth element you must change the iteration as 1.

Example:

The array is [0, 1, 2, 3, 4, 5, 6] changeArray(array, 0) will change the 0 index and 6 index element.

The array is [6, 1, 2, 3, 4, 5, 0] changeArray(array, 1) will change the 1 index and 5 index element, so it will be like [6, 5, 2, 3, 4, 1, 0]

I added a constraint, because you can not divide forever. I calculated how many iterations will be changed. If the user adds any iteration which one is invalid, then it will return the array itself.

 const changeArray = (array, iteration) => {
    const length = array.length;

    if(Math.floor(length / 2) < iteration) return array;

    let values = {
      first: {
        index: iteration,
        value: array[iteration]
      },
      last: {
        index: length - iteration - 1,
        value: array[length - iteration - 1]
      }
    }

    array[values.last.index] = values.first.value;
    array[values.first.index] = values.last.value;

    return array;
  };

  const array = [6, 1, 2, 3, 4, 5, 0];
  console.log(changeArray(array, 1))
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131