最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Javascript Exercise - Reverse 2 dimensional array - Stack Overflow

matteradmin8PV0评论

Reverse the values of a 2 dimensional array that could extend n times.

[1, [2, [3, ... [n, null]]]]

Given:

  1. All arrays always have a length of 2
  2. Last array in the list will contain an index 1 of null

Example:

  • [1, [2, [3, null]]] will output [3, [2, [1, null]]]
  • [1, [2, [3, [4, null]]]] would output [4, [3, [2, [1, null]]]]

I'm not sure if I'm describing it right but I came across this exercise today and came up with a fairly obvious solution.

var ars = [1, [2, [3, null]]], rev = null;

function r(x) {
    rev = (rev == null) ? [x[0]] : [x[0], rev];
    if( x[1] !== null )
        r(x[1]);
}
r(ars);
console.log( rev );

/

I am by no means a javascript expert, so I was wondering if there was a better way to do it?

Reverse the values of a 2 dimensional array that could extend n times.

[1, [2, [3, ... [n, null]]]]

Given:

  1. All arrays always have a length of 2
  2. Last array in the list will contain an index 1 of null

Example:

  • [1, [2, [3, null]]] will output [3, [2, [1, null]]]
  • [1, [2, [3, [4, null]]]] would output [4, [3, [2, [1, null]]]]

I'm not sure if I'm describing it right but I came across this exercise today and came up with a fairly obvious solution.

var ars = [1, [2, [3, null]]], rev = null;

function r(x) {
    rev = (rev == null) ? [x[0]] : [x[0], rev];
    if( x[1] !== null )
        r(x[1]);
}
r(ars);
console.log( rev );

http://jsfiddle/5b4xntwg/

I am by no means a javascript expert, so I was wondering if there was a better way to do it?

Share Improve this question edited Aug 27, 2014 at 17:25 gen_Eric 227k42 gold badges303 silver badges342 bronze badges asked Aug 27, 2014 at 17:24 rginrgin 2,3114 gold badges24 silver badges33 bronze badges 7
  • 1 Does the array always end in a null? Also, your example seems to leave that out of the final result. – gen_Eric Commented Aug 27, 2014 at 17:27
  • 1 I'm not sure that "2D" and "reverse" are quite the right terms here. This is a nested array and you are, for want of a better term, turning it inside out. – Matt Burland Commented Aug 27, 2014 at 17:31
  • This question appears to be off-topic because it belongs on codereview.stackexchange. – Matt Burland Commented Aug 27, 2014 at 17:33
  • @RocketHazmat- The bottom array should always end with null. Something that I missed, apparently. – rgin Commented Aug 27, 2014 at 17:33
  • @MattBurland - Yeah, I suppose you're right. Turning it inside out is more appropriate. But like I said, I really just came across this exercise today and don't really know how to describe it. (it was shown to me by a co-worker). – rgin Commented Aug 27, 2014 at 17:34
 |  Show 2 more ments

1 Answer 1

Reset to default 8

Here's a more concise approach that doesn't have side-effects:

function r(arr, acc) {
    acc = acc || null;
    return arr ? r(arr[1], [arr[0], acc]) : acc;
}

http://jsfiddle/5b4xntwg/1/

It goes through the following recursive calls for the input [1, [2, [3, null]]]:

r([1, [2, [3, null]]]                     )
r([2, [3, null]]     , [1, null]          )
r([3, null]          , [2, [1, null]]     )
r(null               , [3, [2, [1, null]]])

On the last call, arr is null (this is the base case), so it just returns acc, which has the value [3, [2, [1, null]]].

One thing worth mentioning is that this nested array structure is basically a cons list, which is used extensively in functional programming and is very conducive to recursive operations.

Lastly, here's an iterative version:

function r(arr) {
    var acc = null;
    while (arr) { 
        acc = [arr[0], acc]; 
        arr = arr[1]; 
    }
    return acc;
}
Post a comment

comment list (0)

  1. No comments so far