【问题标题】:Converting string to array and display vertically将字符串转换为数组并垂直显示
【发布时间】:2021-04-30 04:03:47
【问题描述】:

我的作业要求我要求用户输入一个字符串,例如“这是美好的一天”,然后该字符串反向垂直显示,像这样..

  • 漂亮
  • 一个
  • 这个

我遇到的问题是,字符串不是由 smaces 分隔,而是分隔每个字符并像这样显示字符串...

  • d
  • 一个
  • l
  • f

等等……

我尝试弄乱变量中引号之间的空格以及“sentence.split(”"),但没有运气。

  <! DOCTYPE>
  <html>
<head>
<meta charset = "utf-8"> 
  <script>

  function allStrings() 
  {
    var sentence = window.prompt( "Enter a sentence:" );
    var splitString  = sentence.split( " " );
    var flip = "";

    for( i=splitString.length-1; i>=0; i-- )
    {
      flip = flip + " " + splitString[i];
    }
      for( i=0; i<flip.length; i++ )
    {
      document.write(flip[i]);
      document.write("<br>");
    }
  }

  allStrings();
  </script>
    </head>
  </html>

【问题讨论】:

    标签: javascript arrays string


    【解决方案1】:

    你只需要在迭代之前将'flip'变成一个数组。

    for( i=splitString.length-1; i>=0; i-- )
        {
          flip = flip + " " + splitString[i];
        }
    
          // at this point flip is a string, so the next for loop will iterate through each character
          // convert to an array
      flip = flip.split(" ");
    
      for( i=0; i<flip.length; i++ )
        {
          document.write(flip[i]);
          document.write("<br>");
        }
    

    【讨论】:

      【解决方案2】:

      我对您的代码做了一些小改动。

      <! DOCTYPE>
        <html>
      <head>
      <meta charset = "utf-8"> 
        <script>
      
        function allStrings() 
        {
          var sentence = window.prompt( "Enter a sentence:" );
          var splitString  = sentence.split( " " ).reverse();
       
            for( i=0; i<splitString.length; i++ )
          {
            document.write(splitString[i]);
            document.write("<br>");
          }
        }
      
        allStrings();
        </script>
          </head>
        </html>
      

      【讨论】:

        【解决方案3】:

        解决方案可以简单得多:

        const fn = () => {
          const sentence = window.prompt( "Enter a sentence:" );
          return sentence.split(' ').reverse().join('<br>');
        }
        document.getElementById('content').innerHTML = fn();
        &lt;div id="content"&gt;&lt;/div&gt;

        【讨论】:

        • 谢谢,这非常简单。我们还没有在我的课堂上使用反向方法,所以我认为我应该使用本章中讨论的与该作业相关的方法。我们正在使用“Internet and World Wide Web How to Program Dietel 第 5 版”。它已经过时了,但我们正在使用它。
        【解决方案4】:

        您不需要flip 变量和第二个loop。您可以打印出 splitString 数组中的值。

        <html>
        
        <head>
            <meta charset="utf-8">
            <script>
        
                function allStrings() {
                    var sentence = window.prompt("Enter a sentence:");
                    var splitString = sentence.split(" ");
        
                    document.write("<ul>");
        
                    for (i = splitString.length - 1; i >= 0; i--) {
                        document.write("<li>" + splitString[i] + "</li>");
                    }
        
                    document.write("</ul>");
                }
        
                allStrings();
            </script>
        </head>
        
        </html>
        

        【讨论】:

        • 谢谢,这比我尝试做的更简单、更直接。
        • 如果您觉得这个答案有帮助,请接受它。
        猜你喜欢
        • 2021-06-03
        • 2021-12-04
        • 1970-01-01
        • 1970-01-01
        • 2022-11-14
        • 1970-01-01
        • 2017-09-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多