Skip to content

Instantly share code, notes, and snippets.

@awonak
Created August 31, 2015 18:45
Show Gist options
  • Save awonak/5c8a11adcd924793cbd5 to your computer and use it in GitHub Desktop.
Save awonak/5c8a11adcd924793cbd5 to your computer and use it in GitHub Desktop.
var task_counter = 0;
task = function() {
return {
add: function(numberToAdd) {
if (numberToAdd < 10) {
task_counter = task_counter + numberToAdd;
}
else {
console.log('You cannot add a number greater than 10');
}
},
remove: function(numerToRemove) {
if (numerToRemove > 10) {
task_counter = task_counter - numerToRemove;
}
else {
console.log('You cannot add a number greater than 10');
}
},
show: function() {
return task_counter;
}
}
}
@awonak
Copy link
Author

awonak commented Aug 31, 2015

We asked our junior JavaScript developer to write a simple module that tracks a number of tasks. The module should be able to increment the counter by a given value and decrement by a given value. The module should also ensure that the increment/decrement value is not greater than 10, but that may change later.

If you were to review this developer's code, what recommendations would you make?

@grantelope
Copy link

var taskCounter = 0;

var task = function () {
  return {
    add: function (numberToAdd) {
      if (numberToAdd < 10) {
        taskCounter = taskCounter + numberToAdd;
      } else {
       console.log('You cannot add a number greater than 10');
      }
    },
    remove: function (numberToRemove) {
      if (numberToRemove < 10) {
          taskCounter = taskCounter - numerToRemove;
      } else {
        console.log('You cannot add a number greater than 10');
      }
    },
    show: function () {
      return taskCounter;
    }
  }
};

function Task(maxValue, task_start) {
  this.maxValue = maxValue || 10;
  this.taskCounter = task_start || 0;

  this.maxValue = parseInt(this.maxValue, 0);
  this.taskCounter = parseInt(this.taskCounter, 0);
}

Task.prototype.show = function () {
  return this.taskCounter;
};

Task.prototype.add = function (numberToAdd) {

  var self = this;

  if (numberToAdd < this.maxValue) {
    this.taskCounter = this.taskCounter + numberToAdd;
  } else {
    console.log('You cannot add a number greater than 10');
  }

  return this;

};

Task.prototype.remove = function (numberToRemove) {
  if (numberToRemove < this.maxValue) {
    this.taskCounter = this.taskCounter - numberToRemove;
  } else {
    console.log('You cannot add a number greater than 10');
  }

  return this;
};

new Task(10, 4).add(1).show();
// 5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment