gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 04:55:48 24 4
gpt4 key购买 nike

我正在尝试从//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>

最佳答案

这就是如何在 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

关于javascript - 将输入字符串转换为数组,以便可以操作每个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42569083/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com