在javascript中创建一个函数,以相反的顺序返回数组和元素

时间:2018-08-12 01:20:06

标签: javascript arrays foreach reverse

正如标题中所述,我想创建一个函数来接收数组并以相反的顺序返回所有元素。我学习了一小段javascript,但是这困扰了我一段时间。我做了两个明确的功能,因为即使我的朋友也不了解这个概念,第二个功能就是困扰我的人。

const reverse = (pal) => {
  var aux = "";
  for(var i in pal){
      aux = pal[i] + aux;
  }
  return aux;
}
console.log(reverse("are")); //=> returns "era"

上面的这个函数工作正常,仅返回一个单词,下一个需要返回数组和所有单词反向(以防万一有人不理解的话)

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  

有人可以帮我吗?

5 个答案:

答案 0 :(得分:1)

一种简单,实用的反转字符串的方法是将其散布到数组中,并在其上调用reverse()并对其进行备份join()

let word = "hello"

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

您可以使用map()将其应用于数组的每个元素:

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

console.log(reverPal(["hello", "how", "are", "you"]))

答案 1 :(得分:0)

  

我想创建一个接收数组并以相反顺序返回所有元素的函数。

如果要反转数组元素顺序:

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]

如果要在字符串中倒置字符:

function reverseString(s){
   var outS = "";
    for (var i = s.length-1;i>=0;i--){
        outS += s[i];
    }
    return outS;
}
console.log(reverseString("abc中"));
// 中cba

答案 2 :(得分:0)

您始终可以只使用reverse()函数。此功能将执行以下操作:

  • map()进行foreach循环,并简单地将结果作为数组中的新元素返回。
  • split()将单个单词转换为字母数组。 hi => ['h', 'i']
  • reverse()反转元素的顺序。 ['h', 'i'] => ['i', 'h']
  • join()接受数组的元素并将其转换回字符串。 ['i', 'h'] => ih

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

console.log(reverPal(["hello", "how", "are", "you"]));

答案 3 :(得分:0)

您没有在第二个函数中创建数组

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   

您希望reverPal返回一个数组,但是要在哪里创建该数组?

似乎您只想在第二个功能中使用第一个功能

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

或者如果您不想在第二个中使用第一个,则只需将第一个粘贴在第二个中

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

所有这些都说明了您可能应该了解JavaScript的几件事

答案 4 :(得分:0)

由于您已经具有一个输出反向单词的有效函数,因此您只需将该函数传递给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"