Make a function in javascript that returns the array and elements in reverse order
Clash Royale CLAN TAG#URR8PPP
Make a function in javascript that returns the array and elements in reverse order
As I said in the title, I want to make a function that receives an array and return all the elements in reverse order. I studied a lit bit of javascript, but this is bugging me for some time. I made two functions to make it clear, as even my friends didn't understand the concept, the second is the one who is bugging me.
const reverse = (pal) =>
var aux = "";
for(var i in pal)
aux = pal[i] + aux;
return aux;
console.log(reverse("are")); //=> returns "era"
This function above works fine and returns only one word, the next needs to return the array and all the words in reverse (just in case if someone didn't understand)
const reverPal = (pal) =>
let aux = "";
let aux1 = "";
for (var i in pal)
aux += pal[i]
for (var i in aux.length) //I think the problem starts here
aux.forEach((word) =>
word.reverse();
aux1 += word;
)
return aux1;
console.log(reverPal(["hello", "how", "are", "you"]));
//should return = "olleh", "woh", "era", "ouy"
//but returns nothing in this code
Can someone help me?
console.log()
debugger;
5 Answers
5
You could always just use the reverse()
function. This function will do the following:
reverse()
hi
['h', 'i']
['h', 'i']
['i', 'h']
['i', 'h']
ih
const reverPal = (pal) =>
return pal.map(word => word.split('').reverse().join(''));
console.log(reverPal(["hello", "how", "are", "you"]));
Thank you, and thank you more for your explanation of the functions. Now I can ease up
– Marrows
Aug 12 at 1:41
This is NOT an answer to the question. The question is "how do I write a function to reverse things". The question is not "how do I reverse things". One question is how to get things done. The other question (the actual question) is more along the lines of "how do I write this myself without using the built in solution"
– gman
Aug 12 at 1:53
@gman I disagree, "As I said in the title, I want to make a function that receives an array and return all the elements in reverse order.". This simplifies what OP was trying to do, and accomplishes the goal that she set out.
– FrankerZ
Aug 12 at 1:56
He asked for debugging help with his function. He did not ask you to write a new function. That's very clear from his question.
– gman
Aug 12 at 1:57
@gman Can you ask where he specifically mentioned not to write a new function? If there are built-ins to javascript that perform the functionality, why is this not a viable solution. I agree, your solution goes into a bit more depth of why his functions weren't working correctly, but to say that this isn't an answer, is a far-cry.
– FrankerZ
Aug 12 at 1:58
An easy, functional, way to reverse a string is to spread it into an array and call reverse()
on it and the join()
it back up:
reverse()
join()
let word = "hello"
console.log([...word].reverse().join(''))
You can apply this to each element of your array with map()
:
map()
reverPal = (arr) => arr.map(word => [...word].reverse().join(''))
console.log(reverPal(["hello", "how", "are", "you"]))
Nice alternate. I like the ES6 syntax.
– FrankerZ
Aug 12 at 1:41
Glad it helped @Marrows, good luck with the project!
– Mark Meyer
Aug 12 at 1:41
Thx, @MarkMeyer
– Marrows
Aug 12 at 1:55
You're not making an array in your second function
const reverPal = (pal) =>
let aux = "";
let aux1 = "";
for (var i in pal)
aux += pal[i]
for (var i in aux.length) //I think the problem starts here
aux.forEach((word) =>
word.reverse();
aux1 += word;
)
return aux1;
//console.log(reverPal(["hello", "how", "are", "you"]);
//should return = "olleh", "woh", "era", "ouy"
//but returns nothing in this code
You want reverPal
to return an array but where are you creating the array?
reverPal
It seems like you want to just use your first function in your second
const reverse = (pal) =>
var aux = "";
for(var i in pal)
aux = pal[i] + aux;
return aux;
const reverPal = (pal) =>
let aux1 = ;
for (var i in pal)
aux1.push(reverse(pal[i]));
return aux1;
;
console.log(reverPal(["hello", "how", "are", "you"]));
//should return = "olleh", "woh", "era", "ouy"
//but returns nothing in this code
Or if you wanted to not use the first in the second then just paste the first inside the second
const reverPal = (pal) =>
let aux1 = ;
for (var i in pal)
var pal1 = pal[i];
var aux = "";
for(var i in pal1)
aux = pal1[i] + aux;
aux1.push(aux);
return aux1;
;
console.log(reverPal(["hello", "how", "are", "you"]));
//should return = "olleh", "woh", "era", "ouy"
//but returns nothing in this code
All that said there's a few things you should probably get to know about JavaScript
Don't use for(elem in array)
in JavaScript.
for(elem in array)
see: Why is using "for...in" with array iteration a bad idea?
Consider never using var
ever
var
Now that const
and let
both exist there is never a reason to usevar
. var
has a host of issues that both const
and let
solve.
const
let
var
var
const
let
See: https://hackernoon.com/why-you-shouldnt-use-var-anymore-f109a58b9b70
If you're just looking for a solution rather than your actual question of "making a function" others have posted more idiomatic solutions.
Now I see my mistake, thank you for the tip.
– Marrows
Aug 12 at 1:54
I want to make a function that receives an array and return all the elements in reverse order.
If you want reverse array element order:
function reverseArr(arr)
var outArr = ;
for (var i = arr.length-1;i>=0;i--)
outArr.push(arr[i]);
return outArr;
console.log(reverseArr([1,2,3]));
// [3, 2, 1]
If you want reverse characters in a string:
function reverseString(s)
var outS = "";
for (var i = s.length-1;i>=0;i--)
outS += s[i];
return outS;
console.log(reverseString("abc中"));
// 中cba
I want to make a function from strings because from numbers I already did, but thanks, that'll help me.
– Marrows
Aug 12 at 1:42
Since you already have a working function that outputs a reversed word, you can just pass that function into Array.map.
const reverse = (pal) =>
var aux = "";
for(var i in pal)
aux = pal[i] + aux;
return aux;
const reverPal = (arr) =>
return arr.map(reverse);
console.log(reverse("hello"));
console.log(reverPal(["hello", "how", "are", "you"]));
//should return = "olleh", "woh", "era", "ouy"
Thank you, now I see that all that coding wasn't necessary
– Marrows
Aug 12 at 1:58
A good thing to learn is how to reuse your code!
– James
Aug 12 at 2:00
Yeah, I gonna start doing it now !
– Marrows
Aug 12 at 2:01
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
When in doubt, add
console.log()
's everywhere. Additionally, you can putdebugger;
and it will actually pause the code in the browser, and you can see each individual loop, and step through the code line by line to see where the issue lies.– FrankerZ
Aug 12 at 1:40