翻译短语中的某些单词

时间:2014-02-05 01:04:36

标签: javascript

我试图反转一个句子的每一个字。即第2,第4,第6,第8 ......单词。这是我到目前为止所得到的,我只是无法设置正确的反击。我的话都被倒置而不是我想要的。我仍然在javascript的基础知识,这个问题将更容易使用函数和数组但我不能使用它们。

var str=prompt("Enter")
var length=str.length;    
var sentence="";    
var word = "";    
var counter = 1;

for(var i=0; i < length; i++) {   
    if (counter = 2){    
      if (str.charAt(i) != ' '){    
        word = str.charAt(i) + word;
        counter = 1
      }else {
        sentence += word +" ";
        word = "";
        counter=2
      }
   }
}

sentence += word;    
alert("the result is: "+sentence);

3 个答案:

答案 0 :(得分:1)

这样的事情应该做到这一点:

http://jsfiddle.net/QP4fL/5/

这是一个很长的啰嗦,但如果没有你说过在最简单的syntax可能无法使用的一些操作符,它确实会找到你正在寻找的东西。 :

var str="Test to make sure that this is working.";
var length=str.length;    
var sentence="";    
var word = "";   
var counter = 0;

for(var i=0; i < length; i++) {
    if(str[i]===" " && counter === 1){
        sentence += word+" ";
        counter = 0; word = "";
    } else if(str[i]===" " && counter === 0){
        sentence += word+" ";
        counter++; word = "";
    } else if(length-1 === i){
        word += str[i];
        sentence += word;
    } else if(counter === 1) {
        word = str[i] + word;
    } else {
        word += str[i];
    }
}

alert("the result is: "+sentence);

通过评论解释:

/* Define our base variables. We need a few things. A string to test,
the length of the string, what our sentence will end up being, a temp
word variable, and a counter to determine odds/etc. */

var str=prompt("Enter: ");
var length=str.length;    
var sentence="";    
var word = "";   
var counter = 0;

/* Typical for loop for every character in the string you provided. */
for(var i=0; i < length; i++) {

// Now we need to know a few things, as I'll discuss as we get to them.

/* First check. If we encounter a " " AND the counter is at 1 (meaning
we are at the second find of a " ", we want to add the word to our sentence
plus an additional space to make up for the lack of catching the " ". 
Furthermore, we need to reset our counter and our word variables. */
if(str[i]===" " && counter === 1){
    sentence += word+" ";
    counter = 0; word = "";

/* Second check. If we encounter a " " but the counter is still at 0,
we want to increment counter and add the word normally. Also, reset
the word. */

} else if(str[i]===" " && counter === 0){
    sentence += word+" ";
    counter++; word = "";

 /* Third check. If we encounter the end of our string, we may as well
 just print our word as is. */
} else if(length-1 === i){
    word += str[i];
    sentence += word;

 /* Fourth check. If we encounter a series of letters where the counter
 is at 1, we can reverse the string by adding `str[i]` BEFORE the current
 word string. This will ensure the NEW characters precede the EXISTING ones. */
} else if(counter === 1) {
    word = str[i] + word;

// And, if none of the above is true, just add the letter to our word string.
} else {
    word += str[i];
}
}

alert("the result is: "+sentence);

答案 1 :(得分:0)

这是一种方法:

var str = prompt("Enter"),
    words = str.split(' ');

for ( var i = 0, len = words.length; i < len; i++ ) {
    if ( i % 2 != 0 )
        words[i] = words[i].split('').reverse().join('');
    }

alert("the result is: " + words.join(' '));

首先,我使用words = str.split(' ')制作了一系列单词。 我只为这个条件i % 2 != 0循环每个单词并反转单词的字母。 Fianally,我使​​用words.join(' ')

将数组中的每个单词都放回到一个句子中

答案 2 :(得分:0)

嗯,我决定采用不同的方法解决它(下面的解释)。

http://jsfiddle.net/43DXM/2/

var str = prompt("Enter");
var sentence = [];
var split = str.split(' ');
function reverse(s){
    return s.split('').reverse().join('');
}
for(var i=0;i<split.length;i++){
    if(i%2===0){
        sentence.push(split[i]);
    } else {
        sentence.push(reverse(split[i]));
    }
}
var final = sentence.join(" ");
alert("the result is: "+final);

<强>解释

  • 像以前一样设置str
  • 让我们使用数组来收集单词而不是上面提到的方法。 它使操作更容易一些。
  • 使用split方法将建议的string拆分为 spaces它找到了。
  • 写一个quickie reverse function来轻松处理每个来电。
  • 运行for loop以查找奇数,并push这些项目 我们的arrayeven项,push reversed valuearray sentence
  • join sentence数组中的所有项目,中间有space

http://jsfiddle.net/43DXM/2/

编辑:似乎morgul的回答有点简化了相同的原则,并将命令链接起来,而不是长时间地将它放在一边。做得好的伙伴。