Javascript 全局变量计数

Javascript 全局变量计数,javascript,Javascript,当math>5时,我正在计算多少次,现在正在计算,但当math=5){ monkey++; 控制台日志(monkey); }否则{ console.log(“我们”) } } 你好 您错过了monkey的重置 var monkey = 0; function Normal() { var math = Math.floor((Math.random() * 10) + 1); if (math >= 5) { monkey++; cons

当math>5时,我正在计算多少次,现在正在计算,但当math<5时,我希望monkey重置为0。然后它将从0开始再次计数

    var monkey = 0;

function Normal() {

    var math = Math.floor((Math.random() * 10) + 1);
    if (math >= 5) {
        monkey++;
        console.log(monkey);
    } else {

        console.log("We")
    }
}

<button onclick="Normal()">Hello</button>
var-monkey=0;
函数正规(){
var math=math.floor((math.random()*10)+1);
如果(数学>=5){
monkey++;
控制台日志(monkey);
}否则{
console.log(“我们”)
}
}
你好

您错过了monkey的重置

var monkey = 0;

function Normal() {

    var math = Math.floor((Math.random() * 10) + 1);
    if (math >= 5) {
        monkey++;
        console.log(monkey);
    } else {
        monkey = 0;
        console.log("We")
    }
}

<button onclick="Normal()">Hello</button>
var-monkey=0;
函数正规(){
var math=math.floor((math.random()*10)+1);
如果(数学>=5){
monkey++;
控制台日志(monkey);
}否则{
猴子=0;
console.log(“我们”)
}
}
你好

所以把
monkey=0
放在else块中。我是不是遗漏了什么?@JosiahKeller也许数学>=5改成数学>5也会有点帮助:)@JosiahKeller那没用,它只会回来NaN@user5544792显示正在执行此操作的代码。@user5544792
monkey=0
not
var monkey=0
,您不想定义新的局部变量。好的,谢谢,我不小心将var添加到了else部分。