HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptMinor

Implement a Clock in JavaScript

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
javascriptimplementclock

Problem

How idiomatic is my code?

var Clock = function(hour, minute) {
  this.hour = hour || 0;
  this.minute = minute || 0;

  this.plus = function(minutes) {
    computed_minutes = this.minute + minutes
    if (computed_minutes > 60) {
      this.minute = computed_minutes % 60
      this.hour += computed_minutes / 60
    } else {
      this.minute = computed_minutes
    }
    if (this.hour >= 24) {
      this.hour = this.hour - 24
    }
    this.hour = Math.round(this.hour)
    this.minute = Math.round(this.minute)
    return this;
  },

  this.minus = function(minutes) {
    computed_minutes = this.minute - minutes
    if (computed_minutes = 10 ? n : "0" + n;
  }
  return format(this.hour) + ":" + format(this.minute)
}

module.exports = Clock;

Solution

Some issues:

  • Every instance of Clock gets its own instance of plus and minus rather than attaching these functions to the prototype.



  • Clock.at sets a global variable c.



  • Clock.prototype.toString creates the format function each time it is called; you can move this out of the function and into a different scope so it is only created once.



  • plus and minus do a lot of checking that could be moved into a single set function and then this can also be called from the constructor (so that the same checks and rounding is also done in the constructor).



  • 24 and 60 appear as magic constants. In this context it is easy to understand what they are but it would be better to assign them to named constants (HOURS_PER_DAY and MINUTES_PER_HOUR) that identify why those magic numbers are being used.



My suggestions:

module.export = (function(){
    var Clock = function( hours, minutes ) {
      this.set( hours, minutes )
    }

    Clock.at = function(hours, minutes) {
      return new Clock(hours, minutes);
    }

    Clock.HOURS_PER_DAY = 24;
    Clock.MINUTES_PER_HOUR = 60;

    Clock.prototype.set = function( hours, minutes ){
        var hrs = Math.round( hours || 0 ); 
        var mns = Math.round( minutes || 0 );

        this.hour = ( hrs + Math.floor( mns / Clock.MINUTES_PER_HOUR ) ) % Clock.HOURS_PER_DAY;
        if ( this.hour = 10 ? n : "0" + n;
    }

    Clock.prototype.toString = function() {
          return format(this.hour) + ":" + format(this.minute)
    }

    return Clock;
})();

Code Snippets

module.export = (function(){
    var Clock = function( hours, minutes ) {
      this.set( hours, minutes )
    }

    Clock.at = function(hours, minutes) {
      return new Clock(hours, minutes);
    }

    Clock.HOURS_PER_DAY = 24;
    Clock.MINUTES_PER_HOUR = 60;

    Clock.prototype.set = function( hours, minutes ){
        var hrs = Math.round( hours || 0 ); 
        var mns = Math.round( minutes || 0 );

        this.hour = ( hrs + Math.floor( mns / Clock.MINUTES_PER_HOUR ) ) % Clock.HOURS_PER_DAY;
        if ( this.hour < 0 )
        {
            this.hour += Clock.HOURS_PER_DAY;
        }

        this.minute = mns % Clock.MINUTES_PER_HOUR;
        if ( this.minute < 0 )
        {
            this.minute += Clock.MINUTES_PER_HOUR;
        }

        return this;
    }

    Clock.prototype.plus = function(minutes) {
     return this.set( this.hour, this.minute + minutes )
    },

    Clock.prototype.minus = function(minutes) {
     return this.set( this.hour, this.minute - minutes );
    },

    Clock.prototype.equals = function(other) {
     return this.hour == other.hour && this.minute == other.minute;
    }

    function format(n) {
        return n >= 10 ? n : "0" + n;
    }

    Clock.prototype.toString = function() {
          return format(this.hour) + ":" + format(this.minute)
    }

    return Clock;
})();

Context

StackExchange Code Review Q#70054, answer score: 3

Revisions (0)

No revisions yet.