JavaScript 初学者 : Trouble with strings

标签 javascript

在我的代码顶部,我需要询问用户是否想玩游戏。使用字符串函数查看他们答案的第一个字母并将其变为小写。如果他们的答案 == "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/

相关文章:

javascript - Chrome 消息传递。 Uncaught Error : Error connecting to extension

javascript - mousemove jquery 上的多重转换

javascript - 类变量的 Javascript 等价物是什么?

javascript - 元素构建时触发 jQuery 更改事件

javascript - jQuery 使用一个元素的文本来切换包含相同文本的另一个元素?

javascript - 数组值未被方法更改

javascript - 如何将 mongoDb 与 cordova 应用程序一起使用(mongodb 与 js)

javascript - 用 JS 分页

JavaScript 构造函数和原型(prototype)对象

javascript - 如何在 Highcharts 中设置轴的最小上限?