为什么这在我的第3个if语句中给我一个0?

时间:2016-06-02 15:00:35

标签: javascript

当我在第3个if语句中将此代码放入REPL时,我一直得到0。似乎没有人能够在我身边找到它。你能看出它有什么问题吗?

 var humanReadable = function (seconds){
      var sec = 0;
      var min = 0;
      var hr = 0;

      sec = seconds % 60;
      min = parseInt((seconds / 60) % 60);
      hr = parseInt( seconds / 3600 );

      if(sec<10){
        sec = "0"+sec;
      }
      if(min<10){
        min = "0"+min;
      }
      if(hr<10){
        min = "0"+hr;
      }
      console.log(hr + ":" + min + ":" + sec);
    };

humanReadable(0) // '00:00:00'
humanReadable(5) // '00:00:05'
humanReadable(60) // '00:01:00'
humanReadable(86399) // '23:59:59'
humanReadable(359999) // '99:59:59

3 个答案:

答案 0 :(得分:4)

我相信你的功能有错字:

public class MyFcmListenerService extends FirebaseMessagingService {

    @override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        String from = remoteMessage.getFrom();
        Map<String, String> data = remoteMessage.getData();
        String body = rawData.get("body");

        Log.i("From",from);
        Log.i("Message",body);

    }
}

答案 1 :(得分:0)

你的第三个if语句的正文应该是:hr = "0"+hr;我还在函数调用中添加了分号。

运行下面的更新代码,您会发现它会产生您想要的输出。

&#13;
&#13;
var humanReadable = function (seconds)
{
  var sec = 0;
  var min = 0;
  var hr = 0;

  sec = seconds % 60;
  min = parseInt((seconds / 60) % 60);
  hr = parseInt( seconds / 3600 );

  if(sec<10)
  {
    sec = "0"+sec;
  }
  if(min<10)
  {
    min = "0"+min;
  }
  if(hr<10)
  {
    hr = "0"+hr;
  }
  console.log(hr + ":" + min + ":" + sec);
};

humanReadable(0); // '00:00:00'
humanReadable(5); // '00:00:05'
humanReadable(60); // '00:01:00'
humanReadable(86399); // '23:59:59'
humanReadable(359999); // '99:59:59'
&#13;
&#13;
&#13;

答案 2 :(得分:-1)

if(hr<10){
        min = "0"+hr;
}

就是你拥有的

应该是

if(hr<10){
        hr = "0"+hr;
}