不使用.reverse()反转字符串的单词(不是字母)

时间:2015-08-28 04:39:51

标签: javascript arrays string

我正在处理的问题要求我编写一个函数reverseMessage(),它接受​​一个字符串消息并反转现有单词的顺序。

我首先提出的答案是作品,但显然不符合此任务的requirements

function reverseMessage(string) {
    return string.split(" ").reverse().join(" ");
}

例如:

var string = "reversed are string your in words the now"
var results = "now the words in your string are reversed"

新解决方案:

我想将这个字符串拆分成一个数组,然后循环遍历该数组并将第一个索引与最后一个索引交换,第二个索引使用倒数第二个索引,第三个索引使用倒数第三个索引,等等。直到我在中间见面。

我正在努力使以下两个不同的功能起作用:

 function reverseMessage(string) {
     var array = string.split(" ");
     //loop through array and replace first index with last index, second   
     index with second to last index, third index with third to last index, etc
     for (var i = 0; i < array.length/2; i++) {
         for (var j = (array.length-1); j > array.length/2; j--) {
             array[i] = array[j];
             array[j] = array[i];
             messageSplit.join(",");
     }
 };



 function reverseMessage2(string) {
     var array = string.split(" ");
     var newArray = []
     for (var i = 0; i < array.length/2; i++) {
         newArray.unshift(array[i]++);
         newArray.join(",");
         return newArray;
     }
 };

我是在正确的轨道上吗?

7 个答案:

答案 0 :(得分:4)

   function reverseMessage(string) {
     var array = string.split(" ");
     var result="";
     for (var i = array.length-1; i >=0; i--) {
         result+=array[i]+" ";
     }
    console.log(result);
 };

答案 1 :(得分:2)

所以在你的reverseMessage函数中你正在尝试交换你正在做的变量

array [i] = array [j]; array [j] = array [i];

哪个给出array [i] = array [j]所以让我们说array [i] =“This”和array [j] =“那个”所以根据你的代码数组[i]将是“那个”和array [j]也将是“那个”,所以你可以尝试这样的事情:

 function reverseMessage(string) {
     var array = string.split(" ");
     //loop through array and replace first index with last index, second
     //index with second to last index, third index with third to last index, etc
     for (var i = 0,j=(array.length)-1; i < array.length/2; i++) {
             temp=array[i]
             array[i] = array[j];
             array[j]=array[i]
             array[j] = temp; j--;}
       console.log(array.join(" "));
 };  reverseMessage("find you will pain only go you recordings security the into if");

答案 2 :(得分:1)

尝试这个

function reverseMessage( str ) {

    var arrStr = [];
    var temp ="";
    for(var i = 0 ; i < str.length ; i++)
    {
        if(str[i] === " ")
        {
            arrStr.push(temp);
            temp="";
        }
        else
        {
            temp += str[i]; 
        }
    }
    if(temp.length >= 0)
    {
        arrStr.push(temp);
    }
    var result = "";
    for(var x=arrStr.length-1 ; x >=0 ; x--)
    {
        result += " "+arrStr[x];
    }
    return result;
}


console.log(reverseMessage("I am here"));

答案 3 :(得分:1)

嗯,有人必须这样做:

&#13;
&#13;
function rev(s){
  var a = s.split(/ /),
      l = a.length;
      i = l/2 | 0;
  while (i--) a.splice(i, 1, a.splice(l-1-i, 1, a[i])[0]);
  return a.join(' ');
}

    document.write(rev('0 1 2 3 4 5 6 7 8 9'));
&#13;
&#13;
&#13;

答案 4 :(得分:1)

使用Array.split()Array.pop()Array.push()String.substr()

var flip = function(str) {
  var res = "", sp = " ",  arr = str.split(sp);
  while (arr.length)
    res += arr.pop() + sp;
  return res.substr(0, res.length - 1); //remove last space
};
alert(flip("reversed are string your in words the now"));

使用String.lastIndexOf()String.substr()

var flip = function(str) {
  var res = "", i, sp = ' ';
  while (str) {
    i = str.lastIndexOf(sp);
    res += str.substr(i + 1) + sp;
    str = str.substr(0, i);
  }
  return res.substr(0, res.length - 1); //remove last space
};

alert(flip("reversed are string your in words the now"));

答案 5 :(得分:0)

你想要做的是将单词消息拆分成单独的字符串,然后将它们加在一起,然后将它们反转到位。试试

function reverseMessage(message){
  var reversedMessage = ''; //A string to hold the reversed message
  var words = message.split(' ');
  for(var i =0; i< words.length; i++){
     reversedMessage +=  reverse(words[i]) + " ";// add the reversed word to the reversed message, with a space at the end so the words dont touch
  }
  return reversedMessage
}
//A helper function that reverses a string
function reverse(s) {
  var o = '';
  for (var i = s.length - 1; i >= 0; i--)
    o += s[i];
  return o;
}

来源:http://eddmann.com/posts/ten-ways-to-reverse-a-string-in-javascript/

答案 6 :(得分:0)

with `pop()` ;)

function reverseMessage(string) {
         var array = string.split(" ");
         var result="";
         while (array.length > 0) {
             result += array.pop()+" ";
         }
        console.log(result);
     };