简体   繁体   English

JavaScript While循环无法正常工作

[英]JavaScript While loop not working

I have been trying to develope javascript to recognise a name, display the name, its length and the the letters and what position they appear but i cannot seem to get the while loop to work. 我一直在尝试开发JavaScript以识别名称,显示名称,名称的长度和字母以及它们出现的位置,但是我似乎无法获得while循环的作用。 could someone please help me out, thanks. 有人可以帮我吗,谢谢。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html >
<head>
    <title>
        javascript
    </title>
    <script>
        function displayName(){

        var str = prompt("enter your name:");
        var len = str.length;
        var chara = str.charAt(0);
        var n = chara.indexOf(0);


        while(chara <= len){
                document.write("<br>the letter " + chara + " is number " + n);
                chara++;
            }

           document.write("the name entered is: " + str + "<br></br>" + "the length is " +  len);

            }

     </script>


</head>
  <body>

     <script>
      displayName();    
     </script>

    </body>
</html>    

your loop is using the wrong iterator. 您的循环使用了错误的迭代器。

function displayName(){

    var str = prompt("enter your name:");
    var len = str.length;
    var chara = 0;

    while(chara < len){
        document.write("<br>the letter " + str.charAt(chara) + " is number " + (chara + 1));
        chara++;
    }

    document.write("<br/>the name entered is: " + str + "<br></br>" + "the length is " +  len);


}

You have chara set to the first character, "A" for example. 你必须chara设置为第一个字符,“A”为例。 Then you try to do while A <= 4 (assuming length is 4), which will never be true. 然后,您尝试while A <= 4 (假设长度为4)时执行此操作,这将永远不会成立。 chara needs to be a number for this to work. chara必须是一个数字才能正常工作。

It looks like you're setting chara to the first character of the returned string. 看起来您正在将chara设置为返回字符串的第一个字符。 Try setting it to 0 尝试将其设置为0

var chara = 0;

and see if that works. 看看是否可行。

You are incrementing the wrong variable. 您正在递增错误的变量。 Instead of incrementing chara , you should increment n and reset chara to point to the next character of your string. 而不是增加chara ,您应该增加n并重置chara以指向字符串的下一个字符。 You are also using indexOf improperly - just set n = 0; 您还使用了不正确的indexOf只需设置n = 0; to start with 从开始

Your loop is comparing a character (the first character of the name) to a number (the length of the name). 您的循环正在将一个字符(名称的第一个字符)与一个数字(名称的长度)进行比较。 There are a few ways to approach the task you are trying to accomplish. 有几种方法可以解决您要完成的任务。

If you want to keep using a while loop, you should keep track of a variable that serves as a counter, which counts which iteration of the loop you are on. 如果要继续使用while循环,则应跟踪用作计数器的变量,该变量将对循环进行计数。 This would replace the chara variable. 这将替换chara变量。

here is a live example 这是一个现场的例子

function displayName(){

    var str = prompt("enter your name:");
    var len = str.length;
    var count = 0;


    while(count < len){
        document.body.innerHTML += "<br>the letter " + str.charAt(count) + " is number " + (count+1);
        count++;
    }

    document.body.innerHTML += "<br>the name entered is: " + str + "<br></br>" + "the length is " +  len;

}

I've replaced the calls to document.write() with the innerHTML parameter because it is considered better practice, but you can use either. 我已经用innerHTML参数替换了对document.write()的调用,因为它被认为是更好的做法,但是您可以使用其中任何一个。

As an aside, it would be more semantic to use a for loop for this task. 顺便说一句,对于此任务使用for循环会更具语义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM