如何在 javascript 中更改现有变量

How to change an existing variable in javascript

本文关键字:变量 javascript      更新时间:2023-09-26
    var userChoice = prompt("Do you want to choose rock, paper, or scissors?")
var computerChoice = Math.random()
if (computerChoice <= 0.33) {
    computerChoice("Rock");
} else if (computerChoice >= 0.67) {
    computerChoice("Scissors");
} else {
    computerChoice("Paper");
}

我是网站和编码的新手。 我试图创建一个简单的石头,纸,剪刀游戏,忘记了如何将computerChoice变量更改为a.Rock,b.Scissors或c.paper?

computerChoice("Scissors");

应该是

computerChoice="Scissors";

这就是你如何为变量分配一些东西。您使用的格式用于调用函数。

您已经在代码的前几行中为赋值使用了正确的语法

var computerChoice = Math.random()

事情就是这样,只是如果你想分配一个新值,你只是不要用var声明它

声明语法:

var variableName = Value   // if Value is a string, it goes inside quotes

分配新值

variableName = NewValue   // if NewValue is a string, it goes inside quotes