1

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?

Blue
  • 22,608
  • 7
  • 62
  • 92
Marrows
  • 127
  • 1
  • 11
  • When in doubt, add `console.log()`'s everywhere. Additionally, you can put `debugger;` 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. – Blue Aug 12 '18 at 01:40

5 Answers5

1

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:

let word = "hello"

console.log([...word].reverse().join(''))

You can apply this to each element of your array with map():

reverPal = (arr) => arr.map(word => [...word].reverse().join(''))

console.log(reverPal(["hello", "how", "are", "you"]))
Mark
  • 90,562
  • 7
  • 108
  • 148
0

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
bronze man
  • 1,470
  • 2
  • 15
  • 28
  • I want to make a function from strings because from numbers I already did, but thanks, that'll help me. – Marrows Aug 12 '18 at 01:42
0

You could always just use the reverse() function. This function will do the following:

  • map() does a foreach loop, and simply returns the result as the new element in the array.
  • split() converts the indivdual word to an array of letters. hi => ['h', 'i']
  • reverse() reverses the order of the elements. ['h', 'i'] => ['i', 'h']
  • join() takes the elements of the array and converts it back to a string. ['i', 'h'] => ih

const reverPal = (pal) => {
  return pal.map(word => word.split('').reverse().join(''));
}

console.log(reverPal(["hello", "how", "are", "you"]));
Blue
  • 22,608
  • 7
  • 62
  • 92
  • Thank you, and thank you more for your explanation of the functions. Now I can ease up – Marrows Aug 12 '18 at 01: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 '18 at 01: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. – Blue Aug 12 '18 at 01: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 '18 at 01: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. – Blue Aug 12 '18 at 01:58
  • It's ok Marrows, I'll give @gman an upvote, because I think his answer deserves merit, but I'm disappointed that you chose to downvote this answer as unhelpful. – Blue Aug 12 '18 at 02:01
  • @FrankerZ did I ? Sorry, man. I'm new here and I think I made some mistake. I upvote the answer and even give the check, I think when I clicked again something happen – Marrows Aug 12 '18 at 02:03
  • Sorry @Marrows, I should have said *he. You can't upvote or downvote yet. (You can see that on the privileges tab. I was referring to gman's downvote). The only thing that you can do, is mark the answer that best answers your question as accepted. – Blue Aug 12 '18 at 02:04
  • @FrankerZ Ah, thank you again for explain me. Got scared for a moment – Marrows Aug 12 '18 at 02:08
0

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?

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

gman
  • 100,619
  • 31
  • 269
  • 393
0

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"
James
  • 20,957
  • 5
  • 26
  • 41