嗨,我想知道如何将以下三元运算作为常规条件语句编写。如果有人能让我知道会非常感激,这里是代码:
h1.textContent = "Time : " + (minutes ? (minutes > 9 ? minutes : "0" + minutes) : "00") + ":" + (seconds > 9 ? seconds : "0" + seconds);
简单的“条件陈述”替代方案
var minutesS = minutes;
if (minutes < 10) minutesS = '0' + minutes;
var secondsS = seconds;
if (seconds < 10) seconddS = '0' + seconds;
h1.textContent = "Time : " + minutesS + ":" + secondsS;
你可以采用一个函数和一些字符串方法。
function twoDigits(value) {
return ('00' + value.toString()).slice(-2);
}
h1.textContent = "Time : " + twoDigits(minutes) + ":" + twoDigits(seconds);