Skip to content
Advertisement

Implementing pseudocode in JavaScript

I’m quite new to JavaScript and I’m trying to implement my code in pseudocode in JavaScript, however I’m not getting the result that I’m supposed to. I want the function to permute the elements of the array p places to the left. In pseudocode I’m using a queue data structure, but I thought I can as well us an array. As the result of my function, I get an array with [2, 2, 2, 2]. Can you please help me out?

My code in pseudocode:

JavaScript

My code in JavaScript:

JavaScript

I did the same in Python and it works fine:

JavaScript

What am I doing wrong with my JavaScript code?

Many thanks!

Advertisement

Answer

In javascript, Array.pop() removes the last element. You need Array.shift() for removing the first one.

JavaScript
Advertisement