Javascript 数错数字
我遇到了一个奇怪的问题 我在服务器上保存了一个“当前正在运行”的剪辑,然后发送下一个或上一个剪辑的id,以确定是否需要向前或向后跳过Javascript 数错数字,javascript,node.js,if-statement,numbers,Javascript,Node.js,If Statement,Numbers,我遇到了一个奇怪的问题 我在服务器上保存了一个“当前正在运行”的剪辑,然后发送下一个或上一个剪辑的id,以确定是否需要向前或向后跳过 if ((request.command.substring(15) > device.currentClip)) { console.log("XXXXX command: " + request.command.substring(15)); console.log("XXXXX current clip
if ((request.command.substring(15) > device.currentClip)) {
console.log("XXXXX command: " + request.command.substring(15));
console.log("XXXXX current clip " + device.currentClip);
console.log("[INFO] skipping forward (playlist)");
return;
} else if ((request.command.substring(15) < device.currentClip)) {
console.log("XXXXX command: " + request.command.substring(15));
console.log("XXXXX current clip " + device.currentClip);
console.log("[INFO] skipping backward (playlist)");
return;
}
有人能解释一下(可能的)错误吗?substring()
返回一个字符串,字符串的比较是通过逐字符比较来完成的,因此:
- “10”<“9”,以及
- “10”<“11”
parseInt
功能,将两侧转换为int
(如果device.currentClip
已经是int
,则不需要再进行转换):
您还可以在引号之间打印整个
request.command
:console.log(“““+request.command+””)代码>?也许您在某个地方有触发行为的空间。使用request.command更新控制台输出
'goto: clip id: 12'
XXXXX command: 12
XXXXX current clip 11
[INFO] skipping forward (playlist)
'goto: clip id: 10'
XXXXX command: 10
XXXXX current clip 9
[INFO] skipping backward (playlist)
var intCommand = parseInt(request.command.substring(15), 10);
var intCurrentClip = parseInt(device.currentClip);
if (intCommand > intCurrentClip)) {
// the rest of the code