How to Reverse an Array in JavaScript Without Using Libraries

How can I reverse an array in JavaScript without using libraries?

Javascript has a reverse() method that you can call in an array

var a = [3,5,7,8];
a.reverse(); // 8 7 5 3

Not sure if that's what you mean by 'libraries you can't use', I'm guessing something to do with practice. If that's the case, you can implement your own version of .reverse()

function reverseArr(input) {
var ret = new Array;
for(var i = input.length-1; i >= 0; i--) {
ret.push(input[i]);
}
return ret;
}

var a = [3,5,7,8]
var b = reverseArr(a);

Do note that the built-in .reverse() method operates on the original array, thus you don't need to reassign a.

Reverse array in Javascript without mutating original array

You can use slice() to make a copy then reverse() it

var newarray = array.slice().reverse();

var array = ['a', 'b', 'c', 'd', 'e'];var newarray = array.slice().reverse();
console.log('a', array);console.log('na', newarray);

How can I reverse an array in JavaScript without using libraries?

Javascript has a reverse() method that you can call in an array

var a = [3,5,7,8];
a.reverse(); // 8 7 5 3

Not sure if that's what you mean by 'libraries you can't use', I'm guessing something to do with practice. If that's the case, you can implement your own version of .reverse()

function reverseArr(input) {
var ret = new Array;
for(var i = input.length-1; i >= 0; i--) {
ret.push(input[i]);
}
return ret;
}

var a = [3,5,7,8]
var b = reverseArr(a);

Do note that the built-in .reverse() method operates on the original array, thus you don't need to reassign a.

How to reverse an array in JavaScript without Array.prototype.reverse?

the problem is you're modifying the array at the same time you look into it, so when you get to the middle element it was already modified.

here's a manual execution of your script :

i | tab
0 | [1,2,3,4,5,6,7,8,9,10]
1 | [10,2,3,4,5,6,7,8,9,10]
2 | [10,9,3,4,5,6,7,8,9,10]
3 | [10,9,8,4,5,6,7,8,9,10]
4 | [10,9,8,7,5,6,7,8,9,10]
5 | [10,9,8,7,6,6,7,8,9,10]
6 | [10,9,8,7,6,6,7,8,9,10]
7 | ...
8 | ...
9 | ...

I think you can see the problem there

there are 2 ways to solve this problem:

// either create a temp array to keep previous valuesconst reverseWithTmpArray = arr => {  let tmp = [...arr]
for(let i = 0; i < arr.length; i++){ let num = (arr.length - i) - 1 arr[i] = tmp[num] }
return arr}
// or reverse the element by pairs const reverseByPairs = arr => { for(let i = 0; i < arr.length / 2; i++){ let num = (arr.length - i) - 1
let tmp = arr[i] arr[i] = arr[num] arr[num] = tmp
// can also be written // [arr[i], arr[num]] = [arr[num], arr[i]] // using destructuring assignement }
return arr}
console.log(reverseWithTmpArray([1,2,3,4,5,6,7,8,9,10]))console.log(reverseByPairs([1,2,3,4,5,6,7,8,9,10]))

Log reverse an array

In order to get this output:

JavaScript
CSS
HTML

Given the following input (Your array):

['HTML', 'CSS', 'JavaScript']

You must, reverse the original array, then loop through it calling console.log.

Try the following function:

function logBackwards(arr) {
// Reverse the input array
const reversed = arr.reverse();

// Log each item
reversed.forEach((element) => {
console.log(element);
});
}

logBackwards(['HTML', 'CSS', 'JavaScript']);

If you want to avoid using functions, a loop approach is also available.

function logBackwards(arr) {
const lastItem = arr.length - 1;

for (let i = lastItem; i >= 0; i--) {
console.log(arr[i]);
}
}

logBackwards(['HTML', 'CSS', 'JavaScript']);

Hope it helps, good luck!

Reverse a string in javascript without using any inbuilt function

Create a new string and add all the chars from the original string to it backwards:

function reverse1(str){
var r = "";
for(var i = str.length - 1; i >= 0; i--){
r += str.charAt(i);
}
return r;
}

Then just say:

str = reverse1(str);


Related Topics



Leave a reply



Submit