gpt4 book ai didi

javascript - 为什么 if(number <=10) 不起作用?

转载 作者:行者123 更新时间:2023-11-28 20:18:29 25 4
gpt4 key购买 nike

var way = 'up';
var m25 = {
'n': 10
};

for (var somelongnumber = 0; i < 10000; i++) {
if (way === 'up') {
m25.n = m25.n + 5;
if (m25.n >= 90) {
way = 'down';
}
} else {
m25.n = m25.n - 5;
if (m25.n <= 10) {
way = 'down';
}
}

console.log(way + m25.n);
}

这个控制台.logs:

up15
up20
up25
up30
up35
up40
up45
up50
up55
up60
up65
up70
up75
up80
up85
down90
down85
down80
down75
down70
down65
down60
down55
down50
down45
down40
down35
down30
down25
down20
down15
down10
down5
down0
down-5
down-10
down-15
down-20
down-25
down-30
down-35
down-40
down-45
down-50
down-55

为什么它会超过零?我很困惑!

最佳答案

首先,您应该格式化代码,使其更易于阅读。那么你可能会注意到你的错误。 if (m25.n <= 10) way = 'down';应该是'up'

var way = 'up';
var m25 = {
'n': 10
};

for (var i = 0; i < 1000; i++) {
if (way === 'up') {
m25.n = m25.n + 5;
if (m25.n >= 90) {
way = 'down';
}
} else {
m25.n = m25.n - 5;
if (m25.n <= 10) {
way = 'up';
}
}

console.log(way + m25.n);
}

关于javascript - 为什么 if(number <=10) 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18807031/

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