gpt4 book ai didi

javascript - 反转短语中的某些单词

转载 作者:行者123 更新时间:2023-11-30 05:39:30 24 4
gpt4 key购买 nike

我正在尝试颠倒句子中的每个第二个单词。即第 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);

最佳答案

像这样的东西应该可以解决问题:

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

这是一个冗长的长篇大论,但它完全符合您的要求,没有一些您说您不能以最简单的语法可能使用的运算符:

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);

关于javascript - 反转短语中的某些单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21566742/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com