Skip to content

Instantly share code, notes, and snippets.

/S70 To-Do 02

Created Jun 2, 2017
Embed
What would you like to do?
var todo = (function(){
var tasks = [];
var STATE_P = '진행';
var STATE_C = '완료';
var addTask = (function(){
var id = 0;
return function(title){
tasks.push({id: id++, title: title, state: STATE_P});
render();
}
})();
var removeTask = function(id){
var isRemoved = false;
for(var i =0; i< tasks.length; i++){
if(tasks[i].id === id){
tasks.splice(i,1);
isRemoved = true;
break;
}
}
if(!isRemoved) {
warning('removeTask : invalid id');
}
render();
};
var changeState = function(id, state){
var ID = false, STATE;
for(var i = 0; i < tasks.length; i++){
if(tasks[i].id === id){
ID = id;
break;
}
}
if(ID === false) {
warning('changeState : invalid id - ' + id);
return;
}
if(STATE_P !== state && STATE_C !== state){
warning('changeState : invalid state - ' + state);
return;
}else{
STATE = state;
}
for(var i =0; i< tasks.length; i++){
if(tasks[i].id === ID){
tasks[i].state = STATE;
break;
}
}
render();
};
var warning = console.log;
var render = function(){
var task;
console.log('진행');
for(var i = 0; i < tasks.length; i++){
task = tasks[i];
if(task.state === '진행'){
console.log(task.id+'.', task.title+'('+task.state+')');
}
}
console.log('완료');
for(var i = 0; i < tasks.length; i++){
task = tasks[i];
if(task.state === '완료'){
console.log(task.id+'.', task.title+'('+task.state+')');
}
}
};
return {
addTask : addTask,
removeTask: removeTask,
changeState: changeState
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment