将输入字符串转换为数组,以便可以操作每个单词

时间:2017-03-03 01:33:33

标签: javascript arrays input words

我正在尝试从// PROGRAM 1中创建的输入字符串创建一个数组,以便我可以操作它。创建数组后,我发现很多关于如何执行此操作,但我需要先创建该数组。

我只需要将输入字符串转换为分隔每个单词的数组所需的任何函数。我实际上可能输入了代码,是“var res = one.split(”“);”但我不知道接下来要做什么,以便我可以记录每个长度,以便我可以为任何超过5个字符的人添加单词。

(下面的更新版本。)如何在第24行修复错误?

<!DOCTYPE html>
<html>
<head>
<title>Project 1 – Michael Fiorello</title>
<script>
do{
    //MAIN MENU
var input = prompt ("Please enter 1, 2, 3, or exit.");{
//PROGRAM 1-Enter the string to be converted to robot speak
    if(input == "1")
     do{
         var one = prompt ("Please enter a string.");{
          if (one == "") { console.warn("You need to enter something");}
             }

         }while (one == "")//keep repeating program 1 until something is entered, aka cannot be blank.

//PROGRAM 2-Convert the string into robot speak
    else if (input == "2")
        {
          if (one == null) {console.warn ("You need to first enter a String")}
            else {console.log ("String Converted")
                var res = one.split(" ");{
                    for(i = 0, i<Arr.length, i++)
                     if(res[i].length >= 5)
                  {
                      Document.write(Arr.[i]+"-blip");
                  }
                  else{
                      Document.write(Arr.[i]+"-clang");
                  }
                }

                 }
                 }
    //Program 3 Robot Language version of the string will appear in the console
    else if (input == "3")
    {
            alert ("AWESOME!"); 
        }
    else if (input.toLowerCase() == "exit")
        {
        alert ("Thanks for using the ROBOT Language Converter!");
        }
    else 
        {
        alert ("Nope");
        console.warn("You need to enter something");
        }
    }
    }while(input.toLowerCase() != "exit");
</script>
</head>
<body>
<h1></h1>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

如何在javascript中拆分字符串

"str".split("") // ["s","t","r"]
"first second".split(" ") // ["first","second"]

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split