对于循环,如何在一行中显示每个字符
For loop, how can I display each character on one single line
var text = prompt("What is Lance trying to say");
//user type any text
for (var i = 0; i <= text.length; i++)
{
var result = text.charAt(i);
if (result == "e" || result == "o" || result == "a" || result == "u")
{
result = "i";
}
else if (result != "e")
{
result == result;
}
console.log(result);
}
当用户输入 "hello"
时我现在得到的结果
h
i
l
l
i
如何在一行中获取每个字符。
您可以使用列表来存储您想要的字符并加入它:
var text = prompt("What is Lance trying to say");
//user type any text
var char_list = [];
for (var i = 0; i <= text.length; i++) {
var result = text.charAt(i);
if (result == "e" || result == "o" || result == "a" || result == "u") {
char_list.push("i");
}else if (result != "e") {
char_list.push(result);
}
}
console.log(char_list.join(''));
不是每次都return输入字母,而是将结果附加到单个变量。循环完成后 return 变量
我想这就是您想要实现的目标 - 没有按请求进行数组连接和推送操作。我还修复了 for 循环中的 <=
;而且 else if
没有意义,只是 else
是正确的
var text = prompt("What is Lance trying to say");
//user type any text
var collect = '';
for (var i = 0; i < text.length; i++) {
var result = text.charAt(i);
if (result == "e" || result == "o" || result == "a" || result == "u") {
collect += "i";
} else {
collect += result;
}
}
console.log(collect);
这里有几种不同的比较方法:
使用一系列 if 语句。请注意 for 语句使用更紧凑的语法。
var text = prompt("What is Lance trying to say");
//user type any text
var result = "";
for (var i in text) {
var ch = text.charAt(i);
if (ch == "e" || ch == "o" || ch == "a" || ch == "u") {
result = result + "i";
} else {
result = result + ch;
}
}
console.log(result);
使用 switch 语句
var text = prompt("What is Lance trying to say");
//user type any text
var result = "";
for (var i in text) {
var ch = text.charAt(i);
switch (ch) {
case "e": case "o": case "a": case "u":
result = result + "i"
break;
default:
result = result + ch;
break;
}
}
console.log(result)
使用正则表达式和三元运算符以及 += 运算符将值附加到字符串
var text = prompt("What is Lance trying to say");
//user type any text
var result = '';
for (var i in text) {
result += function(ch) {
return ch.match(/[eoau]/) ? "i" : ch
}(text.charAt(i));
}
console.log(result);
祝你学习顺利。
var text = prompt("What is Lance trying to say");
//user type any text
for (var i = 0; i <= text.length; i++)
{
var result = text.charAt(i);
if (result == "e" || result == "o" || result == "a" || result == "u")
{
result = "i";
}
else if (result != "e")
{
result == result;
}
console.log(result);
}
当用户输入 "hello"
时我现在得到的结果h
i
l
l
i
如何在一行中获取每个字符。
您可以使用列表来存储您想要的字符并加入它:
var text = prompt("What is Lance trying to say");
//user type any text
var char_list = [];
for (var i = 0; i <= text.length; i++) {
var result = text.charAt(i);
if (result == "e" || result == "o" || result == "a" || result == "u") {
char_list.push("i");
}else if (result != "e") {
char_list.push(result);
}
}
console.log(char_list.join(''));
不是每次都return输入字母,而是将结果附加到单个变量。循环完成后 return 变量
我想这就是您想要实现的目标 - 没有按请求进行数组连接和推送操作。我还修复了 for 循环中的 <=
;而且 else if
没有意义,只是 else
是正确的
var text = prompt("What is Lance trying to say");
//user type any text
var collect = '';
for (var i = 0; i < text.length; i++) {
var result = text.charAt(i);
if (result == "e" || result == "o" || result == "a" || result == "u") {
collect += "i";
} else {
collect += result;
}
}
console.log(collect);
这里有几种不同的比较方法:
使用一系列 if 语句。请注意 for 语句使用更紧凑的语法。
var text = prompt("What is Lance trying to say");
//user type any text
var result = "";
for (var i in text) {
var ch = text.charAt(i);
if (ch == "e" || ch == "o" || ch == "a" || ch == "u") {
result = result + "i";
} else {
result = result + ch;
}
}
console.log(result);
使用 switch 语句
var text = prompt("What is Lance trying to say");
//user type any text
var result = "";
for (var i in text) {
var ch = text.charAt(i);
switch (ch) {
case "e": case "o": case "a": case "u":
result = result + "i"
break;
default:
result = result + ch;
break;
}
}
console.log(result)
使用正则表达式和三元运算符以及 += 运算符将值附加到字符串
var text = prompt("What is Lance trying to say");
//user type any text
var result = '';
for (var i in text) {
result += function(ch) {
return ch.match(/[eoau]/) ? "i" : ch
}(text.charAt(i));
}
console.log(result);
祝你学习顺利。