我试图反转一个句子的每一个字。即第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);
答案 0 :(得分:1)
这是一个很长的啰嗦,但如果没有你说过在最简单的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)
嗯,我决定采用不同的方法解决它(下面的解释)。
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
这些项目
我们的array
或even
项,push
reversed value
到array sentence
。join
sentence
数组中的所有项目,中间有space
。