gpt4 book ai didi

JavaScript 初学者 : Trouble with strings

转载 作者:行者123 更新时间:2023-11-30 18:31:21 24 4
gpt4 key购买 nike

在我的代码顶部,我需要询问用户是否想玩游戏。使用字符串函数查看他们答案的第一个字母并将其变为小写。如果他们的答案 == "y"然后玩游戏。我需要帮助将 Yes 的第一个字母小写。

<HTML>
<HEAD>
</HEAD>
<BODY>

<FORM NAME="testform">
<BR>
<BR>
<BR>
</FORM>

<INPUT id="attempts" TYPE="text" NAME="inputbox" VALUE="" />
<INPUT id="zero" TYPE="button" NAME="resetbox" VALUE="Reset " onclick="reset()" />

<SCRIPT type="text/javascript">

varattempts = 0
x = Math.round((Math.random()*19))+1
var tip

tip=prompt("Do you want to play a game?")

while(tip == "y")

{

var Guess;
document.getElementById('attempts').value = 0;
do {

Guess = prompt("Pick a number between 1 and 20","")
if (Guess === null) break;
document.getElementById('attempts').value = parseInt(document.getElementById('attempts').value)+1
} while (Guess!=x);

if (Guess == x)

{

alert("You guessed right!")

}
}

function reset()

{
varattempts=0;
document.getElementById('attempts').value = 'Attempts: 0';
}

</SCRIPT>
</BODY>
</HTML>

最佳答案

要获取任何字符串的首字母,可以使用.charAt(0):

var firstChar = myString.charAt(0);

并将其转换为小写:

var firstChar = myString.charAt(0).toLowerCase();

瞧!

关于JavaScript 初学者 : Trouble with strings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9526731/

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