Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function reverseInPlace(input) {
- for (let i = 0; i < input.length / 2; i++) {
- let firstLetter = input[i]; // i = 0 a; i = 1 b; i = 2 c; i = 3 d; i = 4 e
- let previousIndex = input.length - 1 - i; // 4; 3; 2; 1; 0
- input[i] = input[previousIndex]; // a = 4; b = 3; c = 2; d = 1; e = 0
- input[previousIndex] = firstLetter; // 4 = a; 3 = b; 2 = c; 1 = d; 0 = e
- }
- console.log(input.join(' '));
- }
Advertisement
Comments
-
- The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string.
Add Comment
Please, Sign In to add comment