gpt4 book ai didi

javascript - 减去两个 HH :MM strings with a recursive function

转载 作者:行者123 更新时间:2023-11-30 12:50:03 25 4
gpt4 key购买 nike

我一直在尝试获取 this上类,我找到了here并根据我的需要进行调整。目前它正在达到预期的值,但是返回值不会在返回时传递到函数之外。我试过对其进行调试,但似乎无法找到/理解问题所在。

该函数应该减去格式为“hh:mm”的两个字符串并返回格式相同的字符串。其中一个字符串是由用户提供的,我们减去在给定截止日期前提供服务所需的时间。

这是我用来跟踪值的带有 console.log() 语句的代码:

function subtractMinutes(time, minsToSubtract) {
/* Converts "hh:mm" format to a total in minutes */
function toMinutes (hh) {
//if in the recursion it will be an int instead of a string
if (hh < 0) {
return Math.abs(hh);
}
hh = hh.split(':');
return (parseInt(hh[0], 10) * 60) + parseInt(hh[1], 10);
}

/* Converts total in minutes to "hh:mm" format */
function toText (m) {
var minutes = m % 60;
var hours = Math.floor(m / 60);

minutes = (minutes < 10 ? '0' : '') + minutes;
hours = (hours < 10 ? '0' : '') + hours;

return hours + ':' + minutes;
}
console.log('time = '+time); //tracking values
console.log('minsToSubtract = '+minsToSubtract); //tracking values

time = toMinutes(time);
console.log('time toMinutes = '+time); //tracking values
minsToSubtract = toMinutes(minsToSubtract);
console.log('minsToSubtract toMinutes = '+minsToSubtract); //tracking values

var diff = time - minsToSubtract;
console.log('diff = '+diff); //tracking values

//if in recursion it will have to account for 24h/day instead of going to negative values
if (diff < 0) {
subtractMinutes("24:00", diff);
}
//end of recursion when diff>0 and the result may be returned
else {
console.log('diff = '+diff); //tracking values
var result = toText(diff);
console.log('result = '+result); //tracking values
return result; //at the end this value is correct, after this point it becomes "undefined"
}
}
var result = subtractMinutes("0:35", "01:00");
console.log(result);
console.log('---------------------');

非常欢迎任何建议,尽管不会考虑包/插件/库。

-----编辑-----

给定rlemon's answer我尝试了两种不同的方法来尝试对此进行排序,但没有成功:

    if (diff < 0) {
subtractMinutes("24:00", diff);
}
//removed the else part of the statement as suggested
console.log('diff = '+diff); //tracking values
var result = toText(diff);
console.log('result = '+result); //tracking values
return result;
}

这不再返回 undefined,但是 toText() 函数在递归解析正确的值后被调用,因此尝试转换值一秒钟时间,返回类似 "0-1:0-25" 的内容。

所以我认为我已经理解了这个问题,我尝试以另一种方式解决这个问题并执行了以下操作:

    var result;
if (diff < 0) {
subtractMinutes("24:00", diff);
} else {
console.log('diff after if= '+diff); //"diff after if= 1415"
result = toText(diff);
console.log('result = '+result); //"result = 23:35" CORRECT VALUE
return result; //tried with this one on and off, same problem
}
console.log('result before return = '+result); //"result before return = undefined"
return result;
}

我想建议在我的函数的最后一个结果上放置一个断点,以查看分配给它的值以及取消定义该值的实际情况。

最佳答案

在第一个函数调用后没有返回,所以它返回'undefined'。
举个简单的例子:

function foo( bar ) {
if( bar ) {
foo(false); // okay.. so you hit this and jump out of the if statement.
// now you think because the return is going to be triggered in the next
// pass that it will be the end result, but it isn't.
} else {
return 1;
}
// no return, so return undefined
}
foo(true)

将“递归”if 语句中的 subtractMinutes("24:00", diff); 更改为 return subtractMinutes("24:00", diff); 应该解决这个问题,因为现在它不会在第一次通话时终止。

查看演示:http://jsfiddle.net/rlemon/2YgyJ/

作为旁注:现在您不需要在那里有“else”语句,您可以完全省略它,只需在 if 之后运行代码:

//if in recursion it will have to account for 24h/day instead of going to negative values
if (diff < 0) {
return subtractMinutes("24:00", diff); // because of the return here, the code below is not hit until the if condition returns false
}
//end of recursion when diff>0 and the result may be returned
console.log('diff = ' + diff); //tracking values
var result = toText(diff);
console.log('result = ' + result); //tracking values
return result;

关于javascript - 减去两个 HH :MM strings with a recursive function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21271490/

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