Nodejs Array Reverse reverseFromLToR(left,right)

Here you can find the source of reverseFromLToR(left,right)

Method Source Code

/**//from ww w .j  ava 2s .com
 * @param {number[]} nums
 * @param {number} k
 * @return {void} Do not return anything, modify nums in-place instead.
 */
 
Array.prototype.reverseFromLToR = function(left,right){
    if(right >= this.length){
        return;
    }

    while(left < right){
        var temp = this[left];
        this[left] = this[right];
        this[right] = temp;
        left++;
        right--;
    }
}

Related

  1. reverse(arr)
    (function() {
    'use strict'
    let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    Array.prototype.reverse = Array.prototype.reverse || function(arr) {
        for(let i = 0; i < arr.length / 2; i++) {
            let opp =  arr.length - i - 1   
            arr[i] += arr[opp]
            arr[opp] = arr[i] - arr[opp]
            arr[i] -= arr[opp]
    ...
    
  2. reverse(fn)
    Array.prototype.reverse = function(fn) {
      for (var i = this.length - 1; i >= 0; i--) fn(this[i]);
    
  3. reverse1()
    Array.prototype.reverse1 = function() {
        var arrLength = this.length;
        for(var i = arrLength-1; i>=0; i--){
            this.push(this[i]);
        this.splice(0,arrLength);
        return this;
    };
    console.log([3,2,1].reverse1());
    ...
    
  4. reverseElements()
    "use strict";
    Array.prototype.reverseElements = function () {
        var temp = this.concat();
        var i = this.length - 1;
        var target = this.length - 1;
        while (i >= 0) {
            this[target - i] = temp[i];
            i--;
        return this;
    
  5. reverseFromLToR(left,right)
    Array.prototype.reverseFromLToR = function(left,right){
        if(right >= this.length){
            return;
        while(left < right){
            var temp = this[left];
            this[left] = this[right];
            this[right] = temp;
            left++;
    ...
    
  6. reversed(array)
    var input = [1, 2, 3, 4];
    input.reverse(); 
    input;           
    Array.prototype.reversed = function(array) {
      var reverse = [];
      for (var i = 0; i<this.length; i++) {
        reverse[i] = this[this.length - i - 1];
      return reverse;
    ...