Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array Left Rotation in Javascript will log to console but not return

I'm working on the array left rotation on Hackerrank. The solution that I have will console.log the array containing the correct result, but will not work using return. Here's the detail from their site - "Print a single line of n space-separated integers denoting the final state of the array after performing d left rotations." I've read that the issue might be with asynchronous functions running in node.js, but I'm not sure how to work around that.

// sample input - 1 2 3 4 5
// sample output - 5 1 2 3 4

function rotLeft(a, d) {
  var arr = [];

    for (var i = 1; i <= a; i++){
      arr.push(i)
    };
    for (var j = 1; j <= d; j++){
    	arr.shift(arr.push(j))
    }
    console.log(arr.toString()); // <-- this will print the desired output.
    return arr.toString(); // <-- no return from this.
}


rotLeft(5, 4)
like image 589
Justin Cefai Avatar asked Jul 06 '18 23:07

Justin Cefai


People also ask

How do I rotate an array to the left?

The array can be left rotated by shifting its elements to a position prior to them which can be accomplished by looping through the array and perform the operation arr[j] = arr[j+1]. The first element of the array will be added to the last of rotated array.

Which algorithm is best for array rotation?

The block swap algorithm for array rotation is an efficient algorithm that is used for array rotation.

Can JavaScript return an array?

Summary. JavaScript doesn't support functions that return multiple values. However, you can wrap multiple values into an array or an object and return the array or the object. Use destructuring assignment syntax to unpack values from the array, or properties from objects.


2 Answers

I have also been trying to solve this problem. There are some issues in your current solution. See you have to create an array and store the array passed from parameters in it. What you have done is just creating a new array and adding the sequence of numbers of elements that should be in it e.g a.length=5 you are doing arr.push 1 2 3 4 5. But the question wants an array of user's choice. Second thing is that you should return an array not the string.

So this is the solution of this problem:

function rotLeft(a, d) {
    var arr = [];
    for (var i = 0; i < a.length; i++) {
        arr.push(a[i]);
    };
    for (var j = 1; j <= d; j++) {
        arr.shift(arr.push(arr[0]));
    }
    return arr;
}
like image 118
Asad Ali Baloch Avatar answered Oct 05 '22 11:10

Asad Ali Baloch


I'll give my solution to this problem for future visitors.

you can do this in one line as follows :

function rotLeft(a, d)
 {
    let rslt = a.slice(d).concat(a.slice(0,d));
    return rslt
 }

You need to split the initial array at the d'th position in two parts, and simply swap those two.

The slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included). The original array will not be modified.

var animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];

console.log(animals.slice(2));
// expected output: Array ["camel", "duck", "elephant"]

console.log(animals.slice(2, 4));
// expected output: Array ["camel", "duck"]

console.log(animals.slice(1, 5));
// expected output: Array ["bison", "camel", "duck", "elephant"]
like image 36
Fred Avatar answered Oct 05 '22 13:10

Fred