【发布时间】:2017-07-10 06:01:53
【问题描述】:
该程序应该将您输入的单词放入一个盒子中,将其拆分为一组字母并将它们随机排列。之后,它将第一个字母大写,其余字母小写,并将其输出到同一个框中。
我正在尝试将一个单词打乱成随机字母,但我无法克服这个错误。
在 chrome 中它说我有一个意外的标识符,在 mozilla 中它说我缺少一个用于 newWord = shuffle(newWord); 的括号*固定
编辑:现在我有一个错误说大写不是一个函数。
<html>
<head>
<title>Final</title>
</head>
<body>
<h1>Final</h1> Random Word scrambler
<br>
<input type="text" id="word">
<input type="button" id="submit" value="Randomize" onclick="Random()">
<script language="javascript">
word = document.getElementById("word").value
var n = word.length;
function Random() {
for (var start = 0; start < n; start++) {
var newWord = word.charAT(start)
newWord = shuffle(newWord);
function shuffle(array) {
var currentIndex = array.length,
temporaryValue, randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function capitalize(str) {
return str.replace(/\w\S*/g, function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
Array.join(newWord);
}
}
if (newWord == newWord){
document.getElementById("word").value = (capitalize(newWord));
}
}
</script>
</body>
</html>
【问题讨论】:
-
您收到此行的错误 b/c:
var newWord = (word.charAT(start),您需要先删除( -
而且,
charAT应该是charAt。
标签: javascript html arrays string