我必须使用 for 循环来遍历字符串中的每个字符,但代码无法正常工作

发布于 2025-01-15 13:44:19 字数 490 浏览 6 评论 0原文

这些是我应该做的练习的说明: 从要求用户输入任何字符串的提示开始。

使用 for 循环遍历字符串中的每个字符。

如果字符串包含字母 A(大写或小写),则跳出循环并将以下消息打印到屏幕上。

如果字符串不包含字母 A,则将以下消息打印到屏幕上。

这是我的代码

var text= prompt("Enter any string.")
for (var i = 0; i < text.length; i++) {
    if (text[i] === "A")
    {alert("The string contains the letter A.");
}
     if (text[i] === "a")
    {alert("The string contains the letter A.");
}
 else
      {alert("The string does not contain the letter A.");
}
}

These are the instruction for the exercise I am supposed to do:
Start with a prompt that asks the user to enter any string.

Using a for loop, go through each character in the string.

If the string contains the letter A (capital or lowercase), break out of the loop and print the message below to the screen.

If the string does not contain the letter A, print the message below to the screen.

Here is my code

var text= prompt("Enter any string.")
for (var i = 0; i < text.length; i++) {
    if (text[i] === "A")
    {alert("The string contains the letter A.");
}
     if (text[i] === "a")
    {alert("The string contains the letter A.");
}
 else
      {alert("The string does not contain the letter A.");
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

香草可樂 2025-01-22 13:44:19

您正在提醒循环的每次迭代,这意味着您正在为每个字母调用“警报”。

您可能想要做的是

var doesContainA = false;

在循环中创建一个像“Then”这样的变量,如果它等于 a 或 A,则将 doesContainA 更改为 true;

最后,执行一个最终的 if 语句,根据该变量是否变为 true 或仍然为 false 来控制要警告的消息。

此外,格式化程序可以通过排列所有内容来帮助您更轻松地阅读代码,例如 这个

You are alerting every iteration of the loop, meaning that you're calling 'alert' for each letter.

What you likely want to do is make a variable like

var doesContainA = false;

Then in your loop, if it equals a or A, change doesContainA to true;

At the end, do one final if statement which controls which message to alert, based on if that variable became true or is still false

Also, formatters can help you read code easier by lining everything up, something like this

灯下孤影 2025-01-22 13:44:19

使用返回布尔结果的单独函数。此方法允许您在第一次出现所需字母后停止迭代:

function hasLetter(text) {
    var char;
    for (var i = 0; i < text.length; i++) {
        char = text[i];
        if (char === "A" || char === "a") {
            return true;
        }
    }

    return false;
}

var text = prompt("Enter any string.");

if (hasLetter(text)) {
    alert("The string contains the letter A.");
} else {
    alert("The string does not contain the letter A.");
}

您还可以使用函数 String.toLowerCase 并检查字符是否与小写字母“a”匹配:

char = text[i].toLowerCase();
if (char === "a") {
    return true;
}

Use a separate function that returns a boolean result. This method lets you stop the iteration after first occurrence of desired letters:

function hasLetter(text) {
    var char;
    for (var i = 0; i < text.length; i++) {
        char = text[i];
        if (char === "A" || char === "a") {
            return true;
        }
    }

    return false;
}

var text = prompt("Enter any string.");

if (hasLetter(text)) {
    alert("The string contains the letter A.");
} else {
    alert("The string does not contain the letter A.");
}

You can also use function String.toLowerCase and check whether the char matches lowercase letter 'a':

char = text[i].toLowerCase();
if (char === "a") {
    return true;
}
べ繥欢鉨o。 2025-01-22 13:44:19

为什么需要循环来做到这一点,你可以通过这个

if(text.includes('A')){
    alert("The string contains the letter A.");
}else if(text.includes('a')){
    alert("The string contains the letter a.");
}else{
    alert("The string does not contain the letter A.");
}

UPDATE来完成

var text= prompt("Enter any string.")
var letterA = false;
var lettera = false
for (var i = 0; i < text.length; i++) {
    if (text[i] === "A")
    {
        letterA = true;
    }
    if (text[i] === "a")
    {
        lettera = true
    }
}
if(letterA=== true){
    alert('string contains letter A');
}else if(lettera ===true){
    alert('string contains letter a');
}else{
     alert(' string does not contain a or A character');
}

Why do you need loop to do so, you can do it by this

if(text.includes('A')){
    alert("The string contains the letter A.");
}else if(text.includes('a')){
    alert("The string contains the letter a.");
}else{
    alert("The string does not contain the letter A.");
}

UPDATE

var text= prompt("Enter any string.")
var letterA = false;
var lettera = false
for (var i = 0; i < text.length; i++) {
    if (text[i] === "A")
    {
        letterA = true;
    }
    if (text[i] === "a")
    {
        lettera = true
    }
}
if(letterA=== true){
    alert('string contains letter A');
}else if(lettera ===true){
    alert('string contains letter a');
}else{
     alert(' string does not contain a or A character');
}
掩饰不了的爱 2025-01-22 13:44:19

编辑:因为OP特别想使用for循环。

  const str = "anything";
  for (let i = 0; i < str.length; i++) {
    const char = str[i];
    if (char === "A") {
      console.log("contains A");
    } else if (char === "a") {
      console.log("contains a");
    } else {
      console.log("do something...");
    }
  }

原始答案:

您可以使用 includes 方法而不是 for 循环。

  const str = "Anything";
  if (str.includes("A")) {
    console.log("contains A");
  } else if (str.includes("a")) {
    console.log("contains a");
  } else {
    console.log("do something...");
  }

EDIT: Since OP specifically wants to use a for loop.

  const str = "anything";
  for (let i = 0; i < str.length; i++) {
    const char = str[i];
    if (char === "A") {
      console.log("contains A");
    } else if (char === "a") {
      console.log("contains a");
    } else {
      console.log("do something...");
    }
  }

Original Answer:

You can use includes method instead of a for loop.

  const str = "Anything";
  if (str.includes("A")) {
    console.log("contains A");
  } else if (str.includes("a")) {
    console.log("contains a");
  } else {
    console.log("do something...");
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文