reverse array in specific places in javascript

I had an interview question that stumped me earlier today. Aside from trying to use which wasn’t doing what I wanted, it didn’t work the same way that my own interpreter was working.

Anyways, I totally bombed on the question, but I didn’t give up on the challenge. The challenge required 3 inputs, an array of letters, a min, and max parameters. The goal was to reverse the strings inbetween the min and max indexes.

When these types of interview questions pop up its important to walk through the steps. I’m not going to modify my existing array, so.

1.) create new array.
2.) loop through and push the items lower than the min index into the new array
3.) push the indexes higher than the max into the array

This is where it became a little tricky for me. Just getting use to the syntax was one thing. At first i was comparing the wrong min max values.
x === a[x], nope.

I’m sure there’s another way to do it that’s better but what i ended up doing was created a separate array called c. Then using the reverse function. So I had two separate arrays. I then used the splice,apply,concat methods to insert the new reversed array into the min position and pushing it inbetween the max.

If you know of another way, please mention in the comments section below.

var a = ['A','B','C','D','E','F'];

function reverse_array(a, i, j){
var b = [];
var c = [];
for (var x=0; x < a.length ; x++){
if (x < i){ b.push(a[x]); } else if (x > j){
else if (i > x < j){
// console.log(a);
// console.log(c);
d = c.reverse();
return b;

[ 'A', 'E', 'D', 'C', 'B', 'F' ]
[Finished in 0.1s]

Leave a Reply

Your email address will not be published. Required fields are marked *