正如标题中所述,我想创建一个函数来接收数组并以相反的顺序返回所有元素。我学习了一小段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
有人可以帮我吗?
答案 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()
函数。此功能将执行以下操作:
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"]));
答案 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的几件事
请勿在JavaScript中使用for(elem in array)
。
请参阅:Why is using "for...in" with array iteration a bad idea?
考虑永远不要使用var
现在const
和let
都存在,因此永远没有理由使用
var
。 var
有许多const
和let
都可以解决的问题。
请参阅:https://hackernoon.com/why-you-shouldnt-use-var-anymore-f109a58b9b70
如果您只是在寻找解决方案,而不是实际的“实现功能”问题,其他人会发布更多惯用的解决方案。
答案 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"