Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 颠倒短语中的某些单词_Javascript - Fatal编程技术网

Javascript 颠倒短语中的某些单词

Javascript 颠倒短语中的某些单词,javascript,Javascript,我试着倒转句子中的每一个单词。i、 e第二、第四、第六、第八,。。话。到目前为止,这就是我得到的,我只是无法将计数器设置正确。我的话都被颠倒了,而不是我想要的。我仍在学习javascript的基础知识,使用函数和数组会使这个问题变得更容易,但我不能使用它们 var str=prompt("Enter") var length=str.length; var sentence=""; var word = ""; var counter = 1; for(var i=0;

我试着倒转句子中的每一个单词。i、 e第二、第四、第六、第八,。。话。到目前为止,这就是我得到的,我只是无法将计数器设置正确。我的话都被颠倒了,而不是我想要的。我仍在学习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);
var str=提示(“输入”)
变量长度=str.length;
var句子=”;
var-word=“”;
var计数器=1;
对于(var i=0;i
这是一种方法:

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(' '));
var str=prompt(“回车”),
单词=str.split(“”);
for(var i=0,len=words.length;i
首先,我使用
words=str.split(“”)
创建了一个单词数组。 我只在这种情况下循环每个单词并反转单词的字母
I%2!=0

最后,我使用
单词将数组中的每个单词放回一个句子。join(“”)
好吧,我决定用不同的方法处理它(解释如下)。

var str=提示(“输入”);
var语句=[];
var split=str.split(“”);
功能反转(s){
返回s.split(“”).reverse().join(“”);
}
对于(var i=0;类似这样的事情应该可以做到:
这是一个长篇大论,但是如果没有一些您说无法在最简单的
语法中使用的运算符,它就可以实现您想要的功能:

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);
var str=“进行测试以确保其正常工作。”;
变量长度=str.length;
var句子=”;
var-word=“”;
var计数器=0;
对于(变量i=0;i
注释说明:
/*定义基本变量。我们需要一些东西。要测试的字符串,
字符串的长度,我们的句子最终是什么,一个临时值
单词变量,以及用于确定赔率/等的计数器*/
var str=提示(“输入:”;
变量长度=str.length;
var句子=”;
var-word=“”;
var计数器=0;
/*对于您提供的字符串中的每个字符,都是典型的for循环*/
对于(变量i=0;i
谢谢。我们还没有学会如何使用split。因此我不能使用它。但我会保留它以备将来参考。当然,让我很快地玩你的。你至少可以使用
substr
吗?是的,你可以。我从来没有想过要使用它。我记得在charAt身上看到过。当然,请稍等。用除了
+
-
变量、一些
if-else
语句和基本的
字符串连接
。希望这对你有用。如果需要,可以提供解释。这是我从一开始就想做的。但后来我尝试了一些东西,却迷路了。谢谢你,为你添加了解释。谢谢你接受的建议答复:-)
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);