logo CodeStepByStep logo

reverseFirstK

Language/Type: JavaScript arrays traversals

Write a function named reverseFirstK that accepts an array of integers and an index k as parameters and modifies the first k elements of the array to be in reverse order. For example, if the array a stores [10, -2, 33, 55, 9, 17, 6], the call of reverseFirstK(a, 5) should reverse the first 5 elements, modifying it to store [9, 55, 33, -2, 10, 17, 6]. If the value of k is 1, 0, negative, or greater than the length of the array, do not modify the array.

Function: Write a JavaScript function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.